Java InputStream to OutputStream copyStream(InputStream in, OutputStream out, long maxLen)

Here you can find the source of copyStream(InputStream in, OutputStream out, long maxLen)

Description

copy Stream

License

Open Source License

Declaration

public static void copyStream(InputStream in, OutputStream out, long maxLen) throws IOException 

Method Source Code


//package com.java2s;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class Main {
    public static final int COPY_BUF_SIZE = 4096 * 2;

    public static void copyStream(InputStream in, OutputStream out, long maxLen) throws IOException {
        byte[] buf = new byte[COPY_BUF_SIZE];
        int len;/*from   w  w  w. j  a  v a2s.  com*/
        if (maxLen <= 0)
            while ((len = in.read(buf)) > 0)
                out.write(buf, 0, len);
        else
            while ((len = in.read(buf)) > 0)
                if (len <= maxLen) {
                    out.write(buf, 0, len);
                    maxLen -= len;
                } else {
                    out.write(buf, 0, (int) maxLen);
                    break;
                }
    }

    public static void copyStream(Reader in, Writer out) throws IOException {
        char[] buf = new char[COPY_BUF_SIZE];
        int len;
        while ((len = in.read(buf)) != -1)
            out.write(buf, 0, len);
    }

    public static void copyStream(Reader in, OutputStream out, String charSet) throws IOException {
        char[] buf = new char[4096];
        int len;
        if (charSet == null)
            while ((len = in.read(buf)) != -1) {
                out.write(new String(buf, 0, len).getBytes());
            }
        else
            while ((len = in.read(buf)) != -1)
                out.write(new String(buf, 0, len).getBytes(charSet));
    }
}

Related

  1. copyStream(InputStream in, OutputStream out, boolean closeOut)
  2. copyStream(InputStream in, OutputStream out, int bufferSize)
  3. copyStream(InputStream in, OutputStream out, int bufsize)
  4. copyStream(InputStream in, OutputStream out, int len)
  5. copyStream(InputStream in, OutputStream out, JarEntry entry)
  6. copyStream(InputStream in, OutputStream out, String end)
  7. copyStream(InputStream input, OutputStream output)
  8. copyStream(InputStream input, OutputStream output)
  9. copyStream(InputStream input, OutputStream output)