Utf8String.java
net.minecraft.network.Utf8String
信息
- 全限定名:net.minecraft.network.Utf8String
- 类型:public class
- 包:net.minecraft.network
- 源码路径:src/main/java/net/minecraft/network/Utf8String.java
- 起始行号:L9
- 职责:
TODO
字段/常量
- 无
内部类/嵌套类型
- 无
构造器
- 无
方法
下面的方法块按源码顺序生成。
public static String read(ByteBuf input, int maxLength) @ L10
- 方法名:read
- 源码定位:L10
- 返回类型:String
- 修饰符:public static
参数:
- input: ByteBuf
- maxLength: int
说明:
TODO
public static void write(ByteBuf output, CharSequence value, int maxLength) @ L35
- 方法名:write
- 源码定位:L35
- 返回类型:void
- 修饰符:public static
参数:
- output: ByteBuf
- value: CharSequence
- maxLength: int
说明:
TODO
代码
public class Utf8String {
public static String read(ByteBuf input, int maxLength) {
int maxEncodedLength = ByteBufUtil.utf8MaxBytes(maxLength);
int bufferLength = VarInt.read(input);
if (bufferLength > maxEncodedLength) {
throw new DecoderException(
"The received encoded string buffer length is longer than maximum allowed (" + bufferLength + " > " + maxEncodedLength + ")"
);
} else if (bufferLength < 0) {
throw new DecoderException("The received encoded string buffer length is less than zero! Weird string!");
} else {
int availableBytes = input.readableBytes();
if (bufferLength > availableBytes) {
throw new DecoderException("Not enough bytes in buffer, expected " + bufferLength + ", but got " + availableBytes);
} else {
String result = input.toString(input.readerIndex(), bufferLength, StandardCharsets.UTF_8);
input.readerIndex(input.readerIndex() + bufferLength);
if (result.length() > maxLength) {
throw new DecoderException("The received string length is longer than maximum allowed (" + result.length() + " > " + maxLength + ")");
} else {
return result;
}
}
}
}
public static void write(ByteBuf output, CharSequence value, int maxLength) {
if (value.length() > maxLength) {
throw new EncoderException("String too big (was " + value.length() + " characters, max " + maxLength + ")");
} else {
int maxEncodedValueLength = ByteBufUtil.utf8MaxBytes(value);
ByteBuf tmp = output.alloc().buffer(maxEncodedValueLength);
try {
int bytesWritten = ByteBufUtil.writeUtf8(tmp, value);
int maxAllowedEncodedLength = ByteBufUtil.utf8MaxBytes(maxLength);
if (bytesWritten > maxAllowedEncodedLength) {
throw new EncoderException("String too big (was " + bytesWritten + " bytes encoded, max " + maxAllowedEncodedLength + ")");
}
VarInt.write(output, bytesWritten);
output.writeBytes(tmp);
} finally {
tmp.release();
}
}
}
}引用的其他类
- VarInt
- 引用位置:
方法调用 - 关联成员:
VarInt.read(), VarInt.write()
- 引用位置: