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

voidcopyStream(InputStream in, OutputStream out, int len)
copy Stream
byte[] buffer = new byte[4096];
for (int i = 0; i < len;) {
    int toRead = Math.min(buffer.length, len - i);
    int r = in.read(buffer, 0, toRead);
    if (r < 0) {
        throw new EOFException();
    i += r;
...
voidcopyStream(InputStream in, OutputStream out, JarEntry entry)
copy Stream
byte[] buffer = new byte[1024 * 4];
long count = 0;
int n = 0;
long size = entry.getSize();
while (-1 != (n = in.read(buffer)) && count < size) {
    out.write(buffer, 0, n);
    count += n;
voidcopyStream(InputStream in, OutputStream out, long maxLen)
copy Stream
byte[] buf = new byte[COPY_BUF_SIZE];
int len;
if (maxLen <= 0)
    while ((len = in.read(buf)) > 0)
        out.write(buf, 0, len);
else
    while ((len = in.read(buf)) > 0)
        if (len <= maxLen) {
...
voidcopyStream(InputStream in, OutputStream out, String end)
copy Stream
if (!estDefinit(end))
    throw new Exception("End string can not be null or empty");
int i = -1, pos = 0;
while (-1 != (i = in.read())) {
    out.write(i);
    pos = i == end.charAt(pos) ? pos + 1 : 0;
    if (pos == end.length())
        break;
...
voidcopyStream(InputStream input, OutputStream output)
Copies data from the input stream to the output stream.
byte[] data = new byte[65536];
while (true) {
    int bytesRead = input.read(data);
    if (bytesRead < 0) {
        break;
    output.write(data, 0, bytesRead);
voidcopyStream(InputStream input, OutputStream output)
copy Stream
try {
    final byte data[] = new byte[8192];
    int count;
    while ((count = input.read(data, 0, 8192)) != -1)
        output.write(data, 0, count);
} catch (final IOException e) {
    throw new RuntimeException(e);
voidcopyStream(InputStream input, OutputStream output)
copy Stream
copyStream(input, output, new byte[1024]);
StringcopyStream(InputStream input, OutputStream output)
Writes the content of input into output
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
    output.write(buffer, 0, bytesRead);
    sb.append(new String(buffer, 0, bytesRead));
return sb.toString();
...
intcopyStream(InputStream input, OutputStream output)
Copies data from one stream to another.
int t = 0;
byte[] buffer = new byte[1024];
int n = input.read(buffer);
while (n >= 0) {
    output.write(buffer, 0, n);
    t += n;
    n = input.read(buffer);
return t;
voidcopyStream(InputStream input, OutputStream output)
Connects an InputStream to an OutputStream.
Author: Jon Skeet
http://stackoverflow.com/a/1574857
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
    output.write(buffer, 0, bytesRead);