diff options
Diffstat (limited to 'src/main/java/org/openslx/satserver/util/Util.java')
-rw-r--r-- | src/main/java/org/openslx/satserver/util/Util.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/satserver/util/Util.java b/src/main/java/org/openslx/satserver/util/Util.java new file mode 100644 index 0000000..a6d354a --- /dev/null +++ b/src/main/java/org/openslx/satserver/util/Util.java @@ -0,0 +1,66 @@ +package org.openslx.satserver.util; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class Util +{ + + /** + * Check if the given string starts with any of the additionally passed strings. + * Ugly boilerplate. + * + * @param stringToCheck a string we want to check the starting of + * @param compareTo list of strings we compare stringToCheck to + * @return true if stringToCheck starts with any of the strings in compareTo + */ + public static boolean startsWith( String stringToCheck, String... compareTo ) + { + for ( String check : compareTo ) { + if ( stringToCheck.startsWith( check ) ) + return true; + } + return false; + } + + /** + * Close all given Closables. Can handle null references. + * @param streams one or more closables/streams + */ + public static void multiClose( Closeable... streams ) + { + if ( streams == null ) + return; + for ( Closeable stream : streams ) { + if ( stream != null ) { + try { + stream.close(); + } catch ( IOException e ) { + // Ignore - nothing meaningful to do + } + } + } + } + + public static boolean streamCopy( InputStream in, OutputStream out, long bytes ) + { + byte buffer[] = new byte[ 7900 ]; + while ( bytes > 0 ) { + try { + int ret = in.read( buffer, 0, (int) ( bytes > buffer.length ? buffer.length : bytes ) ); + if ( ret == -1 ) + return false; + bytes -= ret; + out.write( buffer, 0, ret ); + } catch ( IOException e ) { + e.printStackTrace(); + return false; + } + } + return true; + } + + +} |