From 911e7e77510f2f018d3000b42b1ab7b9dc709570 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 14 Sep 2016 10:12:09 +1000 Subject: [PATCH] Fix downloaded schems + mcedit --- .../main/java/com/boydti/fawe/FaweAPI.java | 12 ++++-- .../io/AbstractDelegateOutputStream.java | 37 +++++++++++++++++++ .../java/com/boydti/fawe/util/MainUtil.java | 7 +++- 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 core/src/main/java/com/boydti/fawe/object/io/AbstractDelegateOutputStream.java diff --git a/core/src/main/java/com/boydti/fawe/FaweAPI.java b/core/src/main/java/com/boydti/fawe/FaweAPI.java index 8271923e..20210c38 100644 --- a/core/src/main/java/com/boydti/fawe/FaweAPI.java +++ b/core/src/main/java/com/boydti/fawe/FaweAPI.java @@ -11,12 +11,14 @@ import com.boydti.fawe.object.PseudoRandom; import com.boydti.fawe.object.RegionWrapper; import com.boydti.fawe.object.RunnableVal; import com.boydti.fawe.object.changeset.DiskStorageHistory; +import com.boydti.fawe.object.io.FastByteArrayOutputStream; import com.boydti.fawe.object.schematic.Schematic; import com.boydti.fawe.regions.FaweMaskManager; import com.boydti.fawe.util.EditSessionBuilder; import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MemUtil; import com.boydti.fawe.util.SetQueue; +import com.boydti.fawe.util.StringMan; import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.WEManager; import com.boydti.fawe.wrappers.WorldWrapper; @@ -44,6 +46,7 @@ import java.io.OutputStream; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -142,10 +145,11 @@ public class FaweAPI { @Override public void run(OutputStream value) { try { - GZIPOutputStream gzip = new GZIPOutputStream(value, true); - ClipboardWriter writer = format.getWriter(gzip); - writer.write(clipboard, null); - gzip.flush(); + try (GZIPOutputStream gzip = new GZIPOutputStream(value, true)) { + try (ClipboardWriter writer = format.getWriter(gzip)) { + writer.write(clipboard, null); + } + } } catch (IOException e) { e.printStackTrace(); } diff --git a/core/src/main/java/com/boydti/fawe/object/io/AbstractDelegateOutputStream.java b/core/src/main/java/com/boydti/fawe/object/io/AbstractDelegateOutputStream.java new file mode 100644 index 00000000..bc93baf4 --- /dev/null +++ b/core/src/main/java/com/boydti/fawe/object/io/AbstractDelegateOutputStream.java @@ -0,0 +1,37 @@ +package com.boydti.fawe.object.io; + +import java.io.IOException; +import java.io.OutputStream; + +public class AbstractDelegateOutputStream extends OutputStream { + private final OutputStream parent; + + @Override + public void write(int b) throws IOException { + parent.write(b); + } + + @Override + public void write(byte[] b) throws IOException { + parent.write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + parent.write(b, off, len); + } + + @Override + public void flush() throws IOException { + parent.flush(); + } + + @Override + public void close() throws IOException { + parent.close(); + } + + public AbstractDelegateOutputStream(OutputStream os) { + this.parent = os; + } +} diff --git a/core/src/main/java/com/boydti/fawe/util/MainUtil.java b/core/src/main/java/com/boydti/fawe/util/MainUtil.java index 7ffdf973..a73432a9 100644 --- a/core/src/main/java/com/boydti/fawe/util/MainUtil.java +++ b/core/src/main/java/com/boydti/fawe/util/MainUtil.java @@ -11,6 +11,7 @@ import com.boydti.fawe.object.RunnableVal; import com.boydti.fawe.object.RunnableVal2; import com.boydti.fawe.object.changeset.CPUOptimizedChangeSet; import com.boydti.fawe.object.changeset.FaweStreamChangeSet; +import com.boydti.fawe.object.io.AbstractDelegateOutputStream; import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.DoubleTag; import com.sk89q.jnbt.EndTag; @@ -281,7 +282,11 @@ public class MainUtil { writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(filename)).append(CRLF); writer.append("Content-Transfer-Encoding: binary").append(CRLF); writer.append(CRLF).flush(); - writeTask.value = output; + OutputStream nonClosable = new AbstractDelegateOutputStream(output) { + @Override + public void close() throws IOException { } // Don't close + }; + writeTask.value = nonClosable; writeTask.run(); output.flush(); writer.append(CRLF).flush();