Skip to content

Instantly share code, notes, and snippets.

@LexManos
Created April 17, 2015 09:08
Show Gist options
  • Save LexManos/6054f1ae2fc1937633df to your computer and use it in GitHub Desktop.
Save LexManos/6054f1ae2fc1937633df to your computer and use it in GitHub Desktop.
package net.minecraftforge.common.util;
import java.io.DataInput;
import java.io.IOException;
import net.minecraft.nbt.NBTSizeTracker;
/**
* This is a wrapper around a DataInput and a NBTSizeTracker, this is a simple
* way to TRUELY keep track of everything that we are reading from the buffer.
*/
public class CountingDataInput implements DataInput
{
private DataInput wrapped;
private NBTSizeTracker tracker;
public CountingDataInput(DataInput wrapped, NBTSizeTracker tracker)
{
this.wrapped = wrapped;
this.tracker = tracker;
}
@Override
public boolean readBoolean() throws IOException
{
tracker.read(8);
return wrapped.readBoolean();
}
@Override
public byte readByte() throws IOException
{
tracker.read(8);
return wrapped.readByte();
}
@Override
public char readChar() throws IOException
{
tracker.read(16);
return wrapped.readChar();
}
@Override
public double readDouble() throws IOException
{
tracker.read(64);
return wrapped.readDouble();
}
@Override
public float readFloat() throws IOException
{
tracker.read(32);
return wrapped.readFloat();
}
@Override
public void readFully(byte[] b) throws IOException
{
tracker.read(8 * b.length);
wrapped.readFully(b);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException
{
tracker.read(8 * len);
wrapped.readFully(b, off, len);
}
@Override
public int readInt() throws IOException
{
tracker.read(32);
return wrapped.readInt();
}
@Override
public String readLine() throws IOException
{
String ret = wrapped.readLine();
if (ret == null)
return null;
tracker.read(8 * ret.length());
return ret;
}
@Override
public long readLong() throws IOException
{
tracker.read(64);
return wrapped.readLong();
}
@Override
public short readShort() throws IOException
{
tracker.read(16);
return wrapped.readShort();
}
@Override
public String readUTF() throws IOException
{
String ret = wrapped.readUTF();
NBTSizeTracker.readUTF(tracker, ret);
return ret;
}
@Override
public int readUnsignedByte() throws IOException
{
tracker.read(8);
return wrapped.readUnsignedByte();
}
@Override
public int readUnsignedShort() throws IOException
{
tracker.read(16);
return wrapped.readUnsignedShort();
}
@Override
public int skipBytes(int n) throws IOException
{
//Technically this skips, but lets count it in the size as well.
// This is never used in vanilla NBT parsing, but just in case!
tracker.read(8 * n);
return wrapped.skipBytes(n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment