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(); + + } + +}