Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions src/main/java/org/codelibs/core/io/CopyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
* <p>
* Any {@link IOException} thrown by these methods is wrapped and thrown as an {@link IORuntimeException}.
* </p>
* <p>
* The returned count is capped at {@link Integer#MAX_VALUE}: content larger than that is still copied in full.
* </p>
*
* @author koichik
*/
Expand Down Expand Up @@ -1158,13 +1161,13 @@ protected static int copyInternal(final InputStream in, final OutputStream out)
try {
final byte[] buf = new byte[DEFAULT_BUF_SIZE];
int len;
int amount = 0;
long amount = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
amount += len;
}
out.flush();
return amount;
return toCount(amount);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
Expand All @@ -1186,14 +1189,14 @@ protected static int copyInternal(final FileInputStream in, final OutputStream o
final ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_BUF_SIZE);
final byte[] buf = buffer.array();
int len;
int amount = 0;
long amount = 0;
while ((len = ChannelUtil.read(channel, buffer, amount)) != -1) {
out.write(buf, 0, len);
buffer.clear();
amount += len;
}
out.flush();
return amount;
return toCount(amount);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
Expand All @@ -1210,23 +1213,8 @@ protected static int copyInternal(final FileInputStream in, final OutputStream o
* @return the number of bytes copied
*/
protected static int copyInternal(final InputStream in, final FileOutputStream out) {
try {
final FileChannel channel = out.getChannel();
final ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_BUF_SIZE);
final byte[] buf = buffer.array();
int len;
int amount = 0;
while ((len = in.read(buf)) != -1) {
buffer.limit(len);
channel.write(buffer, amount);
buffer.clear();
amount += len;
}
out.flush();
return amount;
} catch (final IOException e) {
throw new IORuntimeException(e);
}
// Write at the stream's current position so that content already written to it is kept.
return copyInternal(in, (OutputStream) out);
}

/**
Expand All @@ -1242,7 +1230,17 @@ protected static int copyInternal(final InputStream in, final FileOutputStream o
protected static int copyInternal(final FileInputStream in, final FileOutputStream out) {
final FileChannel ic = in.getChannel();
final FileChannel oc = out.getChannel();
return (int) ChannelUtil.transfer(ic, oc);
return toCount(ChannelUtil.transfer(ic, oc));
}

/**
* Converts the number of copied bytes or characters to the count returned by the copy methods.
*
* @param amount the number of bytes or characters copied
* @return {@code amount}, or {@link Integer#MAX_VALUE} if it does not fit in an {@code int}
*/
private static int toCount(final long amount) {
return (int) Math.min(amount, Integer.MAX_VALUE);
}

/**
Expand All @@ -1259,13 +1257,13 @@ protected static int copyInternal(final Reader in, final Writer out) {
try {
final char[] buf = new char[DEFAULT_BUF_SIZE];
int len;
int amount = 0;
long amount = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
amount += len;
}
out.flush();
return amount;
return toCount(amount);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
Expand All @@ -1282,12 +1280,12 @@ protected static int copyInternal(final Reader in, final StringBuilder out) {
try {
final char[] buf = new char[DEFAULT_BUF_SIZE];
int len;
int amount = 0;
long amount = 0;
while ((len = in.read(buf)) != -1) {
out.append(buf, 0, len);
amount += len;
}
return amount;
return toCount(amount);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/codelibs/core/io/CopyUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,28 @@ public void testFileToFile_LargeContentIsComplete() throws Exception {
}
}

/**
* Copying into a file output stream appends after what the caller has already written to it.
*
* @throws Exception
*/
@Test
public void testIsToFileOs_KeepsWrittenContent() throws Exception {
final File dest = File.createTempFile("copyutil-dest", ".dat");
try {
try (FileOutputStream fos = new FileOutputStream(dest)) {
fos.write(new byte[] { 'H', 'D' });
final int result = copy(is, fos);
assertThat(result, is(srcBytes.length));
}
final byte[] expected = new byte[srcBytes.length + 2];
expected[0] = 'H';
expected[1] = 'D';
System.arraycopy(srcBytes, 0, expected, 2, srcBytes.length);
assertThat(FileUtil.readBytes(dest), is(expected));
} finally {
dest.delete();
}
}

}
Loading