Here you can find the source of copyBytes(InputStream iStream, OutputStream oStream)
Parameter | Description |
---|---|
iStream | a parameter |
oStream | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public final static long copyBytes(InputStream iStream, OutputStream oStream) throws IOException
//package com.java2s; /**//from w w w. j a v a 2s .co m * Copyright 2010 the original author or authors. * * 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 { /** * A default buffer size, could be used f.e. by copying bytes from stream to stream. */ public static final int DEFAULT_BUFFER_SIZE = 4 * 1024; /** * Write all bytes available from Input- to OutputStream in pieces of DEFAULT_BUFFER_SIZE. <br/> * Block until it reads -1. * * * @param iStream * @param oStream * @return total bytes copied * @throws IOException */ public final static long copyBytes(InputStream iStream, OutputStream oStream) throws IOException { long totalLength = 0; int length = -1; byte[] bytes = new byte[DEFAULT_BUFFER_SIZE]; while ((length = iStream.read(bytes)) != -1) { oStream.write(bytes, 0, length); totalLength += length; } return totalLength; } /** * Write length bytes available from Input- to OutputStream in pieces of DEFAULT_BUFFER_SIZE. * * @param iStream * @param oStream * @param length * @throws IOException */ public static void copyBytes(InputStream iStream, OutputStream oStream, long length) throws IOException { byte[] bytes = new byte[DEFAULT_BUFFER_SIZE]; int pieceLength = bytes.length; while (length > 0) { if (length < bytes.length) { pieceLength = iStream.read(bytes, 0, (int) length); } else { pieceLength = iStream.read(bytes, 0, bytes.length); } oStream.write(bytes, 0, pieceLength); length -= pieceLength; } } }