Java Utililty Methods OutputStream Write

List of utility methods to do OutputStream Write

Description

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

Method

voidcopyBytes(InputStream in, OutputStream out, int buffSize, boolean close)
Copies from one stream to another.
try {
    copyBytes(in, out, buffSize);
    if (close) {
        out.close();
        out = null;
        in.close();
        in = null;
} finally {
    if (close) {
        closeStream(out);
        closeStream(in);
voidcopyBytes(InputStream input, int size, OutputStream output)
Copies a sequence of bytes from one stream to another.
if (size == 0) {
    return;
byte[] buffer = new byte[size];
readBytes(input, size, buffer);
output.write(buffer);
voidcopyBytes(InputStream inputStream, OutputStream outputStream, int size)
copy Bytes
int bufferLength = BUFFER_SIZE;
byte[] buf = new byte[bufferLength];
int bytesRead = 0;
while (bytesRead < size) {
    int requestedBunchSize = Math.min(size - bytesRead, bufferLength);
    int read = inputStream.read(buf, 0, requestedBunchSize);
    bytesRead += read;
    outputStream.write(buf, 0, read);
...
voidcopyBytes(InputStream is, DataOutputStream[] os, long numBytes)
copy Bytes
byte buf[] = new byte[4096];
while (numBytes > 0) {
    int numBytesToRead = 4096;
    if (numBytes < 4096)
        numBytesToRead = (int) numBytes;
    int bytesRead = is.read(buf, 0, numBytesToRead);
    for (int i = 0; i < os.length; i++) {
        os[i].write(buf, 0, bytesRead);
...
voidcopyBytes(InputStream is, OutputStream bytes)
copy Bytes
int res = is.read();
while (res != -1) {
    bytes.write(res);
    res = is.read();
longcopyBytes(InputStream iStream, OutputStream oStream)
Write all bytes available from Input- to OutputStream in pieces of DEFAULT_BUFFER_SIZE.
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;
...
voidcopyBytesAndClose(final InputStream is, final OutputStream os)
Calls #copyBytes(java.io.InputStream,java.io.OutputStream) and safely closes both streams.
try {
    copyBytes(is, os);
} finally {
    try {
        os.close();
    } finally {
        is.close();
FilecopyBytesToFile(byte[] bytes, File outputFile)
copy Bytes To File
InputStream inputStream = new ByteArrayInputStream(bytes);
OutputStream outputStream = new FileOutputStream(outputFile);
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = inputStream.read(buf)) > 0) {
    outputStream.write(buf, 0, len);
outputStream.close();
...
voidcopyBytesToStream(ByteArrayOutputStream from, OutputStream to)
copy Bytes To Stream
OutputStream out = new BufferedOutputStream(to);
try {
    from.writeTo(out);
} finally {
    out.flush();
voidcopyBytesToStream(InputStream inputStream, OutputStream outputStream, int length)
Copies given length amount of bytes from given inputStream to given outputStream.
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int totalRead = 0;
do {
    int readReqLen = (length == -1) ? BUFFER_SIZE : Math.min(BUFFER_SIZE, length - totalRead);
    int read = inputStream.read(buffer, 0, readReqLen);
    if (read == -1) {
        if (length != -1) {
...