Here you can find the source of copyStreams(final InputStream input, final OutputStream output)
Parameter | Description |
---|---|
input | The input stream to read |
output | The output stream to write |
Parameter | Description |
---|---|
IOException | If anything fails. |
public static int copyStreams(final InputStream input, final OutputStream output) throws IOException
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**/* w ww.j ava2s . co m*/ * The amount of memory that we will reserve to read files. */ protected static final int BUFFER_SEGMENT_LENGTH = 4096; /** * This method copies the contents of an input stream to an output stream. The streams are not closed after the * method completes. * * @param input * The input stream to read * @param output * The output stream to write * @return The total number of bytes transfered between the streams * @throws IOException * If anything fails. */ public static int copyStreams(final InputStream input, final OutputStream output) throws IOException { int amtRead = 0; int totalLength = 0; byte[] segment = new byte[BUFFER_SEGMENT_LENGTH]; amtRead = input.read(segment, 0, segment.length); while (amtRead != -1) { totalLength += amtRead; output.write(segment, 0, amtRead); amtRead = input.read(segment, 0, segment.length); } return totalLength; } }