2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.util;
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.io.Reader;
13 import java.io.Writer;
14 import java.nio.charset.Charset;
16 import static com.threerings.getdown.Log.log;
18 public class StreamUtil {
20 * Convenient close for a stream. Use in a finally clause and love life.
22 public static void close (InputStream in)
27 } catch (IOException ioe) {
28 log.warning("Error closing input stream", "stream", in, "cause", ioe);
34 * Convenient close for a stream. Use in a finally clause and love life.
36 public static void close (OutputStream out)
41 } catch (IOException ioe) {
42 log.warning("Error closing output stream", "stream", out, "cause", ioe);
48 * Convenient close for a Reader. Use in a finally clause and love life.
50 public static void close (Reader in)
55 } catch (IOException ioe) {
56 log.warning("Error closing reader", "reader", in, "cause", ioe);
62 * Convenient close for a Writer. Use in a finally clause and love life.
64 public static void close (Writer out)
69 } catch (IOException ioe) {
70 log.warning("Error closing writer", "writer", out, "cause", ioe);
76 * Copies the contents of the supplied input stream to the supplied output stream.
78 public static <T extends OutputStream> T copy (InputStream in, T out)
81 byte[] buffer = new byte[4096];
82 for (int read = 0; (read = in.read(buffer)) > 0; ) {
83 out.write(buffer, 0, read);
89 * Reads the contents of the supplied stream into a byte array.
91 public static byte[] toByteArray (InputStream stream)
94 return copy(stream, new ByteArrayOutputStream()).toByteArray();