Skip to content

fix(io): copy content larger than 2GB and keep what a file stream already holds - #25

Merged
marevol merged 1 commit into
mainfrom
fix/copyutil-large-files
Nov 29, 2026
Merged

marevol merged 1 commit into
mainfrom
fix/copyutil-large-files

Conversation

@marevol

@marevol marevol commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Changes

CopyUtil tracked the copy position in an int and used it as an absolute file offset.

  • copyInternal(InputStream, FileOutputStream) (reached by copy(InputStream, File) and by copy(InputStream, OutputStream) with a FileOutputStream) wrote each chunk with FileChannel.write(buffer, amount).
    • Past 2GB the offset overflowed and the copy failed with IllegalArgumentException: Negative position.
    • The offset started at 0, so it also overwrote anything the caller had already written to the stream (e.g. a header written before calling copy).
    • It now writes at the stream's current position by delegating to the plain stream loop.
  • copyInternal(FileInputStream, OutputStream) (reached by copy(File, 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; the returned int is capped at Integer.MAX_VALUE instead of wrapping. Public signatures are unchanged.

Verification

  • mvn test: 708 tests pass. Added testIsToFileOs_KeepsWrittenContent, which fails on main (the pre-written bytes are overwritten) and passes with this change.
  • A one-off harness (not committed, it needs ~2GB of disk) copied 2GB + 12,305 bytes through copy(InputStream, File) and copy(File, OutputStream):
    • main: both fail with IllegalArgumentException: Negative position.
    • this branch: both copy every byte and return Integer.MAX_VALUE.

…eady 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.
@marevol marevol self-assigned this Nov 29, 2026
@marevol marevol added this to the 0.3.0 milestone Nov 29, 2026
@marevol
marevol merged commit 3400568 into main Nov 29, 2026
1 check passed
marevol added a commit to codelibs/fess-crawler that referenced this pull request Nov 29, 2026
…260)

* test(s3): cover the response stream cleanup of a temp-file download

Bring back the regression tests for #259, which were dropped from that
pull request before it was merged: a successful download closes the S3
response stream, and a failed one aborts it before closing it. Both fail
without the #259 change.

If abort() itself throws, keep the copy failure as the thrown exception
and attach the abort failure as suppressed, so the warning names the
cause of the failed download.

* fix: write stream content to files with transferTo instead of CopyUtil

CopyUtil.copy(InputStream, File) and CopyUtil.copy(InputStream,
FileOutputStream) write each chunk at an absolute offset held in an int.
Past 2GB the offset overflows and the copy fails with
"IllegalArgumentException: Negative position". The fix is in corelib
(codelibs/corelib#25), but it reaches fess-crawler only with a new
corelib release, so copy with InputStream.transferTo() here:

- GcsClient: the download of an object above the cache threshold
- PdfExtractor, JodExtractor: the spooled input file
- TikaExtractor: the materialized input file
- FileTransformer: the stored response body
- ResponseDataUtil.createResponseBodyFile()

The exceptions callers see are unchanged. JodExtractor, PdfExtractor and
FileTransformer wrap the IOException in IORuntimeException as CopyUtil
did: JodExtractor and FileTransformer let it escape, and in PdfExtractor
it must not reach the "Failed to load PDF." handler for IOException. The
other sites already catch any exception.

* fix: keep CopyUtil where its source is a raw FileInputStream, and test the GCS temp-file download

FileTransformer and ResponseDataUtil copy ResponseData.getResponseBody(),
which is a raw FileInputStream for a file-backed body. CopyUtil sends a
FileInputStream to a FileOutputStream through a long channel transfer,
and an in-memory body is bounded by maxCachedContentSize, so neither
site reaches the int offset that fails past 2GB. Put them back as they
were.

GcsClientTest gains a case that downloads a 3MB object above the cache
threshold. Every test object was 5 bytes before, so the temp-file branch
changed in the previous commit was never exercised.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant