Improve documentation
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / StreamUtils.java
index a589e739af389b3bf0ef22b5cc1f811acd124a62..f4242e6c273bbea6de0dd392256d40915109b9f6 100644 (file)
@@ -6,20 +6,19 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Reader;
+import java.io.UncheckedIOException;
 import java.io.Writer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.StringJoiner;
 
 /** Stream utilities to be used when Apache Commons IO is not available. */
 public class StreamUtils {
        private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 
-       /*
-        * APACHE COMMONS IO (inspired)
-        */
-
        /** @return the number of bytes */
-       public static Long copy(InputStream in, OutputStream out) throws IOException {
-               Long count = 0l;
+       public static long copy(InputStream in, OutputStream out) throws IOException {
+               long count = 0l;
                byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
                while (true) {
                        int length = in.read(buf);
@@ -32,8 +31,8 @@ public class StreamUtils {
        }
 
        /** @return the number of chars */
-       public static Long copy(Reader in, Writer out) throws IOException {
-               Long count = 0l;
+       public static long copy(Reader in, Writer out) throws IOException {
+               long count = 0l;
                char[] buf = new char[DEFAULT_BUFFER_SIZE];
                while (true) {
                        int length = in.read(buf);
@@ -88,6 +87,22 @@ public class StreamUtils {
                        }
        }
 
+       public static String toString(Class<?> clss, String resource) {
+               return toString(clss.getResourceAsStream(resource), StandardCharsets.UTF_8);
+       }
+
+       public static String toString(InputStream in) {
+               return toString(in, StandardCharsets.UTF_8);
+       }
+
+       public static String toString(InputStream in, Charset encoding) {
+               try {
+                       return new String(in.readAllBytes(), encoding);
+               } catch (IOException e) {
+                       throw new UncheckedIOException(e);
+               }
+       }
+
        public static String toString(BufferedReader reader) throws IOException {
                StringJoiner sn = new StringJoiner("\n");
                String line = null;