Skip to content

Commit

Permalink
Fix #366 for 2.14(.3)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 10, 2023
1 parent 9443add commit 0e3505a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ public void writeRawUTF8String(byte[] raw, int offset, int len)
return;
}
_writeLengthMarker(PREFIX_TYPE_TEXT, len);
_writeBytes(raw, 0, len);
_writeBytes(raw, offset, len);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Random;

Expand Down Expand Up @@ -267,6 +268,14 @@ public String q(String str) {
return '"'+str+'"';
}

protected static byte[] utf8Bytes(String str) {
return str.getBytes(StandardCharsets.UTF_8);
}

protected static String utf8String(ByteArrayOutputStream bytes) {
return new String(bytes.toByteArray(), StandardCharsets.UTF_8);
}

protected static byte[] concat(byte[] ... chunks)
{
int len = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,22 @@ public void testLongWithMultiBytes() throws Exception
for (int i = 0, len = strings.size(); i < len; ++i) {
String str = strings.get(i);
// let's mix in alternative representaton as well
if ((++fuzz % 7) == 1) {
byte[] b = str.getBytes("UTF-8");
gen.writeRawUTF8String(b, 0, b.length);
} else {
switch (++fuzz % 7) {
case 1:
{
byte[] b = utf8Bytes(str);
gen.writeRawUTF8String(b, 0, b.length);
}
break;
case 2:
{
byte[] b = utf8Bytes(str);
byte[] tmp = new byte[b.length + 4];
System.arraycopy(b, 0, tmp, 2, b.length);
gen.writeRawUTF8String(tmp, 2, b.length);
}
break;
default:
char[] ch = str.toCharArray();
gen.writeString(ch, 0, ch.length);
}
Expand Down
6 changes: 6 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,9 @@ Brian Harrington (brharrington@github)

* Contributed #342: (smile) Possible performance improvement on jdk9+ for Smile decoding
(2.14.1)

Nik Everett (nik9000@github)

* Reported #366: `CBORGenerator.writeRawUTF8String()` seems to ignore offset
(2.14.3)

4 changes: 3 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ Active maintainers:

Not yet released

#354: Missing license file in Maven package for newer versions
#354: (all) Missing license file in Maven package for newer versions
(reported by Philipp d-S)
#366: `CBORGenerator.writeRawUTF8String()` seems to ignore offset
(reported by Nik E)

2.14.2 (28-Jan-2023)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ public void testUtf8RawStrings() throws Exception
SmileFactory jf = new SmileFactory();
JsonGenerator jgen = jf.createGenerator(out, JsonEncoding.UTF8);
jgen.writeStartArray();
int counter = 0;
for (byte[] str : strings) {
jgen.writeRawUTF8String(str, 0, str.length);
// 09-Mar-2023, tatu: Let's use offset...
if ((++counter & 3) == 0) {
byte[] tmp = new byte[str.length + 4];
System.arraycopy(str, 0, tmp, 2, str.length);
jgen.writeRawUTF8String(tmp, 2, str.length);
} else {
jgen.writeRawUTF8String(str, 0, str.length);
}
}
jgen.writeEndArray();
jgen.close();
Expand Down

0 comments on commit 0e3505a

Please sign in to comment.