Use byte array streams for chunk I/O.

This commit is contained in:
Steven Smith 2015-08-23 11:28:09 -07:00
parent 93978378f6
commit 83b83bdc3e
3 changed files with 22 additions and 20 deletions

View file

@ -5,11 +5,11 @@ import org.spacehq.mc.protocol.data.game.chunk.Column;
import org.spacehq.mc.protocol.util.NetUtil;
import org.spacehq.packetlib.io.NetInput;
import org.spacehq.packetlib.io.NetOutput;
import org.spacehq.packetlib.io.buffer.ByteBufferNetOutput;
import org.spacehq.packetlib.io.stream.StreamNetOutput;
import org.spacehq.packetlib.packet.Packet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class ServerChunkDataPacket implements Packet {
@ -55,15 +55,16 @@ public class ServerChunkDataPacket implements Packet {
@Override
public void write(NetOutput out) throws IOException {
ByteBufferNetOutput byteOut = new ByteBufferNetOutput(ByteBuffer.allocate(557312));
int mask = NetUtil.writeColumn(byteOut, this.column, this.column.hasBiomeData(), this.column.hasSkylight());
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
NetOutput netOut = new StreamNetOutput(byteOut);
int mask = NetUtil.writeColumn(netOut, this.column, this.column.hasBiomeData(), this.column.hasSkylight());
out.writeInt(this.column.getX());
out.writeInt(this.column.getZ());
out.writeBoolean(this.column.hasBiomeData());
out.writeShort(mask);
out.writeVarInt(byteOut.getByteBuffer().arrayOffset());
out.writeBytes(byteOut.getByteBuffer().array(), byteOut.getByteBuffer().arrayOffset());
out.writeVarInt(byteOut.size());
out.writeBytes(byteOut.toByteArray(), byteOut.size());
}
@Override

View file

@ -4,11 +4,11 @@ import org.spacehq.mc.protocol.data.game.chunk.Column;
import org.spacehq.mc.protocol.util.NetUtil;
import org.spacehq.packetlib.io.NetInput;
import org.spacehq.packetlib.io.NetOutput;
import org.spacehq.packetlib.io.buffer.ByteBufferNetOutput;
import org.spacehq.packetlib.io.stream.StreamNetOutput;
import org.spacehq.packetlib.packet.Packet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class ServerMultiChunkDataPacket implements Packet {
@ -66,8 +66,6 @@ public class ServerMultiChunkDataPacket implements Packet {
}
}
ByteBufferNetOutput byteOut = new ByteBufferNetOutput(ByteBuffer.allocate(columns.length * 557312));
out.writeBoolean(skylight);
out.writeVarInt(columns.length);
@ -81,12 +79,15 @@ public class ServerMultiChunkDataPacket implements Packet {
}
out.writeVarInt(columns.length);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
NetOutput netOut = new StreamNetOutput(byteOut);
for(Column column : columns) {
out.writeInt(NetUtil.writeColumn(byteOut, column, true, skylight));
out.writeInt(NetUtil.writeColumn(netOut, column, true, skylight));
}
out.writeVarInt(byteOut.getByteBuffer().arrayOffset());
out.writeBytes(byteOut.getByteBuffer().array(), byteOut.getByteBuffer().arrayOffset());
out.writeVarInt(byteOut.size());
out.writeBytes(byteOut.toByteArray(), byteOut.size());
}
@Override

View file

@ -1,13 +1,13 @@
package org.spacehq.mc.protocol.util;
import org.spacehq.mc.protocol.data.game.EntityMetadata;
import org.spacehq.mc.protocol.data.game.ItemStack;
import org.spacehq.mc.protocol.data.game.Position;
import org.spacehq.mc.protocol.data.game.Rotation;
import org.spacehq.mc.protocol.data.game.chunk.BlockStorage;
import org.spacehq.mc.protocol.data.game.chunk.Chunk;
import org.spacehq.mc.protocol.data.game.chunk.Column;
import org.spacehq.mc.protocol.data.game.EntityMetadata;
import org.spacehq.mc.protocol.data.game.ItemStack;
import org.spacehq.mc.protocol.data.game.chunk.NibbleArray3d;
import org.spacehq.mc.protocol.data.game.Position;
import org.spacehq.mc.protocol.data.game.Rotation;
import org.spacehq.mc.protocol.data.game.values.MagicValues;
import org.spacehq.mc.protocol.data.game.values.entity.MetadataType;
import org.spacehq.mc.protocol.data.game.values.world.block.BlockFace;
@ -16,14 +16,14 @@ import org.spacehq.opennbt.NBTIO;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import org.spacehq.packetlib.io.NetInput;
import org.spacehq.packetlib.io.NetOutput;
import org.spacehq.packetlib.io.buffer.ByteBufferNetInput;
import org.spacehq.packetlib.io.stream.StreamNetInput;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@ -236,7 +236,7 @@ public class NetUtil {
}
public static Column readColumn(byte data[], int x, int z, boolean fullChunk, boolean hasSkylight, int mask) throws IOException {
NetInput in = new ByteBufferNetInput(ByteBuffer.wrap(data));
NetInput in = new StreamNetInput(new ByteArrayInputStream(data));
Exception ex = null;
Column column = null;
try {