Skip to content

Commit 3373d77

Browse files
committed
Reimplement Util.copyReader() with IOUtils.copyLarge()
1 parent 3be2077 commit 3373d77

2 files changed

Lines changed: 13 additions & 31 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ The <action> type attribute can be add,update,fix,remove.
7575
<action type="fix" dev="ggregory" due-to="Gary Gregory">ListenerList.removeListener(T) now ignores null input to avoid a NullPointerException.</action>
7676
<action type="fix" dev="ggregory" due-to="Gary Gregory">ListenerList.addListener(T) now ignores null input.</action>
7777
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix typo in FTPConnectionClosedException message from FTP.getReply(boolean).</action>
78+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement Util.copyReader() with IOUtils.copyLarge().</action>
79+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement Util.copyStream() with IOUtils.copyLarge().</action>
7880
<!-- ADD -->
7981
<action type="add" dev="ggregory" due-to="Gary Gregory">Add DatagramSocketClient.getDefaultTimeoutDuration() and deprecate getDefaultTimeout().</action>
8082
<action type="add" dev="ggregory" due-to="Maros Orsak, Gary Gregory" issue="NET-741">Add subnet IPv6 handling with SubnetUtils6 #391.</action>

src/main/java/org/apache/commons/net/io/Util.java

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import org.apache.commons.io.IOUtils;
3434
import org.apache.commons.io.output.ProxyOutputStream;
35-
import org.apache.commons.net.util.NetConstants;
35+
import org.apache.commons.io.output.ProxyWriter;
3636

3737
/**
3838
* The Util class cannot be instantiated and stores short static convenience methods that are often quite useful.
@@ -110,6 +110,7 @@ public static long copyReader(final Reader source, final Writer dest, final int
110110
* <p>
111111
* The contents of the Reader are read until its end is reached, but neither the source nor the destination are closed. You must do this yourself outside
112112
* the method call. The number of characters read/written is returned.
113+
* </p>
113114
*
114115
* @param source The source Reader.
115116
* @param dest The destination writer.
@@ -125,40 +126,19 @@ public static long copyReader(final Reader source, final Writer dest, final int
125126
*/
126127
public static long copyReader(final Reader source, final Writer dest, final int bufferSize, final long streamSize, final CopyStreamListener listener)
127128
throws CopyStreamException {
128-
int numChars;
129-
long total = 0;
130-
final char[] buffer = new char[bufferSize > 0 ? bufferSize : DEFAULT_COPY_BUFFER_SIZE];
131-
129+
final AtomicLong total = new AtomicLong();
132130
try {
133-
while ((numChars = source.read(buffer)) != NetConstants.EOS) {
134-
// Technically, some read(char[]) methods may return 0, and we cannot
135-
// accept that as an indication of EOF.
136-
if (numChars == 0) {
137-
final int singleChar = source.read();
138-
if (singleChar < 0) {
139-
break;
140-
}
141-
dest.write(singleChar);
142-
dest.flush();
143-
++total;
144-
if (listener != null) {
145-
listener.bytesTransferred(total, 1, streamSize);
146-
}
147-
continue;
148-
}
131+
return IOUtils.copyLarge(source, listener == null ? dest : new ProxyWriter(dest) {
149132

150-
dest.write(buffer, 0, numChars);
151-
dest.flush();
152-
total += numChars;
153-
if (listener != null) {
154-
listener.bytesTransferred(total, numChars, streamSize);
133+
@Override
134+
protected void afterWrite(int n) throws IOException {
135+
dest.flush();
136+
listener.bytesTransferred(total.addAndGet(n), n, streamSize);
155137
}
156-
}
157-
} catch (final IOException e) {
158-
throw new CopyStreamException("IOException caught while copying.", total, e);
138+
}, new char[bufferSize > 0 ? bufferSize : DEFAULT_COPY_BUFFER_SIZE]);
139+
} catch (IOException e) {
140+
throw new CopyStreamException("IOException caught while copying.", total.get(), e);
159141
}
160-
161-
return total;
162142
}
163143

164144
/**

0 commit comments

Comments
 (0)