Java InputStream to OutputStream copyStream(InputStream is, OutputStream os, long maxLength)

Here you can find the source of copyStream(InputStream is, OutputStream os, long maxLength)

Description

copy Stream

License

Open Source License

Declaration

public static void copyStream(InputStream is, OutputStream os,
                long maxLength) 

Method Source Code

//package com.java2s;
/*//ww w  .j a va  2  s.  c  om
 *  Copyright (C) 2010-2015 JPEXS, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

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

import java.io.OutputStream;

public class Main {
    public static void copyStream(InputStream is, OutputStream os,
            long maxLength) {
        try {
            final int bufSize = 4096;
            byte[] buf = new byte[bufSize];
            int cnt = 0;
            while ((cnt = is.read(buf)) > 0) {
                os.write(buf, 0, cnt);
                maxLength -= cnt;

                // last chunk is smaller
                if (maxLength < bufSize) {
                    buf = new byte[(int) maxLength];
                }
            }
        } catch (IOException ex) {
            // ignore
        }
    }
}

Related

  1. copyStream(InputStream is, OutputStream os)
  2. copyStream(InputStream is, OutputStream os)
  3. copyStream(InputStream is, OutputStream os)
  4. copyStream(InputStream is, OutputStream os, boolean closeInput)
  5. copyStream(InputStream is, OutputStream os, int bufferSize)
  6. copyStream(InputStream source, OutputStream dest)
  7. copyStream(InputStream source, OutputStream dest)
  8. copyStream(InputStream source, OutputStream dest)
  9. copyStream(InputStream source, OutputStream destination, byte[] buffer)