Encoding tests
This commit is contained in:
@@ -335,15 +335,14 @@ public class TypeIO{
|
|||||||
buffer.putShort((short) bytes.length);
|
buffer.putShort((short) bytes.length);
|
||||||
buffer.put(bytes);
|
buffer.put(bytes);
|
||||||
}else{
|
}else{
|
||||||
buffer.put((byte) -1);
|
buffer.putShort((short) -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReadClass(String.class)
|
@ReadClass(String.class)
|
||||||
public static String readString(ByteBuffer buffer){
|
public static String readString(ByteBuffer buffer){
|
||||||
byte length = buffer.get();
|
short slength = buffer.getShort();
|
||||||
if(length != -1){
|
if(slength != -1){
|
||||||
short slength = buffer.getShort();
|
|
||||||
byte[] bytes = new byte[slength];
|
byte[] bytes = new byte[slength];
|
||||||
buffer.get(bytes);
|
buffer.get(bytes);
|
||||||
return new String(bytes, StandardCharsets.UTF_8);
|
return new String(bytes, StandardCharsets.UTF_8);
|
||||||
@@ -372,14 +371,13 @@ public class TypeIO{
|
|||||||
buffer.writeShort((short) bytes.length);
|
buffer.writeShort((short) bytes.length);
|
||||||
buffer.write(bytes);
|
buffer.write(bytes);
|
||||||
}else{
|
}else{
|
||||||
buffer.writeByte((byte) -1);
|
buffer.writeShort((short) -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String readStringData(DataInput buffer) throws IOException{
|
public static String readStringData(DataInput buffer) throws IOException{
|
||||||
byte length = buffer.readByte();
|
short slength = buffer.readShort();
|
||||||
if(length != -1){
|
if(slength != -1){
|
||||||
short slength = buffer.readShort();
|
|
||||||
byte[] bytes = new byte[slength];
|
byte[] bytes = new byte[slength];
|
||||||
buffer.readFully(bytes);
|
buffer.readFully(bytes);
|
||||||
return new String(bytes, StandardCharsets.UTF_8);
|
return new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
|||||||
34
tests/src/test/java/IOTests.java
Normal file
34
tests/src/test/java/IOTests.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import io.anuke.mindustry.io.TypeIO;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class IOTests{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void writeEnglish(){
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocate(500);
|
||||||
|
TypeIO.writeString(buffer, "asd asd asd asd asdagagasasjakbgeah;jwrej 23424234");
|
||||||
|
buffer.position(0);
|
||||||
|
assertEquals(TypeIO.readString(buffer), "asd asd asd asd asdagagasasjakbgeah;jwrej 23424234");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void writeChinese(){
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocate(500);
|
||||||
|
TypeIO.writeString(buffer, "这个服务器可以用自己的语言说话");
|
||||||
|
buffer.position(0);
|
||||||
|
assertEquals(TypeIO.readString(buffer), "这个服务器可以用自己的语言说话");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void writeNull(){
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocate(500);
|
||||||
|
TypeIO.writeString(buffer, null);
|
||||||
|
buffer.position(0);
|
||||||
|
assertEquals(TypeIO.readString(buffer), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user