diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java index 2bcda95006..48a2045cb6 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java @@ -25,11 +25,8 @@ public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider pro return; } // the other case is more complicated however. Best to handle with InputStream wrapper. - // But should we rewind it; and/or make a copy? + // Prior to jackson-databind#4164 we rewound here, but that didn't match heap buffer behavior. ByteBuffer copy = bbuf.asReadOnlyBuffer(); - if (copy.position() > 0) { - copy.rewind(); - } InputStream in = new ByteBufferBackedInputStream(copy); gen.writeBinary(in, copy.remaining()); in.close(); diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java index 9bbf116cf4..cda4ee7092 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java @@ -143,6 +143,7 @@ public void testByteBuffer() throws IOException // so far so good, but must ensure Native buffers also work: ByteBuffer bbuf2 = ByteBuffer.allocateDirect(5); bbuf2.put(INPUT_BYTES); + bbuf2.flip(); assertEquals(exp, MAPPER.writeValueAsString(bbuf2)); } @@ -182,6 +183,18 @@ public void testDuplicatedByteBufferWithCustomPosition() throws IOException assertEquals(exp, MAPPER.writeValueAsString(bbuf.duplicate())); } + public void testDuplicatedByteBufferWithCustomPositionDirect() 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.allocateDirect(INPUT_BYTES.length); + bbuf.put(INPUT_BYTES); + bbuf.position(2); + ByteBuffer duplicated = bbuf.duplicate(); + assertEquals(exp, MAPPER.writeValueAsString(duplicated)); + } + // [databind#2197] public void testVoidSerialization() throws Exception {