Android InputStream Copy copyContents(InputStream in, OutputStream out)

Here you can find the source of copyContents(InputStream in, OutputStream out)

Description

Just copies all bytes from in to out.

License

Open Source License

Parameter

Parameter Description
in The inputstream to copy from
out The outputstream to copy to

Exception

Parameter Description
IOException In case reading or writing fails.

Declaration

public static void copyContents(InputStream in, OutputStream out)
        throws IOException 

Method Source Code

/*/* w ww .j a v  a  2  s . co  m*/
  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();
    }
}

Related

  1. copy(InputStream input, Writer output)
  2. copy(InputStream input, Writer output, String encoding)
  3. copy(InputStream inputStream, OutputStream outputStream)
  4. copy(InputStream pSourceStream, OutputStream pTargetStream)
  5. copy(InputStream pSourceStream, OutputStream pTargetStream, boolean closeSource)
  6. copyFile(InputStream from, String target)
  7. copyFile(InputStream in, File out)
  8. copyFile(InputStream in, OutputStream out)
  9. copyFile(InputStream input, OutputStream output)