From c9212c2b40f2f01e2a16b5d1832b220f7bf2c0b0 Mon Sep 17 00:00:00 2001 From: Kevin Ruland Date: Wed, 25 Jul 2012 18:33:10 +0000 Subject: [PATCH] Added a couple of handy stream utilities. copy copies the contents of an InputStream to an OutputStream. readBytes returns the contents of an InputStream as a byte[]. --- .../src/net/sf/openrocket/util/FileUtils.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 core/src/net/sf/openrocket/util/FileUtils.java diff --git a/core/src/net/sf/openrocket/util/FileUtils.java b/core/src/net/sf/openrocket/util/FileUtils.java new file mode 100644 index 000000000..3956f6b2d --- /dev/null +++ b/core/src/net/sf/openrocket/util/FileUtils.java @@ -0,0 +1,41 @@ +package net.sf.openrocket.util; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public abstract class FileUtils { + + public static void copy( InputStream is, OutputStream os ) throws IOException { + + if ( ! (os instanceof BufferedOutputStream ) ) { + os = new BufferedOutputStream(os); + } + + if ( ! (is instanceof BufferedInputStream ) ) { + is = new BufferedInputStream(is); + } + + byte[] buffer = new byte[1024]; + int bytesRead = 0; + + while( (bytesRead = is.read(buffer)) > 0 ) { + os.write(buffer,0,bytesRead); + } + os.flush(); + } + + public static byte[] readBytes( InputStream is ) throws IOException { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); + + copy( is, bos ); + + return bos.toByteArray(); + + } + +}