Skip to content

Commit

Permalink
Allow null for input/output strings and chars.
Browse files Browse the repository at this point in the history
Rearrange JARs.
  • Loading branch information
NathanSweet committed Apr 7, 2012
1 parent df4f1ce commit 03054ec
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<classpathentry excluding="**/.svn/*" kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="build/junit-4.6.jar"/>
<classpathentry exported="true" kind="lib" path="lib/minlog-1.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/asm-3.3.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/reflectasm-1.01.jar"/>
<classpathentry kind="lib" path="lib/optional/asm-3.3.1.jar"/>
<classpathentry kind="lib" path="lib/optional/reflectasm-1.01.jar"/>
<classpathentry kind="lib" path="lib/minlog-1.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added build/minlog-1.2.jar
Binary file not shown.
Binary file added build/minlog-none-1.2.jar
Binary file not shown.
File renamed without changes.
File renamed without changes.
7 changes: 2 additions & 5 deletions project.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
version: 2.01
dependencies:
- ../reflectasm
- ../minlog
version: 2.02
---
Build.build(project);
Build.oneJAR(project);
Build.oneJAR(project);
16 changes: 14 additions & 2 deletions src/com/esotericsoftware/kryo/io/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,13 @@ public boolean canReadInt () throws KryoException {
/** Reads the length and string of 8 bit characters. */
public String readChars () throws KryoException {
int charCount = readInt(true);
if (charCount == 0) return "";
switch (charCount) {
case 0:
return null;
case 1:
return "";
}
charCount--;
if (chars.length < charCount) chars = new char[charCount];
if (charCount > require(1, charCount)) return readChars_slow(charCount);
char[] chars = this.chars;
Expand All @@ -368,7 +374,13 @@ private String readChars_slow (int charCount) {
/** Reads the length and string of UTF8 characters. */
public String readString () throws KryoException {
int charCount = readInt(true);
if (charCount == 0) return "";
switch (charCount) {
case 0:
return null;
case 1:
return "";
}
charCount--;
if (chars.length < charCount) chars = new char[charCount];
char[] chars = this.chars;
byte[] buffer = this.buffer;
Expand Down
17 changes: 12 additions & 5 deletions src/com/esotericsoftware/kryo/io/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ else if ((value >>> 21 & ~0x7F) == 0)

/** Writes the length and string using 1 byte per character. */
public void writeChars (String value) throws KryoException {
if (value == null) throw new IllegalArgumentException("value cannot be null.");
if (value == null) {
writeByte(0);
return;
}
int charCount = value.length();
writeInt(charCount, true);
writeInt(charCount + 1, true);
if (capacity < charCount)
writeChars_slow(value, charCount);
else {
Expand All @@ -257,11 +260,15 @@ private void writeChars_slow (String value, int charCount) throws KryoException
}
}

/** Writes the length and string using UTF8. */
/** Writes the length and string using UTF8, or null.
* @param value May be null. */
public void writeString (String value) throws KryoException {
if (value == null) throw new IllegalArgumentException("value cannot be null.");
if (value == null) {
writeByte(0);
return;
}
int charCount = value.length();
writeInt(charCount, true);
writeInt(charCount + 1, true);
int charIndex = 0;
if (capacity >= charCount) {
// Try to write 8 bit chars.
Expand Down
4 changes: 4 additions & 0 deletions test/com/esotericsoftware/kryo/InputOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,26 @@ public void runUTFTest (Output write) throws IOException {
write.writeString("uno");
write.writeString("dos");
write.writeString("tres");
write.writeString(null);
write.writeString(value1);
write.writeString(value2);
write.writeChars("uno");
write.writeChars("dos");
write.writeChars("tres");
write.writeChars(null);
write.writeChars(value1);

Input read = new Input(write.toBytes());
assertEquals("uno", read.readString());
assertEquals("dos", read.readString());
assertEquals("tres", read.readString());
assertEquals(null, read.readString());
assertEquals(value1, read.readString());
assertEquals(value2, read.readString());
assertEquals("uno", read.readChars());
assertEquals("dos", read.readChars());
assertEquals("tres", read.readChars());
assertEquals(null, read.readChars());
assertEquals(value1, read.readChars());
}

Expand Down

0 comments on commit 03054ec

Please sign in to comment.