X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=basic%2Fruntime%2Forg.argeo.basic.nodeps%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2FStreamUtils.java;fp=basic%2Fruntime%2Forg.argeo.basic.nodeps%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2FStreamUtils.java;h=d2ccdb737af9e2bad7e00907bd871f118e7a9449;hb=4310414c41cc0b9e52866f81ec6c60bbf6686d16;hp=10af68fa42f0862e72f477c18035b9af37eb55d2;hpb=d7c1552f4c28dc4acc25fb6214fb70d4fe67389d;p=lgpl%2Fargeo-commons.git diff --git a/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/StreamUtils.java b/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/StreamUtils.java index 10af68fa4..d2ccdb737 100644 --- a/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/StreamUtils.java +++ b/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/StreamUtils.java @@ -3,19 +3,40 @@ package org.argeo; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; /** Utilities to be used when APache COmmons IO is not available. */ public class StreamUtils { + private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; - public static void copy(InputStream in, OutputStream out) + /** @return the number of bytes */ + public static Long copy(InputStream in, OutputStream out) throws IOException { - byte[] buf = new byte[8192]; + Long count = 0l; + byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; while (true) { int length = in.read(buf); if (length < 0) break; out.write(buf, 0, length); + count = count + length; } + return count; + } + + /** @return the number of chars */ + 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); + if (length < 0) + break; + out.write(buf, 0, length); + count = count + length; + } + return count; } public static void closeQuietly(InputStream in) { @@ -36,6 +57,24 @@ public class StreamUtils { } } + public static void closeQuietly(Reader in) { + if (in != null) + try { + in.close(); + } catch (Exception e) { + // + } + } + + public static void closeQuietly(Writer out) { + if (out != null) + try { + out.close(); + } catch (Exception e) { + // + } + } + private StreamUtils() { }