/** * Test that toString returns a substring of the buffer with range(0, position). */ @Test public void testToString() throws IOException { byte[] data = "1234567890".getBytes(ConfigConstants.DEFAULT_CHARSET); ByteArrayOutputStreamWithPos stream = new ByteArrayOutputStreamWithPos(data.length); stream.write(data); Assert.assertArrayEquals(data, stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET)); for (int i = 0; i < data.length; i++) { stream.setPosition(i); Assert.assertArrayEquals(Arrays.copyOf(data, i), stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET)); } // validate that the stored bytes are still tracked properly even when expanding array stream.setPosition(data.length + 1); Assert.assertArrayEquals(Arrays.copyOf(data, data.length + 1), stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET)); } }
/** * Test setting negative position. */ @Test public void testSetNegativePosition() throws Exception { stream.write(new byte[BUFFER_SIZE]); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Position out of bounds"); stream.setPosition(-1); }
/** * Test setting position which is exactly the same with the buffer size. */ @Test public void testSetPositionWhenBufferIsFull() throws Exception { stream.write(new byte[BUFFER_SIZE]); // check whether the buffer is filled fully Assert.assertEquals(BUFFER_SIZE, stream.getBuf().length); // check current position is the end of the buffer Assert.assertEquals(BUFFER_SIZE, stream.getPosition()); stream.setPosition(BUFFER_SIZE); // confirm current position is at where we expect. Assert.assertEquals(BUFFER_SIZE, stream.getPosition()); }
/** * Test setting position larger than buffer size. */ @Test public void testSetPositionLargerThanBufferSize() throws Exception { // fully fill the buffer stream.write(new byte[BUFFER_SIZE]); Assert.assertEquals(BUFFER_SIZE, stream.getBuf().length); // expand the buffer by setting position beyond the buffer length stream.setPosition(BUFFER_SIZE + 1); Assert.assertEquals(BUFFER_SIZE * 2, stream.getBuf().length); Assert.assertEquals(BUFFER_SIZE + 1, stream.getPosition()); }
@Override public Iterator<Map.Entry<MK, MV>> subIterator(K key, MK startMapKey, MK endMapKey) { if (key == null || startMapKey == null || endMapKey == null) { return Collections.emptyIterator(); } if (stateStorage.lazySerde()) { SortedMap<MK, MV> map = get(key); return map == null ? Collections.emptyIterator() : map.subMap(startMapKey, endMapKey).entrySet().iterator(); } else { try { outputStream.reset(); StateSerializerUtil.getSerializedPrefixKeyForKeyedMapState( outputStream, outputView, key, keySerializer, getKeyGroup(key), stateNameForSerialize); int keyPosition = outputStream.getPosition(); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, startMapKey, mapKeySerializer); byte[] prefixKey = outputStream.toByteArray(); outputStream.setPosition(keyPosition); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, endMapKey, mapKeySerializer); byte[] prefixKeyEnd = outputStream.toByteArray(); return subIterator(prefixKey, prefixKeyEnd); } catch (Exception e) { throw new StateAccessException(e); } } }
@Override public Iterator<Map.Entry<MK, MV>> tailIterator(K key, MK startMapKey) { if (key == null || startMapKey == null) { return Collections.emptyIterator(); } if (stateStorage.lazySerde()) { SortedMap<MK, MV> map = get(key); return map == null ? Collections.emptyIterator() : map.tailMap(startMapKey).entrySet().iterator(); } else { try { outputStream.reset(); StateSerializerUtil.getSerializedPrefixKeyForKeyedMapState( outputStream, outputView, key, keySerializer, getKeyGroup(key), stateNameForSerialize); int keyPosition = outputStream.getPosition(); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, startMapKey, mapKeySerializer); byte[] prefixKey = outputStream.toByteArray(); outputStream.setPosition(keyPosition); outputStream.write(KEY_END_BYTE); byte[] prefixKeyEnd = outputStream.toByteArray(); return subIterator(prefixKey, prefixKeyEnd); } catch (Exception e) { throw new StateAccessException(e); } } }
/** * Decode a String value. */ public String decodeString(DataInputView source, Order ord) throws IOException { if (reuseBuffer == null) { reuseBuffer = new byte[BUFFER_SIZE]; } if (reuseBaos == null) { reuseBaos = new ByteArrayOutputStreamWithPos(); } reuseBaos.reset(); int len; while ((len = source.read(reuseBuffer)) != -1) { reuseBaos.write(reuseBuffer, 0, len); } if (ord == Order.DESCENDING) { // DESCENDING ordered string requires a termination bit to preserve // sort-order semantics of empty values. reuseBaos.setPosition(reuseBaos.size() - 1); } return new String(ord.apply(reuseBaos.toByteArray()), UTF8); }
byte[] prefixKey = outputStream.toByteArray(); outputStream.setPosition(namespacePosition); outputStream.write(StateSerializerUtil.KEY_END_BYTE); byte[] prefixKeyEnd = outputStream.toByteArray();
for (MK mapKey : mapKeys) { if (mapKey != null) { outputStream.setPosition(prefixLength); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, mapKey, mapKeySerializer); byte[] byteValue = (byte[]) stateStorage.get(outputStream.toByteArray());
int prefixLength = outputStream.getPosition(); for (MK mapKey : mapKeys) { outputStream.setPosition(prefixLength); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, mapKey, mapKeySerializer); stateStorage.remove(outputStream.toByteArray());
/** * Decode a byte array, byte-for-byte copy. */ public byte[] decodeByteArray(DataInputView source, Order ord) throws IOException { if (reuseBuffer == null) { reuseBuffer = new byte[BUFFER_SIZE]; } if (reuseBaos == null) { reuseBaos = new ByteArrayOutputStreamWithPos(); } reuseBaos.reset(); int len; while ((len = source.read(reuseBuffer)) != -1) { reuseBaos.write(reuseBuffer, 0, len); } if (ord == Order.DESCENDING) { // DESCENDING ordered ByteArray requires a termination bit to preserve // sort-order semantics of empty values. reuseBaos.setPosition(reuseBaos.size() - 1); } return ord.apply(reuseBaos.toByteArray()); }
Preconditions.checkNotNull(entry.getValue(), "Can not insert null value to mapstate"); outputStream.setPosition(prefixLength); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, entry.getKey(), mapKeySerializer); byte[] byteKey = outputStream.toByteArray();
byte[] prefixKey = outputStream.toByteArray(); outputStream.setPosition(namespacePosition); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, endMapKey, mapKeySerializer); byte[] prefixKeyEnd = outputStream.toByteArray();
Preconditions.checkNotNull(entry.getValue(), "Can not insert null value to mapstate"); outputStream.setPosition(prefixLength); StateSerializerUtil.serializeItemWithKeyPrefix(outputView, entry.getKey(), mapKeySerializer); byte[] byteKey = outputStream.toByteArray();
/** * Decode a BinaryString value. */ public BinaryString decodeBinaryString(DataInputView source, Order ord) throws IOException { if (reuseBuffer == null) { reuseBuffer = new byte[BUFFER_SIZE]; } if (reuseBaos == null) { reuseBaos = new ByteArrayOutputStreamWithPos(); } reuseBaos.reset(); int len; while ((len = source.read(reuseBuffer)) != -1) { reuseBaos.write(reuseBuffer, 0, len); } if (ord == Order.DESCENDING) { // DESCENDING ordered string requires a termination bit to preserve // sort-order semantics of empty values. reuseBaos.setPosition(reuseBaos.size() - 1); } return BinaryString.fromBytes(ord.apply(reuseBaos.toByteArray())); }