X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Futil%2FStreamUtils.java;h=f4242e6c273bbea6de0dd392256d40915109b9f6;hb=HEAD;hp=a589e739af389b3bf0ef22b5cc1f811acd124a62;hpb=55870eba50d8b28e72a3102fd18a17a6f23f7bad;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/util/StreamUtils.java b/org.argeo.cms/src/org/argeo/cms/util/StreamUtils.java index a589e739a..f4242e6c2 100644 --- a/org.argeo.cms/src/org/argeo/cms/util/StreamUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/util/StreamUtils.java @@ -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;