/** * Returns the minimum encoded size of the value. */ public final int getSizeInBytes() { return sizeOf(value); }
/** * 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(); }
protected static int calcLength(byte[] buf, int offset) { VarInt varint; int cursor = offset;// + 4; varint = new VarInt(buf, cursor); long len = varint.value; len += varint.getOriginalSizeInBytes(); cursor += len; return cursor - offset; }
@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); }
@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); }
protected static int calcLength(byte[] buf, int offset) { VarInt varint; int cursor = offset;// + 4; varint = new VarInt(buf, cursor); long len = varint.value; len += varint.getOriginalSizeInBytes(); cursor += len; return cursor - offset; }
@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); }
@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); }
protected static int calcLength(byte[] buf, int offset) { VarInt varint; // jump past version (uint32) int cursor = offset;// + 4; //vin TransactionInput cursor += 36; varint = new VarInt(buf, cursor); long scriptLen = varint.value; // 4 = length of sequence field (unint32) cursor += scriptLen + 4 + varint.getOriginalSizeInBytes(); return cursor - offset; } @Override
@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); }
public int calculateMessageSizeInBytes() { return VarInt.sizeOf(bytes.length) + bytes.length; } @Override
@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); }
/** * 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(); }
protected static int calcLength(byte[] buf, int offset) { VarInt varint; int cursor = offset; //vin cursor += 36; varint = new VarInt(buf, cursor); long scriptLen = varint.value; // 4 = length of sequence field (uint32) cursor += scriptLen + 4 + varint.getOriginalSizeInBytes(); varint = new VarInt(buf, cursor); long size = varint.value; cursor += varint.getOriginalSizeInBytes(); cursor += size; return cursor - offset; }
@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); }
/** * Returns the minimum encoded size of the value. */ public final int getSizeInBytes() { return sizeOf(value); }