X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=getdown%2Fsrc%2Fgetdown%2Fcore%2Fsrc%2Fmain%2Fjava%2Fcom%2Fthreerings%2Fgetdown%2Futil%2FStreamUtil.java;fp=getdown%2Fsrc%2Fgetdown%2Fcore%2Fsrc%2Fmain%2Fjava%2Fcom%2Fthreerings%2Fgetdown%2Futil%2FStreamUtil.java;h=373cfff00a17158088028d8706e8e5c2e9317f0f;hb=74393b51f368cb9f58589472d432a433d9c4386d;hp=0000000000000000000000000000000000000000;hpb=7a0d503181fe41452120a8a02ca63476392aa08c;p=jalview.git diff --git a/getdown/src/getdown/core/src/main/java/com/threerings/getdown/util/StreamUtil.java b/getdown/src/getdown/core/src/main/java/com/threerings/getdown/util/StreamUtil.java new file mode 100644 index 0000000..373cfff --- /dev/null +++ b/getdown/src/getdown/core/src/main/java/com/threerings/getdown/util/StreamUtil.java @@ -0,0 +1,96 @@ +// +// Getdown - application installer, patcher and launcher +// Copyright (C) 2004-2018 Getdown authors +// https://github.com/threerings/getdown/blob/master/LICENSE + +package com.threerings.getdown.util; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.nio.charset.Charset; + +import static com.threerings.getdown.Log.log; + +public class StreamUtil { + /** + * Convenient close for a stream. Use in a finally clause and love life. + */ + public static void close (InputStream in) + { + if (in != null) { + try { + in.close(); + } catch (IOException ioe) { + log.warning("Error closing input stream", "stream", in, "cause", ioe); + } + } + } + + /** + * Convenient close for a stream. Use in a finally clause and love life. + */ + public static void close (OutputStream out) + { + if (out != null) { + try { + out.close(); + } catch (IOException ioe) { + log.warning("Error closing output stream", "stream", out, "cause", ioe); + } + } + } + + /** + * Convenient close for a Reader. Use in a finally clause and love life. + */ + public static void close (Reader in) + { + if (in != null) { + try { + in.close(); + } catch (IOException ioe) { + log.warning("Error closing reader", "reader", in, "cause", ioe); + } + } + } + + /** + * Convenient close for a Writer. Use in a finally clause and love life. + */ + public static void close (Writer out) + { + if (out != null) { + try { + out.close(); + } catch (IOException ioe) { + log.warning("Error closing writer", "writer", out, "cause", ioe); + } + } + } + + /** + * Copies the contents of the supplied input stream to the supplied output stream. + */ + public static T copy (InputStream in, T out) + throws IOException + { + byte[] buffer = new byte[4096]; + for (int read = 0; (read = in.read(buffer)) > 0; ) { + out.write(buffer, 0, read); + } + return out; + } + + /** + * Reads the contents of the supplied stream into a byte array. + */ + public static byte[] toByteArray (InputStream stream) + throws IOException + { + return copy(stream, new ByteArrayOutputStream()).toByteArray(); + } +}