diff --git a/java/src/json/ext/SWARBasicStringEncoder.java b/java/src/json/ext/SWARBasicStringEncoder.java index 08cd2e47..02a3bfcd 100644 --- a/java/src/json/ext/SWARBasicStringEncoder.java +++ b/java/src/json/ext/SWARBasicStringEncoder.java @@ -23,9 +23,13 @@ void encode(ByteList src) throws IOException { int beg = 0; int pos = 0; - ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, len); - while (ptr + pos + 8 <= len) { - long x = bb.getLong(ptr + pos); + // There are optimizations in JRuby where ptr > 0 will be true. For example, if we + // slice a string, the underlying byte array is the same, but the + // begin index and real size are different. When reading from the ptrBytes + // array, we need to always add ptr to the index. + ByteBuffer bb = ByteBuffer.wrap(ptrBytes, ptr, len); + while (pos + 8 <= len) { + long x = bb.getLong(pos); if (skipChunk(x)) { pos += 8; continue; @@ -43,8 +47,8 @@ void encode(ByteList src) throws IOException { } } - if (ptr + pos + 4 <= len) { - int x = bb.getInt(ptr + pos); + if (pos + 4 <= len) { + int x = bb.getInt(pos); if (skipChunk(x)) { pos += 4; }