]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/StreamUtils.java
1 package org
.argeo
.util
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.io
.OutputStream
;
9 import java
.util
.StringJoiner
;
11 /** Stream utilities to be used when Apache Commons IO is not available. */
12 public class StreamUtils
{
13 private static final int DEFAULT_BUFFER_SIZE
= 1024 * 4;
16 * APACHE COMMONS IO (inspired)
19 /** @return the number of bytes */
20 public static Long
copy(InputStream in
, OutputStream out
) throws IOException
{
22 byte[] buf
= new byte[DEFAULT_BUFFER_SIZE
];
24 int length
= in
.read(buf
);
27 out
.write(buf
, 0, length
);
28 count
= count
+ length
;
33 /** @return the number of chars */
34 public static Long
copy(Reader in
, Writer out
) throws IOException
{
36 char[] buf
= new char[DEFAULT_BUFFER_SIZE
];
38 int length
= in
.read(buf
);
41 out
.write(buf
, 0, length
);
42 count
= count
+ length
;
47 public static void closeQuietly(InputStream in
) {
51 } catch (Exception e
) {
56 public static void closeQuietly(OutputStream out
) {
60 } catch (Exception e
) {
65 public static void closeQuietly(Reader in
) {
69 } catch (Exception e
) {
74 public static void closeQuietly(Writer out
) {
78 } catch (Exception e
) {
83 public static String
toString(BufferedReader reader
) throws IOException
{
84 StringJoiner sn
= new StringJoiner("\n");
86 while ((line
= reader
.readLine()) != null)