Java OutputStream Write copyBytesAndClose(final InputStream is, final OutputStream os)

Here you can find the source of copyBytesAndClose(final InputStream is, final OutputStream os)

Description

Calls #copyBytes(java.io.InputStream,java.io.OutputStream) and safely closes both streams.

License

Open Source License

Parameter

Parameter Description
is the input stream
os the output stream

Exception

Parameter Description
IOException if an I/O error occurs

Declaration

public static void copyBytesAndClose(final InputStream is, final OutputStream os) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w  w w.  ja  v a2s.  c  o m*/
 * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see http://www.gnu.org/licenses/
 */

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

public class Main {
    /**
     * Calls {@link #copyBytes(java.io.InputStream, java.io.OutputStream)} and safely closes both streams.
     * @param is the input stream
     * @param os the output stream
     * @throws IOException if an I/O error occurs
     */
    public static void copyBytesAndClose(final InputStream is, final OutputStream os) throws IOException {
        try {
            copyBytes(is, os);
        } finally {
            try {
                os.close();
            } finally {
                is.close();
            }
        }
    }

    /**
     * Reads bytes from the given input stream and writes them to the given output stream until
     * the end of the input stream is reached.
     *
     * @param is the input stream
     * @param os the output stream
     * @throws IOException if an I/O error occurs
     */
    public static void copyBytes(final InputStream is, final OutputStream os) throws IOException {
        while (true) {
            final int b = is.read();
            if (b == -1) {
                break;
            }
            os.write(b);
        }
    }
}

Related

  1. copyBytes(InputStream input, int size, OutputStream output)
  2. copyBytes(InputStream inputStream, OutputStream outputStream, int size)
  3. copyBytes(InputStream is, DataOutputStream[] os, long numBytes)
  4. copyBytes(InputStream is, OutputStream bytes)
  5. copyBytes(InputStream iStream, OutputStream oStream)
  6. copyBytesToFile(byte[] bytes, File outputFile)
  7. copyBytesToStream(ByteArrayOutputStream from, OutputStream to)
  8. copyBytesToStream(InputStream inputStream, OutputStream outputStream, int length)
  9. copyFile(File fromFile, File toFile)