public static long copyStream()

in src/main/java/org/apache/commons/net/io/Util.java [243:284]


    public static long copyStream(final InputStream source, final OutputStream dest, final int bufferSize, final long streamSize,
            final CopyStreamListener listener, final boolean flush) throws CopyStreamException {
        int numBytes;
        long total = 0;
        final byte[] buffer = new byte[bufferSize > 0 ? bufferSize : DEFAULT_COPY_BUFFER_SIZE];

        try {
            while ((numBytes = source.read(buffer)) != NetConstants.EOS) {
                // Technically, some read(byte[]) methods may return 0, and we cannot
                // accept that as an indication of EOF.

                if (numBytes == 0) {
                    final int singleByte = source.read();
                    if (singleByte < 0) {
                        break;
                    }
                    dest.write(singleByte);
                    if (flush) {
                        dest.flush();
                    }
                    ++total;
                    if (listener != null) {
                        listener.bytesTransferred(total, 1, streamSize);
                    }
                    continue;
                }

                dest.write(buffer, 0, numBytes);
                if (flush) {
                    dest.flush();
                }
                total += numBytes;
                if (listener != null) {
                    listener.bytesTransferred(total, numBytes, streamSize);
                }
            }
        } catch (final IOException e) {
            throw new CopyStreamException("IOException caught while copying.", total, e);
        }

        return total;
    }