Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-09-06 05:00:09 +02:00
Merge pull request #1576 from astei/remove-unneeded-indirection
Remove unneeded indirection
Dieser Commit ist enthalten in:
Commit
d5907711e1
@ -1,14 +1,15 @@
|
||||
package us.myles.ViaVersion.api.type.types;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Preconditions;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import us.myles.ViaVersion.api.type.Type;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class StringType extends Type<String> {
|
||||
// String#length() (used to limit the string in Minecraft source code) uses char[]#length
|
||||
private static final int maxJavaCharUtf8Length = Character.toString(Character.MAX_VALUE)
|
||||
.getBytes(Charsets.UTF_8).length;
|
||||
.getBytes(StandardCharsets.UTF_8).length;
|
||||
|
||||
public StringType() {
|
||||
super(String.class);
|
||||
@ -21,9 +22,9 @@ public class StringType extends Type<String> {
|
||||
Preconditions.checkArgument(len <= Short.MAX_VALUE * maxJavaCharUtf8Length,
|
||||
"Cannot receive string longer than Short.MAX_VALUE * " + maxJavaCharUtf8Length + " bytes (got %s bytes)", len);
|
||||
|
||||
byte[] b = new byte[len];
|
||||
buffer.readBytes(b);
|
||||
String string = new String(b, Charsets.UTF_8);
|
||||
String string = buffer.toString(buffer.readerIndex(), len, StandardCharsets.UTF_8);
|
||||
buffer.skipBytes(len);
|
||||
|
||||
Preconditions.checkArgument(string.length() <= Short.MAX_VALUE,
|
||||
"Cannot receive string longer than Short.MAX_VALUE characters (got %s bytes)", string.length());
|
||||
|
||||
@ -34,7 +35,7 @@ public class StringType extends Type<String> {
|
||||
public void write(ByteBuf buffer, String object) throws Exception {
|
||||
Preconditions.checkArgument(object.length() <= Short.MAX_VALUE, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", object.length());
|
||||
|
||||
byte[] b = object.getBytes(Charsets.UTF_8);
|
||||
byte[] b = object.getBytes(StandardCharsets.UTF_8);
|
||||
Type.VAR_INT.write(buffer, b.length);
|
||||
buffer.writeBytes(b);
|
||||
}
|
||||
|
@ -36,10 +36,7 @@ public class NBTType extends Type<CompoundTag> {
|
||||
return null;
|
||||
} else {
|
||||
buffer.readerIndex(readerIndex);
|
||||
ByteBufInputStream bytebufStream = new ByteBufInputStream(buffer);
|
||||
try (DataInputStream dataInputStream = new DataInputStream(bytebufStream)) {
|
||||
return (CompoundTag) NBTIO.readTag((DataInput) dataInputStream);
|
||||
}
|
||||
return (CompoundTag) NBTIO.readTag((DataInput) new ByteBufInputStream(buffer));
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,11 +46,7 @@ public class NBTType extends Type<CompoundTag> {
|
||||
buffer.writeByte(0);
|
||||
} else {
|
||||
ByteBufOutputStream bytebufStream = new ByteBufOutputStream(buffer);
|
||||
DataOutputStream dataOutputStream = new DataOutputStream(bytebufStream);
|
||||
|
||||
NBTIO.writeTag((DataOutput) dataOutputStream, object);
|
||||
|
||||
dataOutputStream.close();
|
||||
NBTIO.writeTag((DataOutput) bytebufStream, object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren