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);
}
/** @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);
}
}
+ 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;