Fixed string reading not using UTF-8

This commit is contained in:
LOOHP 2020-08-26 01:09:53 +08:00
parent 148a9423c9
commit 10789c3c60
8 changed files with 13 additions and 9 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>Limbo</groupId>
<artifactId>Limbo</artifactId>
<version>0.3.1-ALPHA</version>
<version>0.3.2-ALPHA</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>

View File

@ -2,6 +2,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
@ -46,7 +47,7 @@ public class PacketHandshakingIn extends PacketIn {
}
public PacketHandshakingIn(DataInputStream in) throws IOException {
this(DataTypeIO.readVarInt(in), DataTypeIO.readString(in), in.readShort() & 0xFFFF, HandshakeType.fromNetworkId(DataTypeIO.readVarInt(in)));
this(DataTypeIO.readVarInt(in), DataTypeIO.readString(in, StandardCharsets.UTF_8), in.readShort() & 0xFFFF, HandshakeType.fromNetworkId(DataTypeIO.readVarInt(in)));
}
public int getProtocolVersion() {

View File

@ -2,6 +2,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
@ -14,7 +15,7 @@ public class PacketLoginInLoginStart extends PacketIn {
}
public PacketLoginInLoginStart(DataInputStream in) throws IOException {
this(DataTypeIO.readString(in));
this(DataTypeIO.readString(in, StandardCharsets.UTF_8));
}
public String getUsername() {

View File

@ -21,7 +21,7 @@ public class PacketLoginInPluginMessaging extends PacketIn {
public PacketLoginInPluginMessaging(DataInputStream in, int packetLength, int packetId) throws IOException {
messageId = DataTypeIO.readVarInt(in);
String rawChannel = DataTypeIO.readString(in);
String rawChannel = DataTypeIO.readString(in, StandardCharsets.UTF_8);
channel = new NamespacedKey(rawChannel);
int dataLength = packetLength - DataTypeIO.getVarIntLength(packetId) - DataTypeIO.getVarIntLength(messageId) - DataTypeIO.getStringLength(rawChannel, StandardCharsets.UTF_8);
data = new byte[dataLength];

View File

@ -2,6 +2,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
@ -14,7 +15,7 @@ public class PacketPlayInChat extends PacketIn {
}
public PacketPlayInChat(DataInputStream in) throws IOException {
this(DataTypeIO.readString(in));
this(DataTypeIO.readString(in, StandardCharsets.UTF_8));
}
public String getMessage() {

View File

@ -18,7 +18,7 @@ public class PacketPlayInPluginMessaging extends PacketIn {
}
public PacketPlayInPluginMessaging(DataInputStream in, int packetLength, int packetId) throws IOException {
String rawChannel = DataTypeIO.readString(in);
String rawChannel = DataTypeIO.readString(in, StandardCharsets.UTF_8);
channel = new NamespacedKey(rawChannel);
int dataLength = packetLength - DataTypeIO.getVarIntLength(packetId) - DataTypeIO.getStringLength(rawChannel, StandardCharsets.UTF_8);
data = new byte[dataLength];

View File

@ -2,6 +2,7 @@ package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
@ -16,7 +17,7 @@ public class PacketPlayInTabComplete extends PacketIn {
}
public PacketPlayInTabComplete(DataInputStream in) throws IOException {
this(DataTypeIO.readVarInt(in), DataTypeIO.readString(in));
this(DataTypeIO.readVarInt(in), DataTypeIO.readString(in, StandardCharsets.UTF_8));
}
public int getId() {

View File

@ -34,7 +34,7 @@ public class DataTypeIO {
out.write(b);
}
public static String readString(DataInputStream in) throws IOException {
public static String readString(DataInputStream in, Charset charset) throws IOException {
int length = readVarInt(in);
if (length == -1) {
@ -43,7 +43,7 @@ public class DataTypeIO {
byte[] b = new byte[length];
in.readFully(b);
return new String(b);
return new String(b, charset);
}
public static int getStringLength(String string, Charset charset) throws IOException {