Use more ram to reduce CPU.

This commit is contained in:
md_5 2013-02-01 21:34:13 +11:00
parent 7538ff46ed
commit 00e0e3c1a3

View File

@ -2,9 +2,11 @@ package net.md_5.bungee.packet;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import net.md_5.mendax.datainput.DataInputPacketReader; import net.md_5.mendax.datainput.DataInputPacketReader;
import org.bouncycastle.crypto.io.CipherInputStream;
/** /**
* A specialized input stream to parse packets using the Mojang packet * A specialized input stream to parse packets using the Mojang packet
@ -15,6 +17,7 @@ public class PacketInputStream implements AutoCloseable
private final DataInputStream dataInput; private final DataInputStream dataInput;
private final TrackingInputStream tracker; private final TrackingInputStream tracker;
private final byte[] buffer = new byte[1 << 18];
public PacketInputStream(InputStream in) public PacketInputStream(InputStream in)
{ {
@ -31,7 +34,7 @@ public class PacketInputStream implements AutoCloseable
public byte[] readPacket() throws IOException public byte[] readPacket() throws IOException
{ {
tracker.out.reset(); tracker.out.reset();
DataInputPacketReader.readPacket(dataInput); DataInputPacketReader.readPacket(dataInput, buffer);
return tracker.out.toByteArray(); return tracker.out.toByteArray();
} }
@ -45,23 +48,30 @@ public class PacketInputStream implements AutoCloseable
* Input stream which will wrap another stream and copy all bytes read to a * Input stream which will wrap another stream and copy all bytes read to a
* {@link ByteArrayOutputStream}. * {@link ByteArrayOutputStream}.
*/ */
private class TrackingInputStream extends InputStream private class TrackingInputStream extends FilterInputStream
{ {
private final ByteArrayOutputStream out = new ByteArrayOutputStream(); private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final InputStream wrapped;
public TrackingInputStream(InputStream wrapped) public TrackingInputStream(InputStream in)
{ {
this.wrapped = wrapped; super(in);
} }
@Override @Override
public int read() throws IOException public int read() throws IOException
{ {
int ret = wrapped.read(); int ret = in.read();
out.write(ret); out.write(ret);
return ret; return ret;
} }
@Override
public int read(byte[] b, int off, int len) throws IOException
{
int ret = in.read(b, off, len);
out.write(b, off, ret);
return ret;
}
} }
} }