From 4a02efcf59b60498ecd52b831db38da6a6dfbaa0 Mon Sep 17 00:00:00 2001 From: Shinsuke Sugaya Date: Sun, 29 Nov 2026 19:33:55 +0900 Subject: [PATCH] fix(io): copy content larger than 2GB and keep what a file stream already holds CopyUtil tracked the copy position in an int and used it as an absolute file offset: - copyInternal(InputStream, FileOutputStream) wrote each chunk with FileChannel.write(buffer, amount). Past 2GB the offset overflowed and the copy failed with "IllegalArgumentException: Negative position". Because the offset started at 0, it also overwrote anything the caller had already written to the stream. It now writes at the stream's current position, like the other copy loops. - copyInternal(FileInputStream, OutputStream) read with a positional int offset and failed the same way on a file larger than 2GB. The offset is now a long. The copy loops count in a long, and the int they return is capped at Integer.MAX_VALUE instead of wrapping around to a negative or wrong value. The public signatures are unchanged. --- .../java/org/codelibs/core/io/CopyUtil.java | 50 +++++++++---------- .../org/codelibs/core/io/CopyUtilTest.java | 24 +++++++++ 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/codelibs/core/io/CopyUtil.java b/src/main/java/org/codelibs/core/io/CopyUtil.java index 7362514..dacfebb 100644 --- a/src/main/java/org/codelibs/core/io/CopyUtil.java +++ b/src/main/java/org/codelibs/core/io/CopyUtil.java @@ -111,6 +111,9 @@ *

* Any {@link IOException} thrown by these methods is wrapped and thrown as an {@link IORuntimeException}. *

+ *

+ * The returned count is capped at {@link Integer#MAX_VALUE}: content larger than that is still copied in full. + *

* * @author koichik */ @@ -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); } @@ -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); } @@ -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); } /** @@ -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); } /** @@ -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); } @@ -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); } diff --git a/src/test/java/org/codelibs/core/io/CopyUtilTest.java b/src/test/java/org/codelibs/core/io/CopyUtilTest.java index ce42870..ef4d98a 100644 --- a/src/test/java/org/codelibs/core/io/CopyUtilTest.java +++ b/src/test/java/org/codelibs/core/io/CopyUtilTest.java @@ -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(); + } + } + }