Add simple load methods.

This commit is contained in:
Christian P. MOMON 2013-08-12 14:34:56 +02:00
parent 620399fd97
commit 7372c65677

View file

@ -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