/** * Constructs a new VarInt with the given unsigned long value. * * @param value the unsigned long value (beware widening conversion of negatives!) */ public VarInt(long value) { this.value = value; originallyEncodedSize = getSizeInBytes(); }
/** * Constructs a new VarInt with the given unsigned long value. * * @param value the unsigned long value (beware widening conversion of negatives!) */ public VarInt(long value) { this.value = value; originallyEncodedSize = getSizeInBytes(); }
/** * Constructs a new VarInt with the given unsigned long value. * * @param value the unsigned long value (beware widening conversion of negatives!) */ public VarInt(long value) { this.value = value; originallyEncodedSize = getSizeInBytes(); }
/** * Constructs a new VarInt with the given unsigned long value. * * @param value the unsigned long value (beware widening conversion of negatives!) */ public VarInt(long value) { this.value = value; originallyEncodedSize = getSizeInBytes(); }
@Override protected void parse() throws ProtocolException { long numAddresses = readVarInt(); // Guard against ultra large messages that will crash us. if (numAddresses > MAX_ADDRESSES) throw new ProtocolException("Address message too large."); addresses = new ArrayList<PeerAddress>((int) numAddresses); for (int i = 0; i < numAddresses; i++) { PeerAddress addr = new PeerAddress(params, payload, cursor, protocolVersion, this, serializer); addresses.add(addr); cursor += addr.getMessageSize(); } length = new VarInt(addresses.size()).getSizeInBytes(); // The 4 byte difference is the uint32 timestamp that was introduced in version 31402 length += addresses.size() * (protocolVersion > 31402 ? PeerAddress.MESSAGE_SIZE : PeerAddress.MESSAGE_SIZE - 4); }
@Override protected void parse() throws ProtocolException { long numAddresses = readVarInt(); // Guard against ultra large messages that will crash us. if (numAddresses > MAX_ADDRESSES) throw new ProtocolException("Address message too large."); addresses = new ArrayList<>((int) numAddresses); for (int i = 0; i < numAddresses; i++) { PeerAddress addr = new PeerAddress(params, payload, cursor, protocolVersion, this, serializer); addresses.add(addr); cursor += addr.getMessageSize(); } length = new VarInt(addresses.size()).getSizeInBytes(); // The 4 byte difference is the uint32 timestamp that was introduced in version 31402 length += addresses.size() * (protocolVersion > 31402 ? PeerAddress.MESSAGE_SIZE : PeerAddress.MESSAGE_SIZE - 4); }
@Override protected void parse() throws ProtocolException { long numAddresses = readVarInt(); // Guard against ultra large messages that will crash us. if (numAddresses > MAX_ADDRESSES) throw new ProtocolException("Address message too large."); addresses = new ArrayList<>((int) numAddresses); for (int i = 0; i < numAddresses; i++) { PeerAddress addr = new PeerAddress(params, payload, cursor, protocolVersion, this, serializer); addresses.add(addr); cursor += addr.getMessageSize(); } length = new VarInt(addresses.size()).getSizeInBytes(); // The 4 byte difference is the uint32 timestamp that was introduced in version 31402 length += addresses.size() * (protocolVersion > 31402 ? PeerAddress.MESSAGE_SIZE : PeerAddress.MESSAGE_SIZE - 4); }
@Override protected void parse() throws ProtocolException { long numAddresses = readVarInt(); // Guard against ultra large messages that will crash us. if (numAddresses > MAX_ADDRESSES) throw new ProtocolException("Address message too large."); addresses = new ArrayList<PeerAddress>((int) numAddresses); for (int i = 0; i < numAddresses; i++) { PeerAddress addr = new PeerAddress(params, payload, cursor, protocolVersion, this, serializer); addresses.add(addr); cursor += addr.getMessageSize(); } length = new VarInt(addresses.size()).getSizeInBytes(); // The 4 byte difference is the uint32 timestamp that was introduced in version 31402 length += addresses.size() * (protocolVersion > 31402 ? PeerAddress.MESSAGE_SIZE : PeerAddress.MESSAGE_SIZE - 4); }
@Test public void testBytes() throws Exception { VarInt a = new VarInt(10); // with widening conversion assertEquals(1, a.getSizeInBytes()); assertEquals(1, a.encode().length); assertEquals(10, new VarInt(a.encode(), 0).value); }
@Test public void testShorts() throws Exception { VarInt a = new VarInt(64000); // with widening conversion assertEquals(3, a.getSizeInBytes()); assertEquals(3, a.encode().length); assertEquals(64000, new VarInt(a.encode(), 0).value); }
@Test public void testShortFFFF() throws Exception { VarInt a = new VarInt(0xFFFFL); assertEquals(3, a.getSizeInBytes()); assertEquals(3, a.encode().length); assertEquals(0xFFFFL, new VarInt(a.encode(), 0).value); }
@Test public void testIntFFFFFFFF() throws Exception { VarInt a = new VarInt(0xFFFFFFFFL); assertEquals(5, a.getSizeInBytes()); assertEquals(5, a.encode().length); byte[] bytes = a.encode(); assertEquals(0xFFFFFFFFL, 0xFFFFFFFFL & new VarInt(bytes, 0).value); }
@Test public void testInts() throws Exception { VarInt a = new VarInt(0xAABBCCDDL); assertEquals(5, a.getSizeInBytes()); assertEquals(5, a.encode().length); byte[] bytes = a.encode(); assertEquals(0xAABBCCDDL, 0xFFFFFFFFL & new VarInt(bytes, 0).value); }
@Test public void testLong() throws Exception { VarInt a = new VarInt(0xCAFEBABEDEADBEEFL); assertEquals(9, a.getSizeInBytes()); assertEquals(9, a.encode().length); byte[] bytes = a.encode(); assertEquals(0xCAFEBABEDEADBEEFL, new VarInt(bytes, 0).value); }