1 package org
.argeo
.cms
.util
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.OutputStream
;
10 import java
.util
.StringJoiner
;
12 /** Stream utilities to be used when Apache Commons IO is not available. */
13 public class StreamUtils
{
14 private static final int DEFAULT_BUFFER_SIZE
= 1024 * 4;
17 * APACHE COMMONS IO (inspired)
20 /** @return the number of bytes */
21 public static Long
copy(InputStream in
, OutputStream out
) throws IOException
{
23 byte[] buf
= new byte[DEFAULT_BUFFER_SIZE
];
25 int length
= in
.read(buf
);
28 out
.write(buf
, 0, length
);
29 count
= count
+ length
;
34 /** @return the number of chars */
35 public static Long
copy(Reader in
, Writer out
) throws IOException
{
37 char[] buf
= new char[DEFAULT_BUFFER_SIZE
];
39 int length
= in
.read(buf
);
42 out
.write(buf
, 0, length
);
43 count
= count
+ length
;
48 public static byte[] toByteArray(InputStream in
) throws IOException
{
49 try (ByteArrayOutputStream out
= new ByteArrayOutputStream()) {
51 return out
.toByteArray();
55 public static void closeQuietly(InputStream in
) {
59 } catch (Exception e
) {
64 public static void closeQuietly(OutputStream out
) {
68 } catch (Exception e
) {
73 public static void closeQuietly(Reader in
) {
77 } catch (Exception e
) {
82 public static void closeQuietly(Writer out
) {
86 } catch (Exception e
) {
91 public static String
toString(BufferedReader reader
) throws IOException
{
92 StringJoiner sn
= new StringJoiner("\n");
94 while ((line
= reader
.readLine()) != null)