Exclude byte-order mark from ExtendedBufferedReader byte counting#622
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes byte-position tracking when parsing CSV data with trackBytes=true and a BOM-emitting charset encoder (notably UTF-16), preventing byte offsets from being inflated by a per-encode-call BOM.
Changes:
- Compute and cache the per-call BOM length emitted by
CharsetEncoder.encode(...)and subtract it from per-character encoded byte counts. - Add a regression test ensuring
CSVRecord.getBytePosition()advances at the correct rate forUTF-16when byte tracking is enabled.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java |
Measures per-call BOM emission once and subtracts it from byte-count calculations to keep bytesRead accurate for BOM-emitting encoders. |
src/test/java/org/apache/commons/csv/CSVParserTest.java |
Adds coverage for correct byte-position advancement under UTF-16 with byte tracking enabled. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
Thank you @rootvector2 , merged 🚀 |
garydgregory
added a commit
that referenced
this pull request
Jul 13, 2026
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.
getEncodedCharLengthre-encodes each character with the configuredCharsetEncoderto count its bytes, but the convenienceencodecall re-emits a byte-order mark on every invocation for charsets likeUTF-16. Parsing a UTF-16 stream withsetTrackBytes(true), for example an Excel Unicode export, counts every code unit as 4 bytes instead of 2, soCSVRecord.getBytePosition()grows at twice the true rate. Found by comparing reported byte offsets against the encoded prefix length across charsets.Measure the per-call BOM once and subtract it from each encoded length. The count already lives in
getEncodedCharLength, so keeping the adjustment there covers both the single-char and array read paths, and it stays a no-op for charsets that emit no BOM (UTF-8,UTF-16LE/BE, single-byte).