Archived
0

NetStream_p125

This commit is contained in:
2018-04-14 00:04:16 +03:00
parent 354c91cbfa
commit c4ad074bb3
4 changed files with 51 additions and 87 deletions

View File

@@ -6,82 +6,23 @@ package mc.core.network;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@Slf4j
public abstract class NetStream {
@Getter
@Setter
private int expectedSize;
public static int sizeVarInt(final int value) {
byte size = 0;
int v = value;
do {
v >>>= 7;
size++;
} while (v != 0);
return size;
}
public int readVarInt() {
int result = 0;
byte read;
byte numRead = 0;
do {
read = readByte();
int value = (read & 0b01111111);
result |= (value << (7 * numRead));
numRead++;
if (numRead > 5) {
log.debug("VarInt is too big!");
break;
}
} while ((read & 0b10000000) != 0);
return result;
}
public void writeVarInt(int value) {
do {
byte temp = (byte)(value & 0b01111111);
value >>>= 7;
if (value != 0) {
temp |= 0b10000000;
}
writeByte(temp);
} while (value != 0);
}
public String readString() {
int length = readVarInt();
byte[] buffer = new byte[length];
readBytes(buffer);
return new String(buffer, StandardCharsets.UTF_8);
}
public void writeString(String value) {
writeVarInt(value.length());
writeBytes(value.getBytes());
}
public abstract byte readByte();
public abstract void readBytes(byte[] buffer);
public abstract int readUnsignedByte();
public abstract int readUnsignedShort();
public abstract int readInt();
public abstract String readString();
public abstract void writeByte(int value);
public abstract void writeBytes(byte[] buffer);
public abstract void writeInt(int value);
public abstract void writeString(String value);
public abstract void skipBytes(int count);
}

View File

@@ -11,23 +11,9 @@ import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
@Slf4j
public class ByteArrayOutputNetStream extends NetStream {
public class ByteArrayOutputNetStream extends NetStream_p125 {
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
@Override
public void writeString(String value) {
if (value.length() > 240) {
log.warn("String \"{}\" too long!", value);
byte[] buf = value.substring(0, 240).getBytes(StandardCharsets.UTF_16BE);
writeByte(240);
writeBytes(buf);
} else {
byte[] buf = value.getBytes(StandardCharsets.UTF_16BE);
writeByte(value.length());
writeBytes(buf);
}
}
@Override
public byte readByte() {
throw new UnsupportedOperationException();
@@ -38,6 +24,11 @@ public class ByteArrayOutputNetStream extends NetStream {
throw new UnsupportedOperationException();
}
@Override
public int readUnsignedByte() {
throw new UnsupportedOperationException();
}
@Override
public int readUnsignedShort() {
throw new UnsupportedOperationException();

View File

@@ -0,0 +1,35 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-13
*/
package mc.core.network.proto_125;
import lombok.extern.slf4j.Slf4j;
import mc.core.network.NetStream;
import java.nio.charset.StandardCharsets;
@Slf4j
public abstract class NetStream_p125 extends NetStream {
@Override
public String readString() {
int size = readUnsignedByte() * 2;
byte[] bytes = new byte[size];
readBytes(bytes);
return new String(bytes, StandardCharsets.UTF_16BE);
}
@Override
public void writeString(String value) {
if (value.length() > 240) {
log.warn("String \"{}\" too long!", value);
byte[] buf = value.substring(0, 240).getBytes(StandardCharsets.UTF_16BE);
writeByte(240);
writeBytes(buf);
} else {
byte[] buf = value.getBytes(StandardCharsets.UTF_16BE);
writeByte(value.length());
writeBytes(buf);
}
}
}

View File

@@ -8,22 +8,14 @@ import io.netty.buffer.ByteBuf;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mc.core.network.NetStream;
import mc.core.network.proto_125.NetStream_p125;
import java.nio.charset.StandardCharsets;
@Slf4j
@RequiredArgsConstructor
public class WrapperNetStream extends NetStream {
public class WrapperNetStream extends NetStream_p125 {
private final ByteBuf byteBuf;
@Override
public String readString() {
int size = byteBuf.readUnsignedByte() * 2;
byte[] bytes = new byte[size];
byteBuf.readBytes(bytes);
return new String(bytes, StandardCharsets.UTF_16BE);
}
@Override
public byte readByte() {
return byteBuf.readByte();
@@ -34,6 +26,11 @@ public class WrapperNetStream extends NetStream {
byteBuf.readBytes(buffer);
}
@Override
public int readUnsignedByte() {
return byteBuf.readUnsignedByte();
}
@Override
public int readUnsignedShort() {
return byteBuf.readUnsignedShort();