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[].
This commit is contained in:
parent
6cf525467c
commit
c9212c2b40
41
core/src/net/sf/openrocket/util/FileUtils.java
Normal file
41
core/src/net/sf/openrocket/util/FileUtils.java
Normal file
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user