Skip to content

Commit 5935618

Browse files
author
nathan.sweet
committed
Allow null for input/output strings and chars.
Rearrange JARs. git-svn-id: http://kryo.googlecode.com/svn/trunk@183 a9660ef0-a895-11de-8ba5-91705c1537ed
1 parent bbc3de7 commit 5935618

File tree

9 files changed

+35
-15
lines changed

9 files changed

+35
-15
lines changed

.classpath

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<classpathentry excluding="**/.svn/*" kind="src" path="test"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
66
<classpathentry kind="lib" path="build/junit-4.6.jar"/>
7-
<classpathentry exported="true" kind="lib" path="lib/minlog-1.2.jar"/>
8-
<classpathentry exported="true" kind="lib" path="lib/asm-3.3.1.jar"/>
9-
<classpathentry exported="true" kind="lib" path="lib/reflectasm-1.01.jar"/>
7+
<classpathentry kind="lib" path="lib/optional/asm-3.3.1.jar"/>
8+
<classpathentry kind="lib" path="lib/optional/reflectasm-1.01.jar"/>
9+
<classpathentry kind="lib" path="lib/minlog-1.2.jar"/>
1010
<classpathentry kind="output" path="bin"/>
1111
</classpath>

build/minlog-1.2.jar

3.01 KB
Binary file not shown.

build/minlog-none-1.2.jar

2.72 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.

project.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
version: 2.01
2-
dependencies:
3-
- ../reflectasm
4-
- ../minlog
1+
version: 2.02
52
---
63
Build.build(project);
7-
Build.oneJAR(project);
4+
Build.oneJAR(project);

src/com/esotericsoftware/kryo/io/Input.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,13 @@ public boolean canReadInt () throws KryoException {
344344
/** Reads the length and string of 8 bit characters. */
345345
public String readChars () throws KryoException {
346346
int charCount = readInt(true);
347-
if (charCount == 0) return "";
347+
switch (charCount) {
348+
case 0:
349+
return null;
350+
case 1:
351+
return "";
352+
}
353+
charCount--;
348354
if (chars.length < charCount) chars = new char[charCount];
349355
if (charCount > require(1, charCount)) return readChars_slow(charCount);
350356
char[] chars = this.chars;
@@ -368,7 +374,13 @@ private String readChars_slow (int charCount) {
368374
/** Reads the length and string of UTF8 characters. */
369375
public String readString () throws KryoException {
370376
int charCount = readInt(true);
371-
if (charCount == 0) return "";
377+
switch (charCount) {
378+
case 0:
379+
return null;
380+
case 1:
381+
return "";
382+
}
383+
charCount--;
372384
if (chars.length < charCount) chars = new char[charCount];
373385
char[] chars = this.chars;
374386
byte[] buffer = this.buffer;

src/com/esotericsoftware/kryo/io/Output.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,12 @@ else if ((value >>> 21 & ~0x7F) == 0)
232232

233233
/** Writes the length and string using 1 byte per character. */
234234
public void writeChars (String value) throws KryoException {
235-
if (value == null) throw new IllegalArgumentException("value cannot be null.");
235+
if (value == null) {
236+
writeByte(0);
237+
return;
238+
}
236239
int charCount = value.length();
237-
writeInt(charCount, true);
240+
writeInt(charCount + 1, true);
238241
if (capacity < charCount)
239242
writeChars_slow(value, charCount);
240243
else {
@@ -257,11 +260,15 @@ private void writeChars_slow (String value, int charCount) throws KryoException
257260
}
258261
}
259262

260-
/** Writes the length and string using UTF8. */
263+
/** Writes the length and string using UTF8, or null.
264+
* @param value May be null. */
261265
public void writeString (String value) throws KryoException {
262-
if (value == null) throw new IllegalArgumentException("value cannot be null.");
266+
if (value == null) {
267+
writeByte(0);
268+
return;
269+
}
263270
int charCount = value.length();
264-
writeInt(charCount, true);
271+
writeInt(charCount + 1, true);
265272
int charIndex = 0;
266273
if (capacity >= charCount) {
267274
// Try to write 8 bit chars.

test/com/esotericsoftware/kryo/InputOutputTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,26 @@ public void runUTFTest (Output write) throws IOException {
8787
write.writeString("uno");
8888
write.writeString("dos");
8989
write.writeString("tres");
90+
write.writeString(null);
9091
write.writeString(value1);
9192
write.writeString(value2);
9293
write.writeChars("uno");
9394
write.writeChars("dos");
9495
write.writeChars("tres");
96+
write.writeChars(null);
9597
write.writeChars(value1);
9698

9799
Input read = new Input(write.toBytes());
98100
assertEquals("uno", read.readString());
99101
assertEquals("dos", read.readString());
100102
assertEquals("tres", read.readString());
103+
assertEquals(null, read.readString());
101104
assertEquals(value1, read.readString());
102105
assertEquals(value2, read.readString());
103106
assertEquals("uno", read.readChars());
104107
assertEquals("dos", read.readChars());
105108
assertEquals("tres", read.readChars());
109+
assertEquals(null, read.readChars());
106110
assertEquals(value1, read.readChars());
107111
}
108112

0 commit comments

Comments
 (0)