From 7372c656776fadd1730cf00dfeaa771eb230d37c Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 12 Aug 2013 14:34:56 +0200 Subject: [PATCH] Add simple load methods. --- src/fr/devinsy/util/ToolBox.java | 81 +++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/src/fr/devinsy/util/ToolBox.java b/src/fr/devinsy/util/ToolBox.java index 32aa2b6..038b2c2 100644 --- a/src/fr/devinsy/util/ToolBox.java +++ b/src/fr/devinsy/util/ToolBox.java @@ -6,10 +6,12 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; +import java.net.URL; import java.util.Arrays; import java.util.List; import java.util.Set; @@ -250,12 +252,12 @@ public class ToolBox * @return * @throws IOException */ - static public String load(final File file) throws IOException + static public String load(final File source) throws IOException { String result; final String DEFAULT_CHARSET_NAME = "UTF-8"; - result = load(file, DEFAULT_CHARSET_NAME); + result = load(source, DEFAULT_CHARSET_NAME); // return result; @@ -266,11 +268,11 @@ public class ToolBox * @param file * @throws IOException */ - public static String load(final File file, final String charsetName) throws IOException + public static String load(final File source, final String charsetName) throws IOException { String result; - result = loadToStringBuffer(file, charsetName).toString(); + result = loadToStringBuffer(source, charsetName).toString(); // return result; @@ -282,12 +284,32 @@ public class ToolBox * @return * @throws IOException */ - static public StringBuffer loadToStringBuffer(final File file) throws IOException + static public String load(final URL source) throws IOException { - StringBuffer result; + String result; final String DEFAULT_CHARSET_NAME = "UTF-8"; - result = loadToStringBuffer(file, DEFAULT_CHARSET_NAME); + result = load(source, DEFAULT_CHARSET_NAME); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static String load(final URL source, final String charsetName) throws IOException + { + String result; + + // + StringBuffer buffer = new StringBuffer(source.openConnection().getContentLength() + 1); + read(buffer, source.openStream(), charsetName); + + // + result = buffer.toString(); // return result; @@ -343,6 +365,51 @@ public class ToolBox return result; } + /** + * + * @param file + * @throws IOException + */ + public static void read(final StringBuffer out, final InputStream is, final String charsetName) throws IOException + { + BufferedReader in = null; + try + { + in = new BufferedReader(new InputStreamReader(is, charsetName)); + + boolean ended = false; + final String LINE_SEPARATOR = System.getProperty("line.separator"); + + while (!ended) + { + String line = in.readLine(); + + if (line == null) + { + ended = true; + } + else + { + out.append(line).append(LINE_SEPARATOR); + } + } + } + finally + { + try + { + if (in != null) + { + in.close(); + } + } + catch (IOException exception) + { + exception.printStackTrace(); + } + } + } + /** * * @param file