Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/main/java/in/dragonbra/javasteam/types/KeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ private void recursiveSaveBinaryToStreamCore(OutputStream os) throws IOException
// Only supported types ATM:
// 1. KeyValue with children (no value itself)
// 2. String KeyValue
if (!children.isEmpty()) {
if (value == null) {
os.write(Type.NONE.code());
os.write(name.getBytes(Charset.forName("UTF-8")));
os.write(0);
Expand All @@ -532,11 +532,7 @@ private void recursiveSaveBinaryToStreamCore(OutputStream os) throws IOException
os.write(Type.STRING.code());
os.write(name.getBytes(Charset.forName("UTF-8")));
os.write(0);
if (value == null) {
os.write("".getBytes(Charset.forName("UTF-8")));
} else {
os.write(value.getBytes(Charset.forName("UTF-8")));
}
os.write(value.getBytes(Charset.forName("UTF-8")));
os.write(0);
}
}
Expand Down
49 changes: 48 additions & 1 deletion src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public void keyValuesSavesTextToFile() throws IOException {

String text;
File temporaryFile = folder.newFile();
kv.saveToFile(temporaryFile, false );
kv.saveToFile(temporaryFile, false);
text = new String(Files.readAllBytes(Paths.get(temporaryFile.getPath())));

assertEquals(expected, text);
Expand Down Expand Up @@ -431,6 +431,53 @@ public void keyValuesEscapesTextWhenSerializing() throws IOException {
assertEquals(expectedValue, text);
}

@Test
public void keyValuesTextPreserveEmptyObjects() throws IOException {
KeyValue kv = new KeyValue("key");
kv.getChildren().add(new KeyValue("emptyObj"));
kv.getChildren().add(new KeyValue("emptyString", ""));

String text;
MemoryStream ms = new MemoryStream();
kv.saveToStream(ms.asOutputStream(), false);
ms.seek(0, SeekOrigin.BEGIN);

text = new String(ms.toByteArray());

String expectedValue = "\"key\"\n{\n\t\"emptyObj\"\n\t{\n\t}\n\t\"emptyString\"\t\t\"\"\n}\n";
assertEquals(expectedValue, text);
}

@Test
public void keyValuesBinaryPreserveEmptyObjects() throws IOException {
String expectedHexString = "006B65790000656D7074794F626A000801656D707479537472696E6700000808";

KeyValue kv = new KeyValue("key");
kv.getChildren().add(new KeyValue("emptyObj"));
kv.getChildren().add(new KeyValue("emptyString", ""));

KeyValue deserializedKv = new KeyValue();
byte[] binaryValue;

MemoryStream ms = new MemoryStream();
kv.saveToStream(ms.asOutputStream(), true);
ms.seek(0, SeekOrigin.BEGIN);

binaryValue = ms.toByteArray();
deserializedKv.tryReadAsBinary(ms);

StringBuilder hexValue = new StringBuilder();
for (byte value : binaryValue) {
String string = String.format("%02X", value);
hexValue.append(string);
}

assertEquals(expectedHexString, hexValue.toString());
assertNull(deserializedKv.get("emptyObj").getValue());
assertTrue(deserializedKv.get("emptyObj").getChildren().isEmpty());
assertEquals("", deserializedKv.get("emptyString").getValue());
}

@Test
public void DecodesBinaryWithFieldType10() throws IOException, DecoderException {
String hex = "00546573744F626A656374000A6B65790001020304050607080808";
Expand Down