FML expands S3F to support payloads up to 2 megs in size. Should be transparent

This commit is contained in:
Christian committed 2014-02-16 18:30:27 -05:00
1 parent 572d32358a
commit d0dd05a15c
3 files changed
+119

No files matched your search

@@ -55,7 +55,38 @@ public class ByteBufUtils {
return i;
}
/**
* An extended length short. Used by custom payload packets to extend size.
*
* @param buf
* @return
*/
public static int readVarShort(ByteBuf buf)
{
int low = buf.readUnsignedShort();
int high = 0;
if ((low & 0x8000) != 0)
{
low = low & 0x7FFF;
high = buf.readUnsignedByte();
}
return ((high & 0xFF) << 15) | low;
}
public static void writeVarShort(ByteBuf buf, int toWrite)
{
int low = toWrite & 0x7FFF;
int high = ( toWrite & 0x7F8000 ) >> 15;
if (high != 0)
{
low = low | 0x8000;
}
buf.writeShort(low);
if (high != 0)
{
buf.writeByte(high);
}
}
/**
* Write an integer to the buffer using variable length encoding. The maxSize constrains
* how many bytes (and therefore the maximum number) that will be written.
@@ -120,4 +120,55 @@ public class TestNetStuff {
assertArrayEquals("String repeat bytes", new byte[] {-112, 3, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, buf.array());
}
@Test
public void testVarShort()
{
ByteBuf buf = Unpooled.buffer(3,3);
ByteBufUtils.writeVarShort(buf, 32766);
assertArrayEquals("Two byte short written", new byte[] { 127, -2, 0}, buf.array());
assertEquals("Two byte short written", 2, buf.readableBytes());
buf.clear();
buf.writeZero(3);
buf.clear();
buf.writeShort(32766);
assertArrayEquals("Two byte short written", new byte[] { 127, -2, 0}, buf.array());
int val = ByteBufUtils.readVarShort(buf);
assertEquals("Two byte short read correctly", 32766, val);
buf.clear();
buf.writeZero(3);
buf.clear();
ByteBufUtils.writeVarShort(buf, 32768);
assertArrayEquals("Three byte short written", new byte[] { -128, 0, 1}, buf.array());
assertEquals("Three byte short written", 3, buf.readableBytes());
buf.clear();
buf.writeZero(3);
buf.clear();
buf.writeShort(-32768);
buf.writeByte(1);
val = ByteBufUtils.readVarShort(buf);
assertEquals("Three byte short read correctly", 32768, val);
buf.clear();
buf.writeZero(3);
buf.clear();
ByteBufUtils.writeVarShort(buf, 8388607);
assertArrayEquals("Three byte short written", new byte[] { -1, -1, -1}, buf.array());
assertEquals("Three byte short written", 3, buf.readableBytes());
buf.clear();
buf.writeZero(3);
buf.clear();
buf.writeShort(-1);
buf.writeByte(-1);
val = ByteBufUtils.readVarShort(buf);
assertEquals("Three byte short read correctly", 8388607, val);
}
}