Java InputStream to OutputStream copyStream(InputStream src, OutputStream osstream)

Here you can find the source of copyStream(InputStream src, OutputStream osstream)

Description

copy Stream

License

Apache License

Declaration

public static boolean copyStream(InputStream src, OutputStream osstream) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2005-2012 Alfresco Software Limited.
 * /*  w w w .j  a v  a2  s .  c  o  m*/
 * This file is part of the Alfresco Mobile SDK.
 * 
 * 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.*;

public class Main {
    public static final int MAX_BUFFER_SIZE = 1024;

    public static boolean copyStream(InputStream src, OutputStream osstream) throws IOException {
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        boolean copied = true;

        try {

            bos = new BufferedOutputStream(osstream);
            bis = new BufferedInputStream(src);

            byte[] buffer = new byte[MAX_BUFFER_SIZE];

            int count;
            while ((count = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, count);
            }
            bos.flush();
        } catch (IOException e) {
            copied = false;
            throw e;
        } finally {
            closeStream(osstream);
            closeStream(src);
            closeStream(bis);
        }
        return copied;
    }

    public static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}

Related

  1. copyStream(InputStream sourceStream, OutputStream destinationStream)
  2. copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)
  3. copyStream(InputStream src, OutputStream dest)
  4. copyStream(InputStream src, OutputStream dest)
  5. copyStream(InputStream src, OutputStream dest, boolean closeStreams, String title)
  6. CopyStream(InputStream sSource, OutputStream sTarget)
  7. copyStream(int bufferSize, InputStream in, OutputStream out, boolean canStop)
  8. copyStream(java.io.InputStream in, java.io.OutputStream out)
  9. copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)