Created
December 23, 2015 21:57
-
-
Save Jezza/5d1b9513ebea1e189c9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() : ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment