public static long copyReader()

in src/main/java/org/apache/commons/net/io/Util.java [125:161]


    public static long copyReader(final Reader source, final Writer dest, final int bufferSize, final long streamSize, final CopyStreamListener listener)
            throws CopyStreamException {
        int numChars;
        long total = 0;
        final char[] buffer = new char[bufferSize > 0 ? bufferSize : DEFAULT_COPY_BUFFER_SIZE];

        try {
            while ((numChars = source.read(buffer)) != NetConstants.EOS) {
                // Technically, some read(char[]) methods may return 0, and we cannot
                // accept that as an indication of EOF.
                if (numChars == 0) {
                    final int singleChar = source.read();
                    if (singleChar < 0) {
                        break;
                    }
                    dest.write(singleChar);
                    dest.flush();
                    ++total;
                    if (listener != null) {
                        listener.bytesTransferred(total, 1, streamSize);
                    }
                    continue;
                }

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

        return total;
    }