Mega Code Archive

 
Categories / Android / File
 

File Copy Util

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 <code>InputStream</code> as a String      * using the default character encoding of the platform.      * <p>      * This method buffers the input internally, so there is no need to use a      * <code>BufferedInputStream</code>.      *       * @param input  the <code>InputStream</code> 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 <code>InputStream</code> to chars on a      * <code>Writer</code> using the default character encoding of the platform.      * <p>      * This method buffers the input internally, so there is no need to use a      * <code>BufferedInputStream</code>.      * <p>      * This method uses {@link InputStreamReader}.      *      * @param input  the <code>InputStream</code> to read from      * @param output  the <code>Writer</code> 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 <code>InputStream</code> to chars on a      * <code>Writer</code> using the specified character encoding.      * <p>      * This method buffers the input internally, so there is no need to use a      * <code>BufferedInputStream</code>.      * <p>      * Character encoding names can be found at      * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.      * <p>      * This method uses {@link InputStreamReader}.      *      * @param input  the <code>InputStream</code> to read from      * @param output  the <code>Writer</code> 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 <code>Reader</code> to a <code>Writer</code>.      * <p>      * This method buffers the input internally, so there is no need to use a      * <code>BufferedReader</code>.      * <p>      * Large streams (over 2GB) will return a chars copied value of      * <code>-1</code> after the copy has completed since the correct      * number of chars cannot be returned as an int. For large streams      * use the <code>copyLarge(Reader, Writer)</code> method.      *      * @param input  the <code>Reader</code> to read from      * @param output  the <code>Writer</code> 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) <code>Reader</code> to a <code>Writer</code>.      * <p>      * This method buffers the input internally, so there is no need to use a      * <code>BufferedReader</code>.      *      * @param input  the <code>Reader</code> to read from      * @param output  the <code>Writer</code> 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 <code>Reader</code> to bytes on an      * <code>OutputStream</code> using the default character encoding of the      * platform, and calling flush.      * <p>      * This method buffers the input internally, so there is no need to use a      * <code>BufferedReader</code>.      * <p>      * Due to the implementation of OutputStreamWriter, this method performs a      * flush.      * <p>      * This method uses {@link OutputStreamWriter}.      *      * @param input  the <code>Reader</code> to read from      * @param output  the <code>OutputStream</code> 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 <code>Reader</code> to bytes on an      * <code>OutputStream</code> using the specified character encoding, and      * calling flush.      * <p>      * This method buffers the input internally, so there is no need to use a      * <code>BufferedReader</code>.      * <p>      * Character encoding names can be found at      * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.      * <p>      * Due to the implementation of OutputStreamWriter, this method performs a      * flush.      * <p>      * This method uses {@link OutputStreamWriter}.      *      * @param input  the <code>Reader</code> to read from      * @param output  the <code>OutputStream</code> 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}.  * <p>  * <strong>NOTE:</strong> This implementation, as an alternative to  * <code>java.io.StringWriter</code>, provides an <i>un-synchronized</i>  * (i.e. for use in a single thread) implementation for better performance.  * For safe usage with multiple {@link Thread}s then  * <code>java.io.StringWriter</code> 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();     } }