cache: fix outputstream writeString to not write unicode strings

Add test for assembling a script with a nbsp in it
This commit is contained in:
Adam
2018-08-12 13:08:50 -04:00
parent 47458bcc33
commit c02ff65a5a
5 changed files with 47 additions and 6 deletions

View File

@@ -26,6 +26,7 @@ package net.runelite.cache.io;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
public final class OutputStream extends java.io.OutputStream
@@ -180,7 +181,15 @@ public final class OutputStream extends java.io.OutputStream
public void writeString(String str)
{
byte[] b = str.getBytes();
byte[] b;
try
{
b = str.getBytes("ISO-8859-1");
}
catch (UnsupportedEncodingException ex)
{
throw new RuntimeException(ex);
}
writeBytes(b);
writeByte(0);
}

View File

@@ -39,6 +39,7 @@ import org.junit.Test;
public class ScriptSaverTest
{
private static final String SCRIPT_RESOURCE = "/net/runelite/cache/script/assembler/91.rs2asm";
private static final String SCRIPT_RESOURCE_UNICODE = "/net/runelite/cache/script/assembler/Unicode.rs2asm";
@Test
public void testSave() throws IOException
@@ -51,4 +52,15 @@ public class ScriptSaverTest
assertEquals(script, loadedScripot);
}
@Test
public void testSaveUnicode() throws IOException
{
Instructions instructions = new Instructions();
instructions.init();
ScriptDefinition script = new Assembler(instructions).assemble(getClass().getResourceAsStream(SCRIPT_RESOURCE_UNICODE));
byte[] saved = new ScriptSaver().save(script);
ScriptDefinition loadedScripot = new ScriptLoader().load(42, saved);
assertEquals(script, loadedScripot);
}
}

View File

@@ -24,7 +24,7 @@
*/
package net.runelite.cache.io;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class OutputStreamTest
@@ -38,9 +38,22 @@ public class OutputStreamTest
os.writeBigSmart(65535);
InputStream is = new InputStream(os.getArray());
Assert.assertEquals(42, is.readBigSmart());
Assert.assertEquals(70000, is.readBigSmart());
Assert.assertEquals(65535, is.readBigSmart());
assertEquals(42, is.readBigSmart());
assertEquals(70000, is.readBigSmart());
assertEquals(65535, is.readBigSmart());
}
@Test
public void testWriteString()
{
char[] c = new char[]{32, 160};
String str = new String(c, 0, c.length);
OutputStream os = new OutputStream();
os.writeString(str);
// 1 byte length + 32 + 160
assertEquals(3, os.getOffset());
}
}

View File

@@ -52,7 +52,8 @@ public class AssemblerTest
return new String[]
{
"91.rs2asm",
"681.rs2asm"
"681.rs2asm",
"Unicode.rs2asm"
};
}

View File

@@ -0,0 +1,6 @@
.int_stack_count 0
.string_stack_count 0
.int_var_count 0
.string_var_count 0
load_string ": "
return