@Override protected long getEncodedElementByteSize(long[] value) { return VarInt.getLength(value[0]); } };
@Override protected long getEncodedElementByteSize(byte[] value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null byte[]"); } return (long) VarInt.getLength(value.length) + value.length; } }
@Override protected long getEncodedElementByteSize(Integer value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null Integer"); } return VarInt.getLength(value.longValue()); } }
@Override protected long getEncodedElementByteSize(Long value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null Long"); } return VarInt.getLength(value); } }
@Override protected long getEncodedElementByteSize(RandomAccessData value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null in memory stream"); } return (long) VarInt.getLength(value.size) + value.size; } }
@Override protected long getEncodedElementByteSize(ByteString value) throws Exception { int size = value.size(); return (long) VarInt.getLength(size) + size; }
/** Returns the length of the encoding of the given value (in bytes). */ public static int getLength(int v) { return getLength(convertIntToLongNoSignExtend(v)); }
/** * {@inheritDoc} * * @return the byte size of the UTF-8 encoding of the a string or, in a nested context, the byte * size of the encoding plus the encoded length prefix. */ @Override public long getEncodedElementByteSize(String value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null String"); } int size = Utf8.encodedLength(value); return (long) VarInt.getLength(size) + size; } }
/** * Overridden to short-circuit the default {@code StructuredCoder} behavior of encoding and * counting the bytes. The size is known to be the size of the value plus the number of bytes * required to prefix the length. * * <p>{@inheritDoc} */ @Override protected long getEncodedElementByteSize(T value) throws Exception { if (valueCoder instanceof StructuredCoder) { // If valueCoder is a StructuredCoder then we can ask it directly for the encoded size of // the value, adding the number of bytes to represent the length. long valueSize = ((StructuredCoder<T>) valueCoder).getEncodedElementByteSize(value); return VarInt.getLength(valueSize) + valueSize; } // If value is not a StructuredCoder then fall back to the default StructuredCoder behavior // of encoding and counting the bytes. The encoding will include the length prefix. return super.getEncodedElementByteSize(value); }
/** Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder. */ @Override public void registerByteSizeObserver(RawUnionValue union, ElementByteSizeObserver observer) throws Exception { int index = getIndexForEncoding(union); // Write out the union tag. observer.update(VarInt.getLength(index)); // Write out the actual value. @SuppressWarnings("unchecked") Coder<Object> coder = (Coder<Object>) elementCoders.get(index); coder.registerByteSizeObserver(union.getValue(), observer); }
@Test public void encodeValuesAndGetLength() throws IOException { assertEquals(LONG_VALUES.length, LONG_ENCODED.length); for (int i = 0; i < LONG_VALUES.length; ++i) { byte[] encoded = encodeLong(LONG_VALUES[i]); assertThat(encoded, equalTo(LONG_ENCODED[i])); assertEquals(LONG_ENCODED[i].length, VarInt.getLength(LONG_VALUES[i])); } assertEquals(INT_VALUES.length, INT_ENCODED.length); for (int i = 0; i < INT_VALUES.length; ++i) { byte[] encoded = encodeInt(INT_VALUES[i]); assertThat(encoded, equalTo(INT_ENCODED[i])); assertEquals(INT_ENCODED[i].length, VarInt.getLength(INT_VALUES[i])); } }
observer.update(VarInt.getLength(count));