Refactor packet string charset

This commit is contained in:
P0nk
2021-06-21 22:03:20 +02:00
parent c4c5700d32
commit 72ff8d563b
4 changed files with 9 additions and 12 deletions

View File

@@ -47,12 +47,9 @@ public class ByteBufInPacket implements InPacket {
@Override
public String readString() {
short length = readShort();
char[] characters = new char[length];
for(int i = 0; i < length; i++) {
characters[i] = (char) readByte();
}
return String.valueOf(characters);
byte[] stringBytes = new byte[length];
byteBuf.readBytes(stringBytes);
return new String(stringBytes, STRING_CHARSET);
}
@Override

View File

@@ -1,12 +1,8 @@
package net.packet;
import java.awt.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public interface OutPacket extends Packet {
Charset STRING_CHARSET = StandardCharsets.US_ASCII;
void writeByte(byte value);
void writeByte(int value);
void writeBytes(byte[] value);

View File

@@ -1,5 +1,10 @@
package net.packet;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public interface Packet {
Charset STRING_CHARSET = StandardCharsets.US_ASCII;
byte[] getBytes();
}

View File

@@ -6,7 +6,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.awt.*;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -82,7 +81,7 @@ class ByteBufInPacketTest {
void readString() {
final String writtenString = "You have gained experience (+3200)";
byteBuf.writeShortLE(writtenString.length());
byte[] writtenStringBytes = writtenString.getBytes(StandardCharsets.US_ASCII);
byte[] writtenStringBytes = writtenString.getBytes(Packet.STRING_CHARSET);
byteBuf.writeBytes(writtenStringBytes);
String readString = inPacket.readString();