Skip to content

Instantly share code, notes, and snippets.

@Jezza
Created December 23, 2015 21:57

Revisions

  1. Jezza created this gist Dec 23, 2015.
    20 changes: 20 additions & 0 deletions IO.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    public class IO {
    public static final int EOL = -1;
    public static final int DEFAULT_BUFFER_SIZE = 4096;
    public static final String SCANNER_END_OF_FILE_TOKEN = "\\A";

    public static String toString(Reader in) throws IOException {
    char[] arr = new char[DEFAULT_BUFFER_SIZE];
    StringBuilder buffer = new StringBuilder();
    int numCharsRead;
    while ((numCharsRead = in.read(arr, 0, DEFAULT_BUFFER_SIZE)) != EOL)
    buffer.append(arr, 0, numCharsRead);
    return buffer.toString();
    }

    public static String toString(InputStream in) throws IOException {
    try (Scanner s = new Scanner(in).useDelimiter(SCANNER_END_OF_FILE_TOKEN)) {
    return s.hasNext() ? s.next() : "";
    }
    }
    }