Skip to content

feat: ranged byte[] compress/decompress overloads#58

Closed
dfa1 wants to merge 1 commit into
mainfrom
feat/ranged-byte-overloads
Closed

feat: ranged byte[] compress/decompress overloads#58
dfa1 wants to merge 1 commit into
mainfrom
feat/ranged-byte-overloads

Conversation

@dfa1

@dfa1 dfa1 commented Jun 28, 2026

Copy link
Copy Markdown
Owner

What

Adds (offset, length) sub-range overloads to io.github.dfa1.zstd.Zstd so callers holding a payload inside a larger buffer can compress/decompress it without copying the sub-range out first:

public static byte[] compress(byte[] src, int offset, int length);
public static byte[] compress(byte[] src, int offset, int length, int level);
public static byte[] decompress(byte[] compressed, int offset, int length, int maxSize);

How

  • Range is validated with Objects.checkFromIndexSize(offset, length, src.length) (after a null-check) before any native memory is touched — throws IndexOutOfBoundsException with a clear message, handling negatives/overflow.
  • A new ranged private helper copyIn(Arena, byte[], int offset, int length) backs the native copy; the existing whole-array copyIn delegates to it.
  • The whole-array public overloads now delegate to the ranged ones (compress(src, level) -> compress(src, 0, src.length, level), etc.), keeping the existing null-check semantics identical and avoiding duplication.
  • Only (long) length is passed as the native srcSize.

Tests

New RangedOverloads nested class in ZstdTest: ranged round-trips across the shared payload spread, ranged-equals-extracted-copy proof across all levels, full-range equals whole-array overload, empty range, embedded-frame ranged decompress, and negatives (negative offset/length, range past end, null src) asserting the thrown type.

Verification

  • ./mvnw -pl zstd test — green (285 tests).
  • ./mvnw javadoc:javadoc -pl zstd — zero output (failOnWarnings on).
  • ./mvnw -pl zstd validate (checkstyle) — clean.

Closes #55

🤖 Generated with Claude Code

Add (offset, length) sub-range overloads so callers holding a payload
inside a larger buffer can compress/decompress it without copying the
sub-range out first:

- compress(byte[] src, int offset, int length)
- compress(byte[] src, int offset, int length, int level)
- decompress(byte[] compressed, int offset, int length, int maxSize)

The range is validated with Objects.checkFromIndexSize before touching
native memory. Whole-array overloads now delegate to the ranged ones,
keeping the null-check semantics identical. A ranged copyIn helper backs
the native copy.

Closes #55

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dfa1

dfa1 commented Jun 28, 2026

Copy link
Copy Markdown
Owner Author

Closing: declining the ranged byte[] overloads. A byte[] lives on the JVM heap, so these always copy heap→native — they can't be zero-copy, only convenient. Widening the byte[] surface invites callers to reach for it on hot paths expecting efficiency (a footgun). Zero-copy stays the MemorySegment / direct-ByteBuffer path; the byte[] surface stays minimal.

@dfa1 dfa1 closed this Jun 28, 2026
@dfa1 dfa1 deleted the feat/ranged-byte-overloads branch June 28, 2026 15:30
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.

Add ranged byte[] overloads: compress/decompress(src, offset, length)

1 participant