JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / util / StreamUtil.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.util;
7
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;
15
16 import static com.threerings.getdown.Log.log;
17
18 public class StreamUtil {
19     /**
20      * Convenient close for a stream. Use in a finally clause and love life.
21      */
22     public static void close (InputStream in)
23     {
24         if (in != null) {
25             try {
26                 in.close();
27             } catch (IOException ioe) {
28                 log.warning("Error closing input stream", "stream", in, "cause", ioe);
29             }
30         }
31     }
32
33     /**
34      * Convenient close for a stream. Use in a finally clause and love life.
35      */
36     public static void close (OutputStream out)
37     {
38         if (out != null) {
39             try {
40                 out.close();
41             } catch (IOException ioe) {
42                 log.warning("Error closing output stream", "stream", out, "cause", ioe);
43             }
44         }
45     }
46
47     /**
48      * Convenient close for a Reader. Use in a finally clause and love life.
49      */
50     public static void close (Reader in)
51     {
52         if (in != null) {
53             try {
54                 in.close();
55             } catch (IOException ioe) {
56                 log.warning("Error closing reader", "reader", in, "cause", ioe);
57             }
58         }
59     }
60
61     /**
62      * Convenient close for a Writer. Use in a finally clause and love life.
63      */
64     public static void close (Writer out)
65     {
66         if (out != null) {
67             try {
68                 out.close();
69             } catch (IOException ioe) {
70                 log.warning("Error closing writer", "writer", out, "cause", ioe);
71             }
72         }
73     }
74
75     /**
76      * Copies the contents of the supplied input stream to the supplied output stream.
77      */
78     public static <T extends OutputStream> T copy (InputStream in, T out)
79         throws IOException
80     {
81         byte[] buffer = new byte[4096];
82         for (int read = 0; (read = in.read(buffer)) > 0; ) {
83             out.write(buffer, 0, read);
84         }
85         return out;
86     }
87
88     /**
89      * Reads the contents of the supplied stream into a byte array.
90      */
91     public static byte[] toByteArray (InputStream stream)
92         throws IOException
93     {
94         return copy(stream, new ByteArrayOutputStream()).toByteArray();
95     }
96 }