From db4abfe4867570cc5b3426fe26bf9e3f58c4906e Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 5 Jul 2013 19:17:25 +1000 Subject: [PATCH] Expand streams a little bit --- .../md_5/bungee/protocol/MinecraftInput.java | 38 ++++++++++++ .../md_5/bungee/protocol/MinecraftOutput.java | 50 +++++++++++++++ .../md_5/bungee/protocol/MinecraftStream.java | 62 ------------------- .../packet/PacketFAPluginMessage.java | 7 ++- .../bungee/connection/InitialHandler.java | 5 +- 5 files changed, 94 insertions(+), 68 deletions(-) create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/MinecraftInput.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/MinecraftOutput.java delete mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/MinecraftStream.java diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftInput.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftInput.java new file mode 100644 index 00000000..02036adf --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftInput.java @@ -0,0 +1,38 @@ +package net.md_5.bungee.protocol; + +import io.netty.buffer.ByteBuf; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class MinecraftInput +{ + + private final ByteBuf buf; + + public byte readByte() + { + return buf.readByte(); + } + + public short readUnisgnedByte() + { + return buf.readUnsignedByte(); + } + + public int readInt() + { + return buf.readInt(); + } + + public String readString() + { + short len = buf.readShort(); + char[] c = new char[ len ]; + for ( int i = 0; i < c.length; i++ ) + { + c[i] = buf.readChar(); + } + + return new String( c ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftOutput.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftOutput.java new file mode 100644 index 00000000..00998f81 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftOutput.java @@ -0,0 +1,50 @@ +package net.md_5.bungee.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.util.Arrays; + +public class MinecraftOutput +{ + + private final ByteBuf buf; + + public MinecraftOutput() + { + buf = Unpooled.buffer(); + } + + public byte[] toArray() + { + if ( buf.hasArray() ) + { + return Arrays.copyOfRange( buf.array(), buf.arrayOffset(), buf.arrayOffset() + buf.writerIndex() ); + } else + { + byte[] b = new byte[ buf.writerIndex() ]; + buf.readBytes( b ); + return b; + } + } + + public MinecraftOutput writeByte(byte b) + { + buf.writeByte( b ); + return this; + } + + public void writeInt(int i) + { + buf.writeInt( i ); + } + + public void writeString(String s) + { + char[] cc = s.toCharArray(); + buf.writeShort( cc.length ); + for ( char c : cc ) + { + buf.writeChar( c ); + } + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftStream.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftStream.java deleted file mode 100644 index 482329d6..00000000 --- a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftStream.java +++ /dev/null @@ -1,62 +0,0 @@ -package net.md_5.bungee.protocol; - -import io.netty.buffer.ByteBuf; -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -public class MinecraftStream -{ - - private final ByteBuf buf; - - public byte readByte() - { - return buf.readByte(); - } - - public MinecraftStream writeByte(byte b) - { - buf.writeByte( b ); - return this; - } - /*========================================================================*/ - - public short readUnisgnedByte() - { - return buf.readUnsignedByte(); - } - - /*========================================================================*/ - public int readInt() - { - return buf.readInt(); - } - - public void writeInt(int i) - { - buf.writeInt( i ); - } - /*========================================================================*/ - - public String readString() - { - short len = buf.readShort(); - char[] c = new char[ len ]; - for ( int i = 0; i < c.length; i++ ) - { - c[i] = buf.readChar(); - } - - return new String( c ); - } - - public void writeString(String s) - { - char[] cc = s.toCharArray(); - buf.writeShort( cc.length ); - for ( char c : cc ) - { - buf.writeChar( c ); - } - } -} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java index d7c14e98..d2eef89b 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java @@ -8,7 +8,8 @@ import java.io.DataInputStream; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; -import net.md_5.bungee.protocol.MinecraftStream; +import net.md_5.bungee.protocol.MinecraftInput; +import net.md_5.bungee.protocol.MinecraftOutput; @Getter @ToString @@ -56,8 +57,8 @@ public class PacketFAPluginMessage extends DefinedPacket return new DataInputStream( new ByteArrayInputStream( data ) ); } - public MinecraftStream getMCStream() + public MinecraftInput getMCStream() { - return new MinecraftStream( Unpooled.wrappedBuffer( data ) ); + return new MinecraftInput( Unpooled.wrappedBuffer( data ) ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 4cb8a83d..8a193431 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -2,7 +2,6 @@ package net.md_5.bungee.connection; import com.google.common.base.Preconditions; import io.netty.util.concurrent.ScheduledFuture; -import java.io.DataInput; import java.math.BigInteger; import java.net.InetSocketAddress; import java.net.URLEncoder; @@ -39,7 +38,7 @@ import net.md_5.bungee.netty.PacketDecoder; import net.md_5.bungee.netty.PacketHandler; import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.protocol.Forge; -import net.md_5.bungee.protocol.MinecraftStream; +import net.md_5.bungee.protocol.MinecraftInput; import net.md_5.bungee.protocol.Vanilla; import net.md_5.bungee.protocol.packet.DefinedPacket; import net.md_5.bungee.protocol.packet.Packet1Login; @@ -109,7 +108,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection { if ( pingFuture.cancel( false ) ) { - MinecraftStream in = pluginMessage.getMCStream(); + MinecraftInput in = pluginMessage.getMCStream(); version = in.readByte(); String connectHost = in.readString(); int connectPort = in.readInt();