Fixed charset issues

This commit is contained in:
Anuken
2018-11-21 23:05:33 -05:00
parent 7795a690ed
commit 9266b55ddf

View File

@@ -27,7 +27,6 @@ import java.io.DataInput;
import java.io.DataOutput; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static io.anuke.mindustry.Vars.*; import static io.anuke.mindustry.Vars.*;
@@ -332,12 +331,7 @@ public class TypeIO{
@WriteClass(String.class) @WriteClass(String.class)
public static void writeString(ByteBuffer buffer, String string){ public static void writeString(ByteBuffer buffer, String string){
if(string != null){ if(string != null){
Charset charset = Charset.defaultCharset(); byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
byte[] nameBytes = charset.name().getBytes(StandardCharsets.UTF_8);
buffer.put((byte)nameBytes.length);
buffer.put(nameBytes);
byte[] bytes = string.getBytes(charset);
buffer.putShort((short) bytes.length); buffer.putShort((short) bytes.length);
buffer.put(bytes); buffer.put(bytes);
}else{ }else{
@@ -349,14 +343,10 @@ public class TypeIO{
public static String readString(ByteBuffer buffer){ public static String readString(ByteBuffer buffer){
byte length = buffer.get(); byte length = buffer.get();
if(length != -1){ if(length != -1){
byte[] cbytes = new byte[length];
buffer.get(cbytes);
Charset charset = Charset.forName(new String(cbytes, StandardCharsets.UTF_8));
short slength = buffer.getShort(); short slength = buffer.getShort();
byte[] bytes = new byte[slength]; byte[] bytes = new byte[slength];
buffer.get(bytes); buffer.get(bytes);
return new String(bytes, charset); return new String(bytes, StandardCharsets.UTF_8);
}else{ }else{
return null; return null;
} }
@@ -378,12 +368,7 @@ public class TypeIO{
public static void writeStringData(DataOutput buffer, String string) throws IOException{ public static void writeStringData(DataOutput buffer, String string) throws IOException{
if(string != null){ if(string != null){
Charset charset = Charset.defaultCharset(); byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
byte[] nameBytes = charset.name().getBytes(StandardCharsets.UTF_8);
buffer.writeByte((byte)nameBytes.length);
buffer.write(nameBytes);
byte[] bytes = string.getBytes(charset);
buffer.writeShort((short) bytes.length); buffer.writeShort((short) bytes.length);
buffer.write(bytes); buffer.write(bytes);
}else{ }else{
@@ -394,14 +379,10 @@ public class TypeIO{
public static String readStringData(DataInput buffer) throws IOException{ public static String readStringData(DataInput buffer) throws IOException{
byte length = buffer.readByte(); byte length = buffer.readByte();
if(length != -1){ if(length != -1){
byte[] cbytes = new byte[length];
buffer.readFully(cbytes);
Charset charset = Charset.forName(new String(cbytes, StandardCharsets.UTF_8));
short slength = buffer.readShort(); short slength = buffer.readShort();
byte[] bytes = new byte[slength]; byte[] bytes = new byte[slength];
buffer.readFully(bytes); buffer.readFully(bytes);
return new String(bytes, charset); return new String(bytes, StandardCharsets.UTF_8);
}else{ }else{
return null; return null;
} }