Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed any local memory from Field classes so they can be called fro…

…m multiple threads
  • Loading branch information...
commit e0e0fbea5b855ccdf0b584d153a7fec0b87c30be 1 parent 71e535b
@Raphfrk authored
Showing with 97 additions and 138 deletions.
  1. +6 −2 src/main/java/com/raphfrk/craftproxylib/MCInputStream.java
  2. +8 −0 src/main/java/com/raphfrk/craftproxylib/MCSocket.java
  3. +6 −23 src/main/java/com/raphfrk/craftproxylib/fields/Field.java
  4. +5 −3 src/main/java/com/raphfrk/craftproxylib/fields/FieldFixedLength.java
  5. +3 −3 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldChar.java
  6. +3 −3 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldDouble.java
  7. +1 −1  src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldEntityId.java
  8. +3 −3 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldFloat.java
  9. +2 −6 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedByteArray.java
  10. +3 −7 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedByteArrayWithInt.java
  11. +2 −6 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedItemArray.java
  12. +2 −6 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedTripleByteArray.java
  13. +7 −8 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldInteger.java
  14. +6 −10 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldItem.java
  15. +10 −11 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldLong.java
  16. +10 −14 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldMetadata.java
  17. +3 −5 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldShort.java
  18. +2 −6 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldShortSizedByteArray.java
  19. +9 −9 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldString.java
  20. +5 −11 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldVelocity.java
  21. +1 −1  src/main/java/com/raphfrk/craftproxylib/packet/Packet.java
View
8 src/main/java/com/raphfrk/craftproxylib/MCInputStream.java
@@ -88,8 +88,7 @@ public Packet readPacket(Packet p) throws SocketTimeoutException, IOException {
Field[] fields = Field.getCompressedFields(id);
if (fields == null) {
- int[] oldIds = packetIds.read();
- System.out.println("Packet ids: " + Arrays.toString(oldIds));
+ printRecentPacketIds();
throw new IOException("Unknown packet id 0x" + Integer.toHexString(id));
}
@@ -110,6 +109,11 @@ public Packet readPacket(Packet p) throws SocketTimeoutException, IOException {
return r;
}
+ public void printRecentPacketIds() {
+ int[] oldIds = packetIds.read();
+ System.out.println("Packet ids: " + Arrays.toString(oldIds));
+ }
+
public void setServerEntityId(int entityId) {
byte[] newEntityId = new byte[4];
FieldInteger.writeInt(newEntityId, 0, entityId);
View
8 src/main/java/com/raphfrk/craftproxylib/MCSocket.java
@@ -1,11 +1,13 @@
package com.raphfrk.craftproxylib;
+import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
+import java.util.Arrays;
import com.raphfrk.craftproxylib.fields.Field;
import com.raphfrk.craftproxylib.packet.Packet;
@@ -123,7 +125,9 @@ public Packet readPacket(Packet p) throws IOException {
if (in == null) {
throw new IOException("Input stream was not correctly opened");
}
+
p = in.readPacket(p);
+
if (p.getId() == 0) {
if (!server) {
Integer i = (Integer)Field.read(p, 0);
@@ -212,6 +216,10 @@ public void close(String message) throws IOException {
}
}
+ public void printRecentPacketIds() {
+ in.printRecentPacketIds();
+ }
+
public boolean isClosed() {
return socket.isClosed();
}
View
29 src/main/java/com/raphfrk/craftproxylib/fields/Field.java
@@ -228,16 +228,6 @@ private static void compressFieldMap() {
return entityIdInfo[i];
}
- protected final byte[] buffer;
-
- public Field() {
- buffer = null;
- }
-
- public Field(int bufferSize) {
- this.buffer = new byte[bufferSize];
- }
-
/**
* Skips over a packet field from an InputStream.<br>
*
@@ -356,19 +346,12 @@ public static String fieldArrayToString(Field[] fields) {
return sb.toString();
}
- public static void fillBuffer(byte[] buffer, InputStream in) throws IOException {
- if (buffer == null) {
- throw new NullPointerException("Buffer is null");
- }
-
- int i = 0;
- while (i < buffer.length) {
- int b = in.read(buffer, i, buffer.length - i);
- if (b == -1) {
- throw new EOFException("EOF reached");
- } else {
- i += b;
- }
+ public static int getByte(InputStream in) throws IOException {
+ int b = in.read();
+ if (b == -1) {
+ throw new EOFException("EOF reached");
+ } else {
+ return b;
}
}
View
8 src/main/java/com/raphfrk/craftproxylib/fields/FieldFixedLength.java
@@ -7,19 +7,21 @@
* Represents a fixed length field
*/
public class FieldFixedLength extends Field {
+
+ private final int length;
public FieldFixedLength(int length) {
- super(length);
+ this.length = length;
}
@Override
public int getFixedLength() {
- return buffer.length;
+ return length;
}
@Override
public int skip(InputStream in) throws IOException {
- return (int)in.skip(buffer.length);
+ return (int)in.skip(length);
}
@Override
View
6 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldChar.java
@@ -11,13 +11,13 @@ public FieldChar() {
super(2);
}
- public static char readCharacter(InputStream in, byte[] buffer) throws IOException {
- return (char)FieldShort.readShort(in, buffer);
+ public static char readCharacter(InputStream in) throws IOException {
+ return (char)FieldShort.readShort(in);
}
@Override
public Character read(InputStream in) throws IOException {
- return readCharacter(in, buffer);
+ return readCharacter(in);
}
public static int writeCharacter(byte[] buf, int pos, char i) {
View
6 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldDouble.java
@@ -11,16 +11,16 @@ public FieldDouble() {
super(8);
}
- public static double readDouble(InputStream in, byte[] buffer) throws IOException {
+ public static double readDouble(InputStream in) throws IOException {
- long longValue = FieldLong.readLong(in, buffer);
+ long longValue = FieldLong.readLong(in);
return Double.longBitsToDouble(longValue);
}
@Override
public Double read(InputStream in) throws IOException {
- return readDouble(in, buffer);
+ return readDouble(in);
}
public static int writeDouble(byte[] buf, int pos, double i) {
View
2  src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldEntityId.java
@@ -13,7 +13,7 @@ public FieldEntityId() {
@Override
public Integer read(InputStream in) throws IOException {
- return FieldInteger.readInt(in, buffer);
+ return FieldInteger.readInt(in);
}
@Override
View
6 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldFloat.java
@@ -11,16 +11,16 @@ public FieldFloat() {
super(4);
}
- public static float readFloat(InputStream in, byte[] buffer) throws IOException {
+ public static float readFloat(InputStream in) throws IOException {
- int intValue = FieldInteger.readInt(in, buffer);
+ int intValue = FieldInteger.readInt(in);
return Float.intBitsToFloat(intValue);
}
@Override
public Float read(InputStream in) throws IOException {
- return readFloat(in, buffer);
+ return readFloat(in);
}
public static int writeFloat(byte[] buf, int pos, float i) {
View
8 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedByteArray.java
@@ -7,21 +7,17 @@
import com.raphfrk.craftproxylib.fields.Field;
public class FieldIntSizedByteArray extends Field {
-
- public FieldIntSizedByteArray() {
- super(4);
- }
@Override
public int skip(InputStream in) throws IOException {
- int length = FieldInteger.readInt(in, buffer);
+ int length = FieldInteger.readInt(in);
return 4 + (int)in.skip(length);
}
@Override
public byte[] read(InputStream in) throws IOException {
- int length = FieldInteger.readInt(in, buffer);
+ int length = FieldInteger.readInt(in);
byte[] buf = new byte[length];
int i = 0;
View
10 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedByteArrayWithInt.java
@@ -8,14 +8,10 @@
import com.raphfrk.craftproxylib.fields.values.ByteArrayWithInt;
public class FieldIntSizedByteArrayWithInt extends Field {
-
- public FieldIntSizedByteArrayWithInt() {
- super(4);
- }
@Override
public int skip(InputStream in) throws IOException {
- int length = FieldInteger.readInt(in, buffer);
+ int length = FieldInteger.readInt(in);
in.skip(4);
return 8 + (int)in.skip(length);
@@ -23,9 +19,9 @@ public int skip(InputStream in) throws IOException {
@Override
public ByteArrayWithInt read(InputStream in) throws IOException {
- int length = FieldInteger.readInt(in, buffer);
+ int length = FieldInteger.readInt(in);
- int d = FieldInteger.readInt(in, buffer);
+ int d = FieldInteger.readInt(in);
byte[] buf = new byte[length];
int i = 0;
View
8 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedItemArray.java
@@ -9,14 +9,10 @@
public class FieldIntSizedItemArray extends Field {
private FieldItem fItem = new FieldItem();
-
- public FieldIntSizedItemArray() {
- super(2);
- }
@Override
public int skip(InputStream in) throws IOException {
- int length = FieldShort.readShort(in, buffer) & 0xFFFF;
+ int length = FieldShort.readShort(in) & 0xFFFF;
int skipped = 4;
for (int i = 0; i < length; i++) {
@@ -28,7 +24,7 @@ public int skip(InputStream in) throws IOException {
@Override
public Item[] read(InputStream in) throws IOException {
- int length = FieldShort.readShort(in, buffer) & 0xFFFF;
+ int length = FieldShort.readShort(in) & 0xFFFF;
Item[] itemArray = new Item[length];
View
8 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldIntSizedTripleByteArray.java
@@ -7,14 +7,10 @@
import com.raphfrk.craftproxylib.fields.Field;
public class FieldIntSizedTripleByteArray extends Field {
-
- public FieldIntSizedTripleByteArray() {
- super(4);
- }
@Override
public int skip(InputStream in) throws IOException {
- int length = FieldInteger.readInt(in, buffer);
+ int length = FieldInteger.readInt(in);
length += length << 1;
@@ -23,7 +19,7 @@ public int skip(InputStream in) throws IOException {
@Override
public byte[] read(InputStream in) throws IOException {
- int length = FieldInteger.readInt(in, buffer);
+ int length = FieldInteger.readInt(in);
length += length << 1;
View
15 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldInteger.java
@@ -11,20 +11,19 @@ public FieldInteger() {
super(4);
}
- public static int readInt(InputStream in, byte[] buffer) throws IOException {
- fillBuffer(buffer, in);
-
+ public static int readInt(InputStream in) throws IOException {
+
return
- ((buffer[0] & 0xFF) << 24) |
- ((buffer[1] & 0xFF) << 16) |
- ((buffer[2] & 0xFF) << 8) |
- ((buffer[3] & 0xFF) << 0);
+ (getByte(in) << 24) |
+ (getByte(in) << 16) |
+ (getByte(in) << 8) |
+ (getByte(in) << 0);
}
@Override
public Integer read(InputStream in) throws IOException {
- return readInt(in, buffer);
+ return readInt(in);
}
public static int writeInt(byte[] buf, int pos, int i) {
View
16 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldItem.java
@@ -71,14 +71,10 @@
enchanted[i] = enchantedItemsIds.contains(i);
}
}
-
- public FieldItem() {
- super(2);
- }
@Override
public int skip(InputStream in) throws IOException {
- int id = FieldShort.readShort(in, buffer);
+ int id = FieldShort.readShort(in);
if (id >= 0) {
int count = in.read();
@@ -86,10 +82,10 @@ public int skip(InputStream in) throws IOException {
throw new EOFException("EOF reached");
}
- FieldShort.readShort(in, buffer);
+ FieldShort.readShort(in);
if (enchanted[id]) {
- int length = FieldShort.readShort(in, buffer);
+ int length = FieldShort.readShort(in);
in.skip(length);
return length + 7;
} else {
@@ -102,7 +98,7 @@ public int skip(InputStream in) throws IOException {
@Override
public Item read(InputStream in) throws IOException {
- short id = FieldShort.readShort(in, buffer);
+ short id = FieldShort.readShort(in);
if (id >= 0) {
int count = in.read();
@@ -110,10 +106,10 @@ public Item read(InputStream in) throws IOException {
throw new EOFException("EOF reached");
}
- short damage = FieldShort.readShort(in, buffer);
+ short damage = FieldShort.readShort(in);
if (enchanted[id]) {
- int length = FieldShort.readShort(in, buffer);
+ int length = FieldShort.readShort(in);
byte[] enchant = new byte[length];
int i = 0;
while (i < length) {
View
21 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldLong.java
@@ -11,24 +11,23 @@ public FieldLong() {
super(8);
}
- public static long readLong(InputStream in, byte[] buffer) throws IOException {
- fillBuffer(buffer, in);
+ public static long readLong(InputStream in) throws IOException {
return
- ((buffer[0]) << 56) |
- ((buffer[1] & 0xFF) << 48) |
- ((buffer[2] & 0xFF) << 40) |
- ((buffer[3] & 0xFF) << 32) |
- ((buffer[4] & 0xFF) << 24) |
- ((buffer[5] & 0xFF) << 16) |
- ((buffer[6] & 0xFF) << 8) |
- ((buffer[7] & 0xFF) << 0);
+ (getByte(in) << 56) |
+ (getByte(in) << 48) |
+ (getByte(in) << 40) |
+ (getByte(in) << 32) |
+ (getByte(in) << 24) |
+ (getByte(in) << 16) |
+ (getByte(in) << 8) |
+ (getByte(in) << 0);
}
@Override
public Long read(InputStream in) throws IOException {
- return readLong(in, buffer);
+ return readLong(in);
}
public static int writeLong(byte[] buf, int pos, long i) {
View
24 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldMetadata.java
@@ -8,10 +8,6 @@
import com.raphfrk.craftproxylib.fields.Field;
public class FieldMetadata extends Field {
-
- public FieldMetadata() {
- super(4);
- }
private FieldByte fByte = new FieldByte(false);
private FieldString fString = new FieldString();
@@ -56,37 +52,37 @@ public int skip(InputStream in) throws IOException {
b = (b & 0xFF) >> 5;
switch(b) {
case 0: {
- data.add((byte)fByte.readByte(in));
+ data.add(fByte.readByte(in));
break;
}
case 1: {
- data.add((short)FieldShort.readShort(in, shortBuf));
+ data.add(FieldShort.readShort(in));
break;
}
case 2: {
- data.add((int)FieldInteger.readInt(in, buffer));
+ data.add(FieldInteger.readInt(in));
break;
}
case 3: {
- data.add((float)FieldFloat.readFloat(in, buffer));
+ data.add(FieldFloat.readFloat(in));
break;
}
case 4: {
- data.add(fString.read(in));
+ data.add(FieldString.readString(in));
break;
}
case 5: {
- short aa = FieldShort.readShort(in, shortBuf);
+ short aa = FieldShort.readShort(in);
byte bb = (byte)fByte.readByte(in);
- short cc = FieldShort.readShort(in, shortBuf);
+ short cc = FieldShort.readShort(in);
data.add(new Object[] {aa , bb, cc});
break;
}
case 6: {
- int aa = FieldInteger.readInt(in, buffer);
- int bb = FieldInteger.readInt(in, buffer);
- int cc = FieldInteger.readInt(in, buffer);
+ int aa = FieldInteger.readInt(in);
+ int bb = FieldInteger.readInt(in);
+ int cc = FieldInteger.readInt(in);
data.add(new Object[] {aa , bb, cc});
break;
View
8 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldShort.java
@@ -11,15 +11,13 @@ public FieldShort() {
super(2);
}
- public static short readShort(InputStream in, byte[] buffer) throws IOException {
- fillBuffer(buffer, in);
-
- return (short)((buffer[0] << 8) | (buffer[1] & 0xFF));
+ public static short readShort(InputStream in) throws IOException {
+ return (short)((getByte(in) << 8) | getByte(in));
}
@Override
public Short read(InputStream in) throws IOException {
- return readShort(in, buffer);
+ return readShort(in);
}
public static int writeShort(byte[] buf, int pos, short i) {
View
8 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldShortSizedByteArray.java
@@ -8,20 +8,16 @@
public class FieldShortSizedByteArray extends Field {
- public FieldShortSizedByteArray() {
- super(2);
- }
-
@Override
public int skip(InputStream in) throws IOException {
- int length = FieldShort.readShort(in, buffer);
+ int length = FieldShort.readShort(in);
return 2 + (int)in.skip(length);
}
@Override
public byte[] read(InputStream in) throws IOException {
- int length = FieldShort.readShort(in, buffer);
+ int length = FieldShort.readShort(in);
byte[] buf = new byte[length];
int i = 0;
View
18 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldString.java
@@ -6,31 +6,31 @@
import com.raphfrk.craftproxylib.fields.Field;
public class FieldString extends Field {
-
- public FieldString() {
- super(2);
- }
@Override
public int skip(InputStream in) throws IOException {
- int length = FieldShort.readShort(in, buffer);
+ int length = FieldShort.readShort(in);
int bytes = length << 1;
return 2 + (int)in.skip(bytes);
}
- @Override
- public String read(InputStream in) throws IOException {
- int length = FieldShort.readShort(in, buffer);
+ public static String readString(InputStream in) throws IOException {
+ int length = FieldShort.readShort(in);
StringBuilder sb = new StringBuilder(length);
for(int cnt=0; cnt<length;cnt++) {
- sb.append((char)FieldShort.readShort(in, buffer));
+ sb.append((char)FieldShort.readShort(in));
}
return sb.toString();
}
+ @Override
+ public String read(InputStream in) throws IOException {
+ return readString(in);
+ }
+
public static int writeString(byte[] buf, int pos, String s) {
pos = FieldShort.writeShort(buf, pos, (short)s.length());
char[] chars = s.toCharArray();
View
16 src/main/java/com/raphfrk/craftproxylib/fields/elements/FieldVelocity.java
@@ -8,15 +8,9 @@
public class FieldVelocity extends Field {
- byte[] shortBuffer = new byte[2];
-
- public FieldVelocity() {
- super(4);
- }
-
@Override
public int skip(InputStream in) throws IOException {
- int eid = FieldInteger.readInt(in, buffer);
+ int eid = FieldInteger.readInt(in);
if (eid != 0) {
in.skip(6);
@@ -28,12 +22,12 @@ public int skip(InputStream in) throws IOException {
@Override
public Velocity read(InputStream in) throws IOException {
- int eid = FieldInteger.readInt(in, buffer);
+ int eid = FieldInteger.readInt(in);
if (eid != 0) {
- short vx = FieldShort.readShort(in, shortBuffer);
- short vy = FieldShort.readShort(in, shortBuffer);
- short vz = FieldShort.readShort(in, shortBuffer);
+ short vx = FieldShort.readShort(in);
+ short vy = FieldShort.readShort(in);
+ short vz = FieldShort.readShort(in);
return new Velocity(eid, vx, vy, vz);
} else {
return new Velocity(eid);
View
2  src/main/java/com/raphfrk/craftproxylib/packet/Packet.java
@@ -88,7 +88,7 @@ public int read() throws IOException {
}
return b;
} else {
- return buf[pos++];
+ return buf[pos++] & 0xFF;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.