Multiple user referentials working with IPA.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / StreamUtils.java
index 6d7d940ceaf74ea3b9dd60682688cb918ef6c9ff..5e4e636fc45073f3615e5b9d6ce459bbc3c8ed3c 100644 (file)
@@ -1,13 +1,16 @@
 package org.argeo.util;
 
+import java.io.BufferedReader;
+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.util.StringJoiner;
 
-/** Utilities to be used when Apache Commons IO is not available. */
-class StreamUtils {
+/** Stream utilities to be used when Apache Commons IO is not available. */
+public class StreamUtils {
        private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 
        /*
@@ -15,8 +18,7 @@ class StreamUtils {
         */
 
        /** @return the number of bytes */
-       public static Long copy(InputStream in, OutputStream out)
-                       throws IOException {
+       public static Long copy(InputStream in, OutputStream out) throws IOException {
                Long count = 0l;
                byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
                while (true) {
@@ -43,6 +45,13 @@ class StreamUtils {
                return count;
        }
 
+       public static byte[] toByteArray(InputStream in) throws IOException {
+               try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+                       copy(in, out);
+                       return out.toByteArray();
+               }
+       }
+
        public static void closeQuietly(InputStream in) {
                if (in != null)
                        try {
@@ -78,4 +87,12 @@ class StreamUtils {
                                //
                        }
        }
+
+       public static String toString(BufferedReader reader) throws IOException {
+               StringJoiner sn = new StringJoiner("\n");
+               String line = null;
+               while ((line = reader.readLine()) != null)
+                       sn.add(line);
+               return sn.toString();
+       }
 }