Skip to content

Commit

Permalink
Fix #2602: Make sure ByteBufferSerializer takes position into account
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Tudenhoefner authored and cowtowncoder committed Jan 28, 2020
1 parent 6e5e2b0 commit c6da520
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ public class ByteBufferSerializer extends StdScalarSerializer<ByteBuffer>
public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException
{
// first, simple case when wrapping an array...
if (bbuf.hasArray()) {
gen.writeBinary(bbuf.array(), bbuf.arrayOffset(), bbuf.limit());
if (bbuf.hasArray())
{
if (bbuf.position() > 0)
{
gen.writeBinary(bbuf.array(), bbuf.position(), bbuf.limit() - bbuf.position());
}
else
{
gen.writeBinary(bbuf.array(), bbuf.arrayOffset(), bbuf.limit());
}
return;
}
// the other case is more complicated however. Best to handle with InputStream wrapper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ public void testSlicedByteBuffer() throws IOException
assertEquals(exp, MAPPER.writeValueAsString(slicedBuf));
}

public void testDuplicatedByteBufferWithCustomPosition() throws IOException
{
final byte[] INPUT_BYTES = new byte[] { 1, 2, 3, 4, 5 };
String exp = MAPPER.writeValueAsString(new byte[] { 3, 4, 5 });

ByteBuffer bbuf = ByteBuffer.wrap(INPUT_BYTES);

bbuf.position(2);
ByteBuffer duplicated = bbuf.duplicate();

assertEquals(exp, MAPPER.writeValueAsString(duplicated));
}

// Verify that efficient UUID codec won't mess things up:
public void testUUIDs() throws IOException
{
Expand Down

0 comments on commit c6da520

Please sign in to comment.