From 5dfd9c4157972b8559b88c9b380cb724bf42aca0 Mon Sep 17 00:00:00 2001 From: yawkat Date: Tue, 17 Oct 2023 15:14:08 +0200 Subject: [PATCH 1/2] Consider position when serializing direct ByteBuffer ByteBufferSerializer considers the position when serializing heap buffers, but ignores it (by rewinding) for direct buffers. I believe the former behavior is correct, so I've aligned the direct buffer code with that. This might be better for 2.17, I don't know. --- .../databind/ser/std/ByteBufferSerializer.java | 3 --- .../databind/ser/jdk/JDKTypeSerializationTest.java | 13 +++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) 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..99281e2b03 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 @@ -27,9 +27,6 @@ public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider pro // the other case is more complicated however. Best to handle with InputStream wrapper. // But should we rewind it; and/or make a copy? 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 { From 92f58117e4cfa8663638ba5b01291a2a62b95341 Mon Sep 17 00:00:00 2001 From: yawkat Date: Tue, 17 Oct 2023 17:30:39 +0200 Subject: [PATCH 2/2] fix comment --- .../jackson/databind/ser/std/ByteBufferSerializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 99281e2b03..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,7 +25,7 @@ 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(); InputStream in = new ByteBufferBackedInputStream(copy); gen.writeBinary(in, copy.remaining());