Java InputStream to OutputStream copyStream(final OutputStream to, final InputStream from)

Here you can find the source of copyStream(final OutputStream to, final InputStream from)

Description

Copy one stream to another until the input stream has no more data.

License

Apache License

Parameter

Parameter Description
to The open stream to write to.
from The open stream to read from.

Declaration

public static void copyStream(final OutputStream to, final InputStream from) throws IOException 

Method Source Code

//package com.java2s;
/*/*from ww w .  ja va 2 s.  co  m*/
 * Copyright (C) 2015 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.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.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    private static final int BUF_SIZE = 1024;

    /**
     * Copy one stream to another until the input stream has no more data.
     * 
     * @param to
     *          The open stream to write to.
     * @param from
     *          The open stream to read from.
     */
    public static void copyStream(final OutputStream to, final InputStream from) throws IOException {
        final byte[] buf = new byte[BUF_SIZE];
        int len = from.read(buf);
        while (len > 0) {
            to.write(buf, 0, len);
            len = from.read(buf);
        }
    }
}

Related

  1. copyStream(final InputStream is, final OutputStream os)
  2. copyStream(final InputStream is, final OutputStream os)
  3. copyStream(final InputStream is, final OutputStream out, final Long amount, final int bufferSize)
  4. copyStream(final InputStream source, final OutputStream target)
  5. copyStream(final InputStream src, OutputStream dest)
  6. copyStream(InputStream fis, OutputStream fos)
  7. copyStream(InputStream from, OutputStream to)
  8. copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut)
  9. copyStream(InputStream in, File outputFile)