Skip to content

Commit

Permalink
Merge pull request #65 from blambov/arrayoffset
Browse files Browse the repository at this point in the history
Fixes ByteBuffer methods failing to apply arrayOffset() for array-backed buffers.
  • Loading branch information
jpountz committed May 13, 2015
2 parents 760ad57 + ed95020 commit b69d567
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/build/source_templates/compress.template
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
public int compress(${storage} src, final int srcOff, int srcLen, ${storage} dest, final int destOff, int maxDestLen) {
@if{ storage == "ByteBuffer"}
if (src.hasArray() && dest.hasArray()) {
return compress(src.array(), srcOff, srcLen, dest.array(), destOff, maxDestLen);
return compress(src.array(), srcOff + src.arrayOffset(), srcLen, dest.array(), destOff + dest.arrayOffset(), maxDestLen);
}
src = ${utils}.inNativeByteOrder(src);
dest = ${utils}.inNativeByteOrder(dest);
Expand Down
2 changes: 1 addition & 1 deletion src/build/source_templates/compress_hc.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
public int compress(${storage} src, int srcOff, int srcLen, ${storage} dest, int destOff, int maxDestLen) {
@if{ storage == "ByteBuffer"}
if (src.hasArray() && dest.hasArray()) {
return compress(src.array(), srcOff, srcLen, dest.array(), destOff, maxDestLen);
return compress(src.array(), srcOff + src.arrayOffset(), srcLen, dest.array(), destOff + dest.arrayOffset(), maxDestLen);
}
src = ${utils}.inNativeByteOrder(src);
dest = ${utils}.inNativeByteOrder(dest);
Expand Down
2 changes: 1 addition & 1 deletion src/build/source_templates/decompress.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
public int decompress(${storage} src, final int srcOff@if{ size == "Safe" }, final int srcLen @end{}, ${storage} dest, final int destOff, int destLen) {
@if{ storage == "ByteBuffer"}
if (src.hasArray() && dest.hasArray()) {
return decompress(src.array(), srcOff@if{ size == "Safe" }, srcLen@end{}, dest.array(), destOff, destLen);
return decompress(src.array(), srcOff + src.arrayOffset()@if{ size == "Safe" }, srcLen@end{}, dest.array(), destOff + dest.arrayOffset(), destLen);
}
src = ${utils}.inNativeByteOrder(src);
dest = ${utils}.inNativeByteOrder(dest);
Expand Down
2 changes: 1 addition & 1 deletion src/build/source_templates/xxhash32_hash.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
public int hash(${storage} buf, int off, int len, int seed) {
@if{storage == "ByteBuffer"}
if (buf.hasArray()) {
return hash(buf.array(), off, len, seed);
return hash(buf.array(), off + buf.arrayOffset(), len, seed);
}
${utils}.checkRange(buf, off, len);
buf = ${utils}.inLittleEndianOrder(buf);
Expand Down
2 changes: 1 addition & 1 deletion src/build/source_templates/xxhash64_hash.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
public long hash(${storage} buf, int off, int len, long seed) {
@if{storage == "ByteBuffer"}
if (buf.hasArray()) {
return hash(buf.array(), off, len, seed);
return hash(buf.array(), off + buf.arrayOffset(), len, seed);
}
${utils}.checkRange(buf, off, len);
buf = ${utils}.inLittleEndianOrder(buf);
Expand Down
30 changes: 17 additions & 13 deletions src/java/net/jpountz/lz4/LZ4HCJNICompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,24 @@ public int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int
ByteBufferUtils.checkRange(src, srcOff, srcLen);
ByteBufferUtils.checkRange(dest, destOff, maxDestLen);

byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
} else if (src.isDirect()) {
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
} else if (dest.isDirect()) {
destBuf = dest;
}
if ((src.hasArray() || src.isDirect()) && (dest.hasArray() || dest.isDirect())) {
byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
srcOff += src.arrayOffset();
} else {
assert src.isDirect();
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
destOff += dest.arrayOffset();
} else {
assert dest.isDirect();
destBuf = dest;
}

if ((srcArr != null || srcBuf != null) && (destArr != null || destBuf != null)) {
final int result = LZ4JNI.LZ4_compressHC(srcArr, srcBuf, srcOff, srcLen, destArr, destBuf, destOff, maxDestLen, compressionLevel);
if (result <= 0) {
throw new LZ4Exception();
Expand Down
30 changes: 17 additions & 13 deletions src/java/net/jpountz/lz4/LZ4JNICompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@ public int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int
checkRange(src, srcOff, srcLen);
checkRange(dest, destOff, maxDestLen);

byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
} else if (src.isDirect()) {
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
} else if (dest.isDirect()) {
destBuf = dest;
}
if ((src.hasArray() || src.isDirect()) && (dest.hasArray() || dest.isDirect())) {
byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
srcOff += src.arrayOffset();
} else {
assert src.isDirect();
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
destOff += dest.arrayOffset();
} else {
assert dest.isDirect();
destBuf = dest;
}

if ((srcArr != null || srcBuf != null) && (destArr != null || destBuf != null)) {
final int result = LZ4JNI.LZ4_compress_limitedOutput(srcArr, srcBuf, srcOff, srcLen, destArr, destBuf, destOff, maxDestLen);
if (result <= 0) {
throw new LZ4Exception("maxDestLen is too small");
Expand Down
30 changes: 17 additions & 13 deletions src/java/net/jpountz/lz4/LZ4JNIFastDecompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,24 @@ public int decompress(ByteBuffer src, int srcOff, ByteBuffer dest, int destOff,
ByteBufferUtils.checkRange(src, srcOff);
ByteBufferUtils.checkRange(dest, destOff, destLen);

byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
} else if (src.isDirect()) {
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
} else if (dest.isDirect()) {
destBuf = dest;
}
if ((src.hasArray() || src.isDirect()) && (dest.hasArray() || dest.isDirect())) {
byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
srcOff += src.arrayOffset();
} else {
assert src.isDirect();
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
destOff += dest.arrayOffset();
} else {
assert dest.isDirect();
destBuf = dest;
}

if ((srcArr != null || srcBuf != null) && (destArr != null || destBuf != null)) {
final int result = LZ4JNI.LZ4_decompress_fast(srcArr, srcBuf, srcOff, destArr, destBuf, destOff, destLen);
if (result < 0) {
throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
Expand Down
30 changes: 17 additions & 13 deletions src/java/net/jpountz/lz4/LZ4JNISafeDecompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@ public int decompress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, i
ByteBufferUtils.checkRange(src, srcOff, srcLen);
ByteBufferUtils.checkRange(dest, destOff, maxDestLen);

byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
} else if (src.isDirect()) {
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
} else if (dest.isDirect()) {
destBuf = dest;
}
if ((src.hasArray() || src.isDirect()) && (dest.hasArray() || dest.isDirect())) {
byte[] srcArr = null, destArr = null;
ByteBuffer srcBuf = null, destBuf = null;
if (src.hasArray()) {
srcArr = src.array();
srcOff += src.arrayOffset();
} else {
assert src.isDirect();
srcBuf = src;
}
if (dest.hasArray()) {
destArr = dest.array();
destOff += dest.arrayOffset();
} else {
assert dest.isDirect();
destBuf = dest;
}

if ((srcArr != null || srcBuf != null) && (destArr != null || destBuf != null)) {
final int result = LZ4JNI.LZ4_decompress_safe(srcArr, srcBuf, srcOff, srcLen, destArr, destBuf, destOff, maxDestLen);
if (result < 0) {
throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
Expand Down
2 changes: 1 addition & 1 deletion src/java/net/jpountz/xxhash/XXHash32JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int hash(ByteBuffer buf, int off, int len, int seed) {
checkRange(buf, off, len);
return XXHashJNI.XXH32BB(buf, off, len, seed);
} else if (buf.hasArray()) {
return hash(buf.array(), off, len, seed);
return hash(buf.array(), off + buf.arrayOffset(), len, seed);
} else {
XXHash32 safeInstance = SAFE_INSTANCE;
if (safeInstance == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/java/net/jpountz/xxhash/XXHash64JNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public long hash(ByteBuffer buf, int off, int len, long seed) {
checkRange(buf, off, len);
return XXHashJNI.XXH64BB(buf, off, len, seed);
} else if (buf.hasArray()) {
return hash(buf.array(), off, len, seed);
return hash(buf.array(), off + buf.arrayOffset(), len, seed);
} else {
XXHash64 safeInstance = SAFE_INSTANCE;
if (safeInstance == null) {
Expand Down
7 changes: 5 additions & 2 deletions src/test/net/jpountz/lz4/AbstractLZ4Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ public void fill(byte[] instance, byte b) {
@Override
public ByteBuffer allocate(int length) {
ByteBuffer bb;
int slice = randomInt(5);
if (randomBoolean()) {
bb = ByteBuffer.allocate(length);
bb = ByteBuffer.allocate(length + slice);
} else {
bb = ByteBuffer.allocateDirect(length);
bb = ByteBuffer.allocateDirect(length + slice);
}
bb.position(slice);
bb = bb.slice();
if (randomBoolean()) {
bb.order(ByteOrder.LITTLE_ENDIAN);
} else {
Expand Down

0 comments on commit b69d567

Please sign in to comment.