Java Utililty Methods InputStream to OutputStream

List of utility methods to do InputStream to OutputStream

Description

The list of methods to do InputStream to OutputStream are organized into topic(s).

Method

voidcopyStreamAndClose(InputStream is, OutputStream os)
Copy input stream to output stream and close them both
try {
    copyStream(is, os, DEFAULT_BUFFER_SIZE);
    is.close();
    os.close();
} finally {
    safeClose(is);
    safeClose(os);
voidcopyStreamBounded(InputStream in, OutputStream out, long length)
copy Stream Bounded
final byte[] bytes = new byte[8192];
long totalCount = 0;
while (totalCount < length) {
    int len = (int) Math.min(bytes.length, length - totalCount);
    int count = in.read(bytes, 0, len);
    if (count == -1) {
        throw new EOFException("Reached end of stream prematurely.");
    out.write(bytes, 0, count);
    totalCount += count;
intcopyStreamContent(InputStream inputStream, OutputStream outputStream)
copy Stream Content
byte[] buffer = new byte[10240];
int total = 0;
int count;
while ((count = inputStream.read(buffer)) > 0) {
    outputStream.write(buffer, 0, count);
    total += count;
return total;
...
intcopyStreamContent(InputStream inputStream, OutputStream outputStream)
Copy stream.
final byte[] buffer = new byte[10 * 1024];
int count;
int total = 0;
while ((count = inputStream.read(buffer)) > 0) {
    outputStream.write(buffer, 0, count);
    total += count;
return total;
...
voidcopyStreamFully(InputStream in, OutputStream out, int length)
copy Stream Fully
byte[] bytes = new byte[4096];
while (length > 0) {
    int newBytes = in.read(bytes, 0, Math.min(bytes.length, length));
    if (newBytes < 0) {
        throw new EOFException();
    out.write(bytes, 0, newBytes);
    length -= newBytes;
...
voidcopyStreamIoe(final InputStream is, final OutputStream os)

Copies the data from the given input stream into the given output stream and doesn't wrap any IOException .

int offset;
final byte[] buffer = new byte[WUIC_BUFFER_LEN];
while ((offset = is.read(buffer)) != -1) {
    os.write(buffer, 0, offset);
voidcopyStreamNoClose(InputStream in, OutputStream out)
copy Stream No Close
final byte[] bytes = new byte[8192];
int cnt;
while ((cnt = in.read(bytes)) != -1) {
    out.write(bytes, 0, cnt);
out.flush();
voidcopyStreamPortion(java.io.InputStream inputStream, java.io.OutputStream outputStream, int portionSize, int bufferSize)
Read entire input stream portion and writes it data to output stream
if (bufferSize > portionSize) {
    bufferSize = portionSize;
byte[] writeBuffer = new byte[bufferSize];
int totalRead = 0;
while (totalRead < portionSize) {
    int bytesToRead = bufferSize;
    if (bytesToRead > portionSize - totalRead) {
...
voidcopyStreams(final InputStream in, final OutputStream out)
Copies all bytes from inputstream to outputstream.
copyStreams(in, out, new byte[16384]);
intcopyStreams(final InputStream input, final OutputStream output)
This method copies the contents of an input stream to an output stream.
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);
...