модернизания NetStream (extends Input/Output stream)
cherry-pick: d783317b5d
This commit is contained in:
@@ -1,22 +1,23 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-07-25
|
|
||||||
*/
|
|
||||||
package mc.core.network;
|
package mc.core.network;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public abstract class NetInputStream {
|
public abstract class NetInputStream extends InputStream {
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private int dataSize;
|
private int dataSize;
|
||||||
|
|
||||||
public abstract boolean readBoolean();
|
public abstract boolean readBoolean();
|
||||||
public abstract byte readByte();
|
public abstract byte readByte();
|
||||||
public abstract void readBytes(byte[] buffer);
|
public int readBytes(byte[] buffer) {
|
||||||
|
return readBytes(buffer, 0, buffer.length);
|
||||||
|
}
|
||||||
|
public abstract int readBytes(byte[] buffer, int offset, int length);
|
||||||
public abstract int readUnsignedByte();
|
public abstract int readUnsignedByte();
|
||||||
public abstract int readUnsignedShort();
|
public abstract int readUnsignedShort();
|
||||||
public abstract short readShort();
|
public abstract short readShort();
|
||||||
@@ -30,4 +31,25 @@ public abstract class NetInputStream {
|
|||||||
public abstract UUID readUUID();
|
public abstract UUID readUUID();
|
||||||
|
|
||||||
public abstract void skipBytes(int count);
|
public abstract void skipBytes(int count);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read() throws IOException {
|
||||||
|
return readByte();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read(byte[] b) throws IOException {
|
||||||
|
return readBytes(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read(byte[] b, int off, int len) throws IOException {
|
||||||
|
return readBytes(b, off, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long skip(long n) throws IOException {
|
||||||
|
skipBytes((int) n);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-07-25
|
|
||||||
*/
|
|
||||||
package mc.core.network;
|
package mc.core.network;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public abstract class NetOutputStream {
|
public abstract class NetOutputStream extends OutputStream {
|
||||||
public abstract void writeBoolean(boolean value);
|
public abstract void writeBoolean(boolean value);
|
||||||
public abstract void writeByte(int value);
|
public abstract void writeByte(int value);
|
||||||
public abstract void writeUnsignedByte(int value);
|
public abstract void writeUnsignedByte(int value);
|
||||||
public abstract void writeBytes(byte[] buffer);
|
public void writeBytes(byte[] buffer) {
|
||||||
|
writeBytes(buffer, 0, buffer.length);
|
||||||
|
}
|
||||||
|
public abstract void writeBytes(byte[] buffer, int offset, int lengtn);
|
||||||
public abstract void writeShort(int value);
|
public abstract void writeShort(int value);
|
||||||
public abstract void writeInt(int value);
|
public abstract void writeInt(int value);
|
||||||
public abstract void writeVarInt(int value);
|
public abstract void writeVarInt(int value);
|
||||||
@@ -19,4 +20,19 @@ public abstract class NetOutputStream {
|
|||||||
public abstract void writeDouble(double value);
|
public abstract void writeDouble(double value);
|
||||||
public abstract void writeString(String value);
|
public abstract void writeString(String value);
|
||||||
public abstract void writeUUID(UUID uuid);
|
public abstract void writeUUID(UUID uuid);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(int b) throws IOException {
|
||||||
|
writeByte(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(byte[] b) throws IOException {
|
||||||
|
writeBytes(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
|
writeBytes(b, off, len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-06-10
|
|
||||||
*/
|
|
||||||
package mc.core.network.proto_1_12_2;
|
package mc.core.network.proto_1_12_2;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -25,8 +21,8 @@ public class ByteArrayOutputNetStream extends NetOutputStream_p340 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeBytes(byte[] buffer) {
|
public void writeBytes(byte[] buffer, int offset, int lengtn) {
|
||||||
baos.write(buffer, 0, buffer.length);
|
baos.write(buffer, offset, lengtn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-07-25
|
|
||||||
*/
|
|
||||||
package mc.core.network.proto_1_12_2;
|
package mc.core.network.proto_1_12_2;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -22,8 +18,6 @@ public abstract class NetOutputStream_p340 extends NetOutputStream {
|
|||||||
writeByte(value);
|
writeByte(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeString(String value) {
|
public void writeString(String value) {
|
||||||
if (value.length() > Short.MAX_VALUE) {
|
if (value.length() > Short.MAX_VALUE) {
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package mc.core.network.proto_1_12_2.packets;
|
package mc.core.network.proto_1_12_2.packets;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import mc.core.network.proto_1_12_2.NetInputStream_p340;
|
import mc.core.network.proto_1_12_2.NetInputStream_p340;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class ByteArrayInputNetStream extends NetInputStream_p340 {
|
public class ByteArrayInputNetStream extends NetInputStream_p340 {
|
||||||
private ByteArrayInputStream bais;
|
private ByteArrayInputStream bais;
|
||||||
|
|
||||||
@@ -22,7 +25,17 @@ public class ByteArrayInputNetStream extends NetInputStream_p340 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readBytes(byte[] buffer) {
|
public int readBytes(byte[] buffer, int offset, int length) {
|
||||||
|
try {
|
||||||
|
int read = bais.read(buffer, offset, length);
|
||||||
|
if (read < length) {
|
||||||
|
throw new IOException("not enough data");
|
||||||
|
}
|
||||||
|
return read;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("", e);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -47,7 +60,7 @@ public class ByteArrayInputNetStream extends NetInputStream_p340 {
|
|||||||
int ch3 = bais.read();
|
int ch3 = bais.read();
|
||||||
int ch4 = bais.read();
|
int ch4 = bais.read();
|
||||||
if ((ch1 | ch2 | ch3 | ch4) < 0) return 0;
|
if ((ch1 | ch2 | ch3 | ch4) < 0) return 0;
|
||||||
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
|
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-07-25
|
|
||||||
*/
|
|
||||||
package mc.core.network.proto_1_12_2.netty.wrappers;
|
package mc.core.network.proto_1_12_2.netty.wrappers;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
@@ -25,8 +21,9 @@ public class WrapperNetInputStream extends NetInputStream_p340 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readBytes(byte[] buffer) {
|
public int readBytes(byte[] buffer, int offset, int length) {
|
||||||
byteBuf.readBytes(buffer);
|
byteBuf.readBytes(buffer, offset, length);
|
||||||
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2018-07-25
|
|
||||||
*/
|
|
||||||
package mc.core.network.proto_1_12_2.netty.wrappers;
|
package mc.core.network.proto_1_12_2.netty.wrappers;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
@@ -30,8 +26,8 @@ public class WrapperNetOutputStream extends NetOutputStream_p340 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeBytes(byte[] buffer) {
|
public void writeBytes(byte[] buffer, int offset, int lengtn) {
|
||||||
byteBuf.writeBytes(buffer);
|
byteBuf.writeBytes(buffer, offset, lengtn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user