From 6bb9a14cd1547684d0cdaa0d14089860d7c1da7c Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 1 Feb 2013 21:33:31 +1100 Subject: [PATCH] Require a buffer to be passed along. --- .../net/md_5/mendax/datainput/BulkChunk.java | 4 ++-- .../net/md_5/mendax/datainput/ByteHeader.java | 4 ++-- .../mendax/datainput/DataInputPacketReader.java | 4 ++-- .../net/md_5/mendax/datainput/Instruction.java | 8 +++----- .../net/md_5/mendax/datainput/IntHeader.java | 4 ++-- .../java/net/md_5/mendax/datainput/Item.java | 6 +++--- .../java/net/md_5/mendax/datainput/Jump.java | 4 ++-- .../java/net/md_5/mendax/datainput/MetaData.java | 16 ++++++++-------- .../md_5/mendax/datainput/OptionalMotion.java | 4 ++-- .../net/md_5/mendax/datainput/ShortHeader.java | 4 ++-- .../md_5/mendax/datainput/UnsignedShortByte.java | 4 ++-- 11 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/main/java/net/md_5/mendax/datainput/BulkChunk.java b/src/main/java/net/md_5/mendax/datainput/BulkChunk.java index 4e2de002..a3a9d87c 100644 --- a/src/main/java/net/md_5/mendax/datainput/BulkChunk.java +++ b/src/main/java/net/md_5/mendax/datainput/BulkChunk.java @@ -6,10 +6,10 @@ import java.io.IOException; public class BulkChunk extends Instruction { @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { short count = in.readShort(); int size = in.readInt(); in.readBoolean(); - skip(in, size + count * 12); + skip(in, buffer, size + count * 12); } } diff --git a/src/main/java/net/md_5/mendax/datainput/ByteHeader.java b/src/main/java/net/md_5/mendax/datainput/ByteHeader.java index c9508e00..a67d41c9 100644 --- a/src/main/java/net/md_5/mendax/datainput/ByteHeader.java +++ b/src/main/java/net/md_5/mendax/datainput/ByteHeader.java @@ -12,10 +12,10 @@ class ByteHeader extends Instruction { } @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { byte size = in.readByte(); for (byte b = 0; b < size; b++) { - child.read(in); + child.read(in, buffer); } } } diff --git a/src/main/java/net/md_5/mendax/datainput/DataInputPacketReader.java b/src/main/java/net/md_5/mendax/datainput/DataInputPacketReader.java index 0bb1b092..b5e9d42b 100644 --- a/src/main/java/net/md_5/mendax/datainput/DataInputPacketReader.java +++ b/src/main/java/net/md_5/mendax/datainput/DataInputPacketReader.java @@ -47,7 +47,7 @@ public class DataInputPacketReader { } } - public static void readPacket(DataInput in) throws IOException { + public static void readPacket(DataInput in, byte[] buffer) throws IOException { int packetId = in.readUnsignedByte(); Instruction[] packetDef = instructions[packetId]; @@ -56,7 +56,7 @@ public class DataInputPacketReader { } for (Instruction instruction : packetDef) { - instruction.read(in); + instruction.read(in, buffer); } } } diff --git a/src/main/java/net/md_5/mendax/datainput/Instruction.java b/src/main/java/net/md_5/mendax/datainput/Instruction.java index 9c4e1a17..c91cd277 100644 --- a/src/main/java/net/md_5/mendax/datainput/Instruction.java +++ b/src/main/java/net/md_5/mendax/datainput/Instruction.java @@ -25,12 +25,10 @@ abstract class Instruction { static final Instruction USHORT_BYTE = new UnsignedShortByte(); // Illegal forward references below this line static final Instruction BYTE_INT = new ByteHeader(INT); - // Buffer used to read all skips, make sure it is sufficiently large (1mb packet at the moment) - static final byte[] buf = new byte[1 << 20]; - abstract void read(DataInput in) throws IOException; + abstract void read(DataInput in, byte[] buffer) throws IOException; - final void skip(DataInput in, int len) throws IOException { - in.readFully(buf, 0, len); + final void skip(DataInput in, byte[] buffer, int len) throws IOException { + in.readFully(buffer, 0, len); } } diff --git a/src/main/java/net/md_5/mendax/datainput/IntHeader.java b/src/main/java/net/md_5/mendax/datainput/IntHeader.java index 8f90e4c6..c113ea7b 100644 --- a/src/main/java/net/md_5/mendax/datainput/IntHeader.java +++ b/src/main/java/net/md_5/mendax/datainput/IntHeader.java @@ -12,10 +12,10 @@ class IntHeader extends Instruction { } @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { int size = in.readInt(); for (int i = 0; i < size; i++) { - child.read(in); + child.read(in, buffer); } } } diff --git a/src/main/java/net/md_5/mendax/datainput/Item.java b/src/main/java/net/md_5/mendax/datainput/Item.java index b3e97b18..f32cb4f0 100644 --- a/src/main/java/net/md_5/mendax/datainput/Item.java +++ b/src/main/java/net/md_5/mendax/datainput/Item.java @@ -6,11 +6,11 @@ import java.io.IOException; class Item extends Instruction { @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { short type = in.readShort(); if (type >= 0) { - skip(in, 3); - SHORT_BYTE.read(in); + skip(in, buffer, 3); + SHORT_BYTE.read(in, buffer); } } } diff --git a/src/main/java/net/md_5/mendax/datainput/Jump.java b/src/main/java/net/md_5/mendax/datainput/Jump.java index a9d2b7f3..23c61d3a 100644 --- a/src/main/java/net/md_5/mendax/datainput/Jump.java +++ b/src/main/java/net/md_5/mendax/datainput/Jump.java @@ -15,7 +15,7 @@ class Jump extends Instruction { } @Override - void read(DataInput in) throws IOException { - skip(in, len); + void read(DataInput in, byte[] buffer) throws IOException { + skip(in, buffer, len); } } diff --git a/src/main/java/net/md_5/mendax/datainput/MetaData.java b/src/main/java/net/md_5/mendax/datainput/MetaData.java index bc03f4e6..78eb2c11 100644 --- a/src/main/java/net/md_5/mendax/datainput/MetaData.java +++ b/src/main/java/net/md_5/mendax/datainput/MetaData.java @@ -6,31 +6,31 @@ import java.io.IOException; class MetaData extends Instruction { @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { int x = in.readUnsignedByte(); while (x != 127) { int type = x >> 5; switch (type) { case 0: - BYTE.read(in); + BYTE.read(in, buffer); break; case 1: - SHORT.read(in); + SHORT.read(in, buffer); break; case 2: - INT.read(in); + INT.read(in, buffer); break; case 3: - FLOAT.read(in); + FLOAT.read(in, buffer); break; case 4: - STRING.read(in); + STRING.read(in, buffer); break; case 5: - ITEM.read(in); + ITEM.read(in, buffer); break; case 6: - skip(in, 12); // int, int, int + skip(in, buffer, 12); // int, int, int break; default: throw new IllegalArgumentException("Unknown metadata type " + type); diff --git a/src/main/java/net/md_5/mendax/datainput/OptionalMotion.java b/src/main/java/net/md_5/mendax/datainput/OptionalMotion.java index c1181c84..4a8f8018 100644 --- a/src/main/java/net/md_5/mendax/datainput/OptionalMotion.java +++ b/src/main/java/net/md_5/mendax/datainput/OptionalMotion.java @@ -6,10 +6,10 @@ import java.io.IOException; public class OptionalMotion extends Instruction { @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { int data = in.readInt(); if (data > 0) { - skip(in, 6); + skip(in, buffer, 6); } } } diff --git a/src/main/java/net/md_5/mendax/datainput/ShortHeader.java b/src/main/java/net/md_5/mendax/datainput/ShortHeader.java index d77c2657..442a872b 100644 --- a/src/main/java/net/md_5/mendax/datainput/ShortHeader.java +++ b/src/main/java/net/md_5/mendax/datainput/ShortHeader.java @@ -12,10 +12,10 @@ class ShortHeader extends Instruction { } @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { short size = in.readShort(); for (short s = 0; s < size; s++) { - child.read(in); + child.read(in, buffer); } } } diff --git a/src/main/java/net/md_5/mendax/datainput/UnsignedShortByte.java b/src/main/java/net/md_5/mendax/datainput/UnsignedShortByte.java index ec881294..70b4448f 100644 --- a/src/main/java/net/md_5/mendax/datainput/UnsignedShortByte.java +++ b/src/main/java/net/md_5/mendax/datainput/UnsignedShortByte.java @@ -6,8 +6,8 @@ import java.io.IOException; public class UnsignedShortByte extends Instruction { @Override - void read(DataInput in) throws IOException { + void read(DataInput in, byte[] buffer) throws IOException { int size = in.readUnsignedShort(); - skip(in, size); + skip(in, buffer, size); } }