Here you can find the source of copyStreams(InputStream in, OutputStream out, int buf)
Parameter | Description |
---|---|
in | input stream |
out | output stream |
buf | buffer size (-1 means don't buffer) |
public static void copyStreams(InputStream in, OutputStream out, int buf) throws IOException
//package com.java2s; /**// ww w . j av a 2s .com * Appframework * Copyright (C) 2003-2016 SSHTOOLS Limited * * 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 2 * of the License, or (at your option) any later version. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Copy the input from one stream to the output of another until EOF. It * is up to the invoker to close the streams. * * @param in input stream * @param out output stream * @param buf buffer size (-1 means don't buffer) */ public static void copyStreams(InputStream in, OutputStream out, int buf) throws IOException { copyStreams(in, out, buf, -1); } /** * Copy the input from one stream to the output of another for a number of * bytes (-1 means until EOF) * * @param in input stream * @param out output stream * @param buf buffer size (-1 means don't buffer) * @param bytes bytes to copy */ public static void copyStreams(InputStream in, OutputStream out, int buf, long bytes) throws IOException { InputStream bin = buf == -1 ? in : new BufferedInputStream(in, buf); OutputStream bout = buf == -1 ? out : new BufferedOutputStream(out, buf); byte[] b = null; int r = 0; while (true && (bytes == -1 || (r >= bytes))) { int a = bin.available(); if (a == -1) break; else if (a == 0) a = 1; if (bytes != -1 && (r + a) > bytes) a -= (r + a - bytes); b = new byte[a]; a = bin.read(b); if (a == -1) break; r += a; bout.write(b, 0, a); } bout.flush(); } }