Repository navigation
fix(io): copy content larger than 2GB and keep what a file stream already holds - #25
Merged
Merged
Conversation
…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
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.
2 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
CopyUtiltracked the copy position in anintand used it as an absolute file offset.copyInternal(InputStream, FileOutputStream)(reached bycopy(InputStream, File)and bycopy(InputStream, OutputStream)with aFileOutputStream) wrote each chunk withFileChannel.write(buffer, amount).IllegalArgumentException: Negative position.copy).copyInternal(FileInputStream, OutputStream)(reached bycopy(File, OutputStream)) read with a positionalintoffset and failed the same way on a file larger than 2GB. The offset is now along.long; the returnedintis capped atInteger.MAX_VALUEinstead of wrapping. Public signatures are unchanged.Verification
mvn test: 708 tests pass. AddedtestIsToFileOs_KeepsWrittenContent, which fails onmain(the pre-written bytes are overwritten) and passes with this change.copy(InputStream, File)andcopy(File, OutputStream):main: both fail withIllegalArgumentException: Negative position.Integer.MAX_VALUE.