import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
/**
* * ***Copied & Pasted from apache commons-io because I dont want to add too many 3rd parties library to the app to keep it as small as possible***
*
*/
public class IOUtils{
private IOUtils(){}
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
// read toString
//-----------------------------------------------------------------------
/**
* Get the contents of an InputStream
as a String
* using the default character encoding of the platform.
*
* This method buffers the input internally, so there is no need to use a
* BufferedInputStream
.
*
* @param input the InputStream
to read from
* @return the requested String
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
*/
public static String toString(InputStream input) throws IOException {
StringBuilderWriter sw = new StringBuilderWriter();
copy(input, sw);
return sw.toString();
}
/**
* Copy bytes from an InputStream
to chars on a
* Writer
using the default character encoding of the platform.
*
* This method buffers the input internally, so there is no need to use a
* BufferedInputStream
.
*
* This method uses {@link InputStreamReader}.
*
* @param input the InputStream
to read from
* @param output the Writer
to write to
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since Commons IO 1.1
*/
public static void copy(InputStream input, Writer output)
throws IOException {
InputStreamReader in = new InputStreamReader(input);
copy(in, output);
}
/**
* Copy bytes from an InputStream
to chars on a
* Writer
using the specified character encoding.
*
* This method buffers the input internally, so there is no need to use a
* BufferedInputStream
.
*
* Character encoding names can be found at
* IANA.
*
* This method uses {@link InputStreamReader}.
*
* @param input the InputStream
to read from
* @param output the Writer
to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since Commons IO 1.1
*/
public static void copy(InputStream input, Writer output, String encoding)
throws IOException {
if (encoding == null) {
copy(input, output);
} else {
InputStreamReader in = new InputStreamReader(input, encoding);
copy(in, output);
}
}
// copy from Reader
//-----------------------------------------------------------------------
/**
* Copy chars from a Reader
to a Writer
.
*
* This method buffers the input internally, so there is no need to use a
* BufferedReader
.
*
* Large streams (over 2GB) will return a chars copied value of
* -1
after the copy has completed since the correct
* number of chars cannot be returned as an int. For large streams
* use the copyLarge(Reader, Writer)
method.
*
* @param input the Reader
to read from
* @param output the Writer
to write to
* @return the number of characters copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @throws ArithmeticException if the character count is too large
* @since Commons IO 1.1
*/
public static int copy(Reader input, Writer output) throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
/**
* Copy chars from a large (over 2GB) Reader
to a Writer
.
*
* This method buffers the input internally, so there is no need to use a
* BufferedReader
.
*
* @param input the Reader
to read from
* @param output the Writer
to write to
* @return the number of characters copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since Commons IO 1.3
*/
public static long copyLarge(Reader input, Writer output) throws IOException {
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
/**
* Copy chars from a Reader
to bytes on an
* OutputStream
using the default character encoding of the
* platform, and calling flush.
*
* This method buffers the input internally, so there is no need to use a
* BufferedReader
.
*
* Due to the implementation of OutputStreamWriter, this method performs a
* flush.
*
* This method uses {@link OutputStreamWriter}.
*
* @param input the Reader
to read from
* @param output the OutputStream
to write to
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since Commons IO 1.1
*/
public static void copy(Reader input, OutputStream output)
throws IOException {
OutputStreamWriter out = new OutputStreamWriter(output);
copy(input, out);
// XXX Unless anyone is planning on rewriting OutputStreamWriter, we
// have to flush here.
out.flush();
}
/**
* Copy chars from a Reader
to bytes on an
* OutputStream
using the specified character encoding, and
* calling flush.
*
* This method buffers the input internally, so there is no need to use a
* BufferedReader
.
*
* Character encoding names can be found at
* IANA.
*
* Due to the implementation of OutputStreamWriter, this method performs a
* flush.
*
* This method uses {@link OutputStreamWriter}.
*
* @param input the Reader
to read from
* @param output the OutputStream
to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since Commons IO 1.1
*/
public static void copy(Reader input, OutputStream output, String encoding)
throws IOException {
if (encoding == null) {
copy(input, output);
} else {
OutputStreamWriter out = new OutputStreamWriter(output, encoding);
copy(input, out);
// XXX Unless anyone is planning on rewriting OutputStreamWriter,
// we have to flush here.
out.flush();
}
}
}
/**
* {@link Writer} implementation that outputs to a {@link StringBuilder}.
*
* NOTE: This implementation, as an alternative to
* java.io.StringWriter
, provides an un-synchronized
* (i.e. for use in a single thread) implementation for better performance.
* For safe usage with multiple {@link Thread}s then
* java.io.StringWriter
should be used.
*
* ***Copied & Pasted from apache commons-io because I dont want to add too many 3rd parties library to the app to keep it as small as possible***
*
* @version $Revision$ $Date$
* @since IO 2.0
*/
class StringBuilderWriter extends Writer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private final StringBuilder builder;
/**
* Construct a new {@link StringBuilder} instance with default capacity.
*/
public StringBuilderWriter() {
this.builder = new StringBuilder();
}
/**
* Construct a new {@link StringBuilder} instance with the specified capacity.
*
* @param capacity The initial capacity of the underlying {@link StringBuilder}
*/
public StringBuilderWriter(int capacity) {
this.builder = new StringBuilder(capacity);
}
/**
* Construct a new instance with the specified {@link StringBuilder}.
*
* @param builder The String builder
*/
public StringBuilderWriter(StringBuilder builder) {
this.builder = (builder != null ? builder : new StringBuilder());
}
/**
* Append a single character to this Writer.
*
* @param value The character to append
* @return This writer instance
*/
@Override
public Writer append(char value) {
builder.append(value);
return this;
}
/**
* Append a character sequence to this Writer.
*
* @param value The character to append
* @return This writer instance
*/
@Override
public Writer append(CharSequence value) {
builder.append(value);
return this;
}
/**
* Append a portion of a character sequence to the {@link StringBuilder}.
*
* @param value The character to append
* @param start The index of the first character
* @param end The index of the last character + 1
* @return This writer instance
*/
@Override
public Writer append(CharSequence value, int start, int end) {
builder.append(value, start, end);
return this;
}
/**
* Closing this writer has no effect.
*/
@Override
public void close() {
}
/**
* Flushing this writer has no effect.
*/
@Override
public void flush() {
}
/**
* Write a String to the {@link StringBuilder}.
*
* @param value The value to write
*/
@Override
public void write(String value) {
if (value != null) {
builder.append(value);
}
}
/**
* Write a portion of a character array to the {@link StringBuilder}.
*
* @param value The value to write
* @param offset The index of the first character
* @param length The number of characters to write
*/
@Override
public void write(char[] value, int offset, int length) {
if (value != null) {
builder.append(value, offset, length);
}
}
/**
* Return the underlying builder.
*
* @return The underlying builder
*/
public StringBuilder getBuilder() {
return builder;
}
/**
* Returns {@link StringBuilder#toString()}.
*
* @return The contents of the String builder.
*/
@Override
public String toString() {
return builder.toString();
}
}