From 880dee5d319c9cc20b1c3f5ddf18f6d288712858 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 24 Oct 2016 02:40:21 +0200 Subject: [PATCH] Added more stream utils (for ImageInputStream) --- Dorkbox-Util/src/dorkbox/util/IO.java | 63 ++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/IO.java b/Dorkbox-Util/src/dorkbox/util/IO.java index 6dfbc2f..1ef194b 100644 --- a/Dorkbox-Util/src/dorkbox/util/IO.java +++ b/Dorkbox-Util/src/dorkbox/util/IO.java @@ -15,12 +15,15 @@ */ package dorkbox.util; +import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -@SuppressWarnings("unused") +import javax.imageio.stream.ImageInputStream; + +@SuppressWarnings({"unused", "Duplicates"}) public class IO { /** @@ -70,7 +73,7 @@ class IO { /** * Copy the contents of the input stream to the output stream. - *

+ *

* DOES NOT CLOSE THE STEAMS! */ public static @@ -85,4 +88,60 @@ class IO { return outputStream; } + + /** + * Copy the contents of the input stream to the output stream. + *

+ * DOES NOT CLOSE THE STEAMS! + */ + public static + T copyStream(final ImageInputStream inputStream, final T outputStream) throws IOException { + byte[] buffer = new byte[4096]; + int read; + + while ((read = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, read); + } + outputStream.flush(); + + return outputStream; + } + + /** + * Copy the contents of the input stream to a new output stream. + *

+ * DOES NOT CLOSE THE STEAMS! + */ + public static + ByteArrayOutputStream copyStream(final InputStream inputStream) throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096); + byte[] buffer = new byte[4096]; + int read; + + while ((read = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, read); + } + outputStream.flush(); + + return outputStream; + } + + /** + * Copy the contents of the input stream to a new output stream. + *

+ * DOES NOT CLOSE THE STEAMS! + */ + public static + ByteArrayOutputStream copyStream(final ImageInputStream inputStream) throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096); + byte[] buffer = new byte[4096]; + int read; + + while ((read = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, read); + } + outputStream.flush(); + + return outputStream; + } }