Skip to content

Commit 32a9028

Browse files
authored
Merge branch '2.19' into issue388
2 parents bae0a2c + 76243f8 commit 32a9028

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ Knut Wannheden (@knutwannheden)
363363
* Contributed #518: Should not read past end for CBOR string values
364364
(2.18.1)
365365

366+
Steven Fackler (@sfackler)
367+
* Reported #300: (smile) Floats are encoded with sign extension while
368+
doubles without
369+
(2.19.0)
370+
366371
Idan Sheinberg (@sheinbergon)
367372
* Reported #308: (avro) Incorrect serialization for `LogicalType.Decimal` (Java
368373
`BigDecimal`)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Active maintainers:
1616

1717
2.19.0 (not yet released)
1818

19+
#300: (smile) Floats are encoded with sign extension while doubles without
20+
(reported by Steven F)
1921
#308: (avro) Incorrect serialization for `LogicalType.Decimal` (Java `BigDecimal`)
2022
(reported by Idan S)
2123
(fix contributed by Michal F)

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,13 +1726,13 @@ public void writeNumber(float f) throws IOException
17261726
int i = Float.floatToRawIntBits(f);
17271727
_outputBuffer[_outputTail++] = TOKEN_BYTE_FLOAT_32;
17281728
_outputBuffer[_outputTail+4] = (byte) (i & 0x7F);
1729-
i >>= 7;
1729+
i >>>= 7;
17301730
_outputBuffer[_outputTail+3] = (byte) (i & 0x7F);
1731-
i >>= 7;
1731+
i >>>= 7;
17321732
_outputBuffer[_outputTail+2] = (byte) (i & 0x7F);
1733-
i >>= 7;
1733+
i >>>= 7;
17341734
_outputBuffer[_outputTail+1] = (byte) (i & 0x7F);
1735-
i >>= 7;
1735+
i >>>= 7;
17361736
_outputBuffer[_outputTail] = (byte) (i & 0x7F);
17371737
_outputTail += 5;
17381738
}

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/TestGeneratorNumbers.java renamed to smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/SmileGeneratorNumbersTest.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.fasterxml.jackson.dataformat.smile.gen;
22

33
import java.io.ByteArrayOutputStream;
4+
import java.util.Arrays;
45

56
import org.junit.jupiter.api.Test;
67

78
import com.fasterxml.jackson.dataformat.smile.*;
89

10+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
911
import static org.junit.jupiter.api.Assertions.assertEquals;
1012

11-
public class TestGeneratorNumbers
13+
public class SmileGeneratorNumbersTest
1214
extends BaseTestForSmile
1315
{
1416
@Test
@@ -118,9 +120,9 @@ public void testFloats() throws Exception
118120
{
119121
// float length is fixed, 6 bytes
120122
ByteArrayOutputStream out = new ByteArrayOutputStream();
121-
SmileGenerator gen = smileGenerator(out, false);
122-
gen.writeNumber(0.125f);
123-
gen.close();
123+
try (SmileGenerator gen = smileGenerator(out, false)) {
124+
gen.writeNumber(0.125f);
125+
}
124126
assertEquals(6, out.toByteArray().length);
125127
}
126128

@@ -129,12 +131,49 @@ public void testDoubles() throws Exception
129131
{
130132
// double length is fixed, 11 bytes
131133
ByteArrayOutputStream out = new ByteArrayOutputStream();
132-
SmileGenerator gen = smileGenerator(out, false);
133-
gen.writeNumber(0.125);
134-
gen.close();
134+
try (SmileGenerator gen = smileGenerator(out, false)) {
135+
gen.writeNumber(0.125);
136+
}
135137
assertEquals(11, out.toByteArray().length);
136138
}
137139

140+
// [dataformats-binary#300]
141+
@Test
142+
public void testFloatUnusedBits() throws Exception
143+
{
144+
ByteArrayOutputStream out = new ByteArrayOutputStream();
145+
try (SmileGenerator gen = smileGenerator(out, false)) {
146+
gen.writeNumber(-0f);
147+
}
148+
byte[] encoded = out.toByteArray();
149+
assertEquals(6, encoded.length);
150+
assertEquals(0x28, encoded[0]); // type byte, float
151+
152+
// From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits)
153+
assertArrayEquals(new byte[] {
154+
0x08, 0x00, 0x00, 0x00, 0x00
155+
}, Arrays.copyOfRange(encoded, 1, encoded.length));
156+
}
157+
158+
// [dataformats-binary#300]
159+
@Test
160+
public void testDoubleUnusedBits() throws Exception
161+
{
162+
ByteArrayOutputStream out = new ByteArrayOutputStream();
163+
try (SmileGenerator gen = smileGenerator(out, false)) {
164+
gen.writeNumber(-0d);
165+
}
166+
byte[] encoded = out.toByteArray();
167+
assertEquals(11, encoded.length);
168+
assertEquals(0x29, encoded[0]); // type byte, double
169+
// From 0x80 0x00 0x00 0x00 ... 0x00 (spread over 10 x 7 bits)
170+
171+
assertArrayEquals(new byte[] {
172+
0x01, 0x00, 0x00, 0x00, 0x00,
173+
0x00, 0x00, 0x00, 0x00, 0x00
174+
}, Arrays.copyOfRange(encoded, 1, encoded.length));
175+
}
176+
138177
// #16: Problems with 'Stringified' numbers
139178
@Test
140179
public void testNumbersAsString() throws Exception

0 commit comments

Comments
 (0)