|
@@ -17,29 +17,39 @@ import java.io.Reader;
|
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-import java.util.Scanner;
|
|
|
|
|
|
|
+import net.ranides.assira.collection.arrays.ArrayAllocator;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
* @author ranides
|
|
* @author ranides
|
|
|
*/
|
|
*/
|
|
|
public final class StringLoader {
|
|
public final class StringLoader {
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private StringLoader() { }
|
|
private StringLoader() { }
|
|
|
|
|
|
|
|
- public static String fromReader(Reader reader) {
|
|
|
|
|
- try(Scanner scanner = new Scanner(reader)) {
|
|
|
|
|
- return scanner.useDelimiter("\\Z").next();
|
|
|
|
|
|
|
+ public static String fromReader(Reader reader) throws IOException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ char[] buffer = new char[32];
|
|
|
|
|
+ int r, c = 0;
|
|
|
|
|
+ while((r = reader.read(buffer, c, buffer.length-c)) > 0) {
|
|
|
|
|
+ c += r;
|
|
|
|
|
+ buffer = ArrayAllocator.grow(buffer, c+32);
|
|
|
|
|
+ }
|
|
|
|
|
+ return new String(buffer, 0, c);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ reader.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static String fromStream(InputStream istream, Charset charset) {
|
|
|
|
|
- try(Scanner scanner = new Scanner(istream, charset.name())) {
|
|
|
|
|
- return scanner.useDelimiter("\\Z").next();
|
|
|
|
|
|
|
+ public static String fromStream(InputStream istream, Charset charset) throws IOException {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return fromReader(new InputStreamReader(istream, charset));
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ istream.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static String fromStream(InputStream istream) {
|
|
|
|
|
|
|
+ public static String fromStream(InputStream istream) throws IOException {
|
|
|
return fromStream(istream, Charsets.UTF8);
|
|
return fromStream(istream, Charsets.UTF8);
|
|
|
}
|
|
}
|
|
|
|
|
|