Skip to content

Commit edf82d1

Browse files
committed
Update release notes wrt, made small changes to fix for FasterXML#2602
1 parent c6da520 commit edf82d1

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,3 +1027,8 @@ Tobias Preuss (johnjohndoe@github)
10271027
* Reported #2599: NoClassDefFoundError at DeserializationContext.<init> on Android 4.1.2
10281028
and Jackson 2.10.0
10291029
(2.10.3)
1030+
1031+
Eduard Tudenhöfner (nastra@github)
1032+
* Reported #2602, contributed fix for: ByteBufferSerializer produces unexpected results with
1033+
a duplicated ByteBuffer and a position > 0
1034+
(2.10.3)

release-notes/VERSION-2.x

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ Project: jackson-databind
66

77
2.10.3 (not yet released)
88

9-
#2599: NoClassDefFoundError at DeserializationContext.<init> on Android 4.1.2 and Jackson 2.10.0
9+
#2599: NoClassDefFoundError at DeserializationContext.<init> on Android 4.1.2
10+
and Jackson 2.10.0
1011
(reported by Tobias P)
12+
#2602: ByteBufferSerializer produces unexpected results with a duplicated ByteBuffer
13+
and a position > 0
14+
(reported by Eduard T)
1115

1216
2.10.2 (05-Jan-2020)
1317

src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@ public class ByteBufferSerializer extends StdScalarSerializer<ByteBuffer>
1919
public void serialize(ByteBuffer bbuf, JsonGenerator gen, SerializerProvider provider) throws IOException
2020
{
2121
// first, simple case when wrapping an array...
22-
if (bbuf.hasArray())
23-
{
24-
if (bbuf.position() > 0)
25-
{
26-
gen.writeBinary(bbuf.array(), bbuf.position(), bbuf.limit() - bbuf.position());
27-
}
28-
else
29-
{
30-
gen.writeBinary(bbuf.array(), bbuf.arrayOffset(), bbuf.limit());
31-
}
22+
if (bbuf.hasArray()) {
23+
final int pos = bbuf.position();
24+
gen.writeBinary(bbuf.array(), bbuf.arrayOffset() + pos, bbuf.limit() - pos);
3225
return;
3326
}
3427
// the other case is more complicated however. Best to handle with InputStream wrapper.

src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,36 @@ public void testByteBuffer() throws IOException
150150
public void testSlicedByteBuffer() throws IOException
151151
{
152152
final byte[] INPUT_BYTES = new byte[] { 1, 2, 3, 4, 5 };
153-
String exp = MAPPER.writeValueAsString(new byte[] { 3, 4, 5 });
154153
ByteBuffer bbuf = ByteBuffer.wrap(INPUT_BYTES);
155154

156155
bbuf.position(2);
157156
ByteBuffer slicedBuf = bbuf.slice();
158157

159-
assertEquals(exp, MAPPER.writeValueAsString(slicedBuf));
158+
assertEquals(MAPPER.writeValueAsString(new byte[] { 3, 4, 5 }),
159+
MAPPER.writeValueAsString(slicedBuf));
160+
161+
// but how about offset within?
162+
slicedBuf.position(1);
163+
assertEquals(MAPPER.writeValueAsString(new byte[] { 4, 5 }),
164+
MAPPER.writeValueAsString(slicedBuf));
160165
}
161166

167+
// [databind#2602]: Need to consider position()
162168
public void testDuplicatedByteBufferWithCustomPosition() throws IOException
163169
{
164170
final byte[] INPUT_BYTES = new byte[] { 1, 2, 3, 4, 5 };
165-
String exp = MAPPER.writeValueAsString(new byte[] { 3, 4, 5 });
166171

172+
String exp = MAPPER.writeValueAsString(new byte[] { 3, 4, 5 });
167173
ByteBuffer bbuf = ByteBuffer.wrap(INPUT_BYTES);
168-
169174
bbuf.position(2);
170175
ByteBuffer duplicated = bbuf.duplicate();
171-
172176
assertEquals(exp, MAPPER.writeValueAsString(duplicated));
177+
178+
// also check differently constructed bytebuffer (noting that
179+
// offset given is the _position_ to use, NOT array offset
180+
exp = MAPPER.writeValueAsString(new byte[] { 2, 3, 4 });
181+
bbuf = ByteBuffer.wrap(INPUT_BYTES, 1, 3);
182+
assertEquals(exp, MAPPER.writeValueAsString(bbuf.duplicate()));
173183
}
174184

175185
// Verify that efficient UUID codec won't mess things up:

0 commit comments

Comments
 (0)