Android Reader Copy copyContents(Reader in, Writer out)

Here you can find the source of copyContents(Reader in, Writer out)

Description

Just copies all characters from in to out.

License

Open Source License

Parameter

Parameter Description
in The reader to copy from
out The reader to copy to

Exception

Parameter Description
IOException If reading or writing failed.

Declaration

public static void copyContents(Reader in, Writer out)
        throws IOException 

Method Source Code

/*/*w w w.  j ava2  s . c  om*/
  Copyright (c) Inexas 2010

  Modifications licensed under the Inexas Software License V1.0. You
  may not use this file except in compliance with the License.

  The License is available at: http://www.inexas.com/ISL-V1.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  The original file and contents are licensed under a separate license:
  see below.
 */

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import org.apache.log4j.Logger;

public class Main{
    /** Size of the buffer used when copying large chunks of data. */
    private static final int BUFFER_SIZE = 4096;
    /**
     * Just copies all characters from <I>in</I> to <I>out</I>. The copying is
     * performed using a buffer of bytes.
     * 
     * @since 1.5.8
     * @param in
     *            The reader to copy from
     * @param out
     *            The reader to copy to
     * @throws IOException
     *             If reading or writing failed.
     */
    public static void copyContents(Reader in, Writer out)
            throws IOException {
        char[] buf = new char[BUFFER_SIZE];
        int bytesRead = 0;

        while ((bytesRead = in.read(buf)) > 0) {
            out.write(buf, 0, bytesRead);
        }

        out.flush();
    }
    /**
     * Just copies all bytes from <I>in</I> to <I>out</I>. The copying is
     * performed using a buffer of bytes.
     * 
     * @since 1.9.31
     * @param in
     *            The inputstream to copy from
     * @param out
     *            The outputstream to copy to
     * @throws IOException
     *             In case reading or writing fails.
     */
    public static void copyContents(InputStream in, OutputStream out)
            throws IOException {
        byte[] buf = new byte[BUFFER_SIZE];
        int bytesRead = 0;

        while ((bytesRead = in.read(buf)) > 0) {
            out.write(buf, 0, bytesRead);
        }

        out.flush();
    }
}