/** Decodes the given bytes using the specified Coder, and returns the resulting decoded value. */ public static <T> T decodeFromByteArray(Coder<T> coder, byte[] encodedValue) throws CoderException { return decodeFromByteArray(coder, encodedValue, Coder.Context.OUTER); }
@Override public T get() { if (value == null) { try { value = CoderUtils.decodeFromByteArray(coder, encodedValue); } catch (CoderException exc) { throw new RuntimeException("Error deserializing via Coder", exc); } } return value; } }
@Override public K getKey() { try { return CoderUtils.decodeFromByteArray(coder, encoded); } catch (CoderException e) { throw new IllegalArgumentException( "Could not decode Key with coder of type " + coder.getClass().getSimpleName(), e); } }
@Override public K getKey() { ByteBuffer keyBytes = (ByteBuffer) keyedStateBackend.getCurrentKey(); try { byte[] bytes = new byte[keyBytes.remaining()]; keyBytes.get(bytes); keyBytes.position(keyBytes.position() - bytes.length); return CoderUtils.decodeFromByteArray(keyCoder, bytes); } catch (CoderException e) { throw new RuntimeException("Error decoding key.", e); } }
@Override public K getKey() { ByteBuffer keyBytes = flinkStateBackend.getCurrentKey(); byte[] bytes = new byte[keyBytes.remaining()]; keyBytes.get(bytes); keyBytes.position(keyBytes.position() - bytes.length); try { return CoderUtils.decodeFromByteArray(keyCoder, bytes); } catch (CoderException e) { throw new RuntimeException("Error decoding key.", e); } }
/** * Clones the given value by encoding and then decoding it with the specified Coder. * * <p>This function is not reentrant; it should not be called from methods of the provided {@link * Coder}. */ public static <T> T clone(Coder<T> coder, T value) throws CoderException { return decodeFromByteArray(coder, encodeToByteArray(coder, value)); }
@Override public K getKey() { ByteBuffer keyBytes = (ByteBuffer) keyedStateBackend.getCurrentKey(); try { byte[] bytes = new byte[keyBytes.remaining()]; keyBytes.get(bytes); keyBytes.position(keyBytes.position() - bytes.length); return CoderUtils.decodeFromByteArray(keyCoder, bytes); } catch (CoderException e) { throw new RuntimeException("Error decoding key.", e); } }
@Override public K getKey() { ByteBuffer keyBytes = flinkStateBackend.getCurrentKey(); byte[] bytes = new byte[keyBytes.remaining()]; keyBytes.get(bytes); keyBytes.position(keyBytes.position() - bytes.length); try { return CoderUtils.decodeFromByteArray(keyCoder, bytes); } catch (CoderException e) { throw new RuntimeException("Error decoding key.", e); } }
@Override public K getKey() { ByteBuffer keyBytes = (ByteBuffer) keyedStateBackend.getCurrentKey(); try { return CoderUtils.decodeFromByteArray(keyCoder, keyBytes.array()); } catch (CoderException e) { throw new RuntimeException("Error decoding key.", e); } }
@Override public Void apply(T actual) { try { T expected = CoderUtils.decodeFromByteArray(coder, encodedExpected); return relation.assertFor(expected).apply(actual); } catch (IOException coderException) { throw new RuntimeException(coderException); } } }
@Override public T decode(InputStream is) throws IOException { return CoderUtils.decodeFromByteArray( innerCoder, Snappy.uncompress(ByteArrayCoder.of().decode(is))); }
private KV<?, ?> asRunnerKV( Coder<KV<String, Integer>> javaWireCoder, Coder<KV<?, ?>> runnerWireCoder, KV<String, Integer> value) throws org.apache.beam.sdk.coders.CoderException { return CoderUtils.decodeFromByteArray( runnerWireCoder, CoderUtils.encodeToByteArray(javaWireCoder, value)); }
@Test public void testEncodeThenMutate() throws Exception { byte[] input = {0x7, 0x3, 0xA, 0xf}; byte[] encoded = CoderUtils.encodeToByteArray(TEST_CODER, input); input[1] = 0x9; byte[] decoded = CoderUtils.decodeFromByteArray(TEST_CODER, encoded); // now that I have mutated the input, the output should NOT match assertThat(input, not(equalTo(decoded))); }
@Test public void testSerializableCoder() throws Exception { IterableCoder<MyRecord> coder = IterableCoder.of(SerializableCoder.of(MyRecord.class)); List<MyRecord> records = new ArrayList<>(); for (String l : LINES) { records.add(new MyRecord(l)); } byte[] encoded = CoderUtils.encodeToByteArray(coder, records); Iterable<MyRecord> decoded = CoderUtils.decodeFromByteArray(coder, encoded); assertEquals(records, decoded); }
@Override public KV<Integer, Long> apply(KV<byte[], Long> input) { try { return KV.of( CoderUtils.decodeFromByteArray(VarIntCoder.of(), input.getKey()), input.getValue()); } catch (CoderException e) { fail("Unexpected Coder Exception " + e); throw new AssertionError("Unreachable"); } } }));
@Test public void testClosingCoderFailsWhenDecodingByteArray() throws Exception { expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage("Caller does not own the underlying"); CoderUtils.decodeFromByteArray(new ClosingCoder(), new byte[0]); }
@Test public void testEncodeDecodeAfterClone() throws Exception { JAXBCoder<TestType> coder = SerializableUtils.clone(JAXBCoder.of(TestType.class)); byte[] encoded = CoderUtils.encodeToByteArray(coder, new TestType("abc", 9999)); assertEquals(new TestType("abc", 9999), CoderUtils.decodeFromByteArray(coder, encoded)); }
@Test public void testEncodeDecode() throws Exception { MyCustomCoder coder = new MyCustomCoder("key"); CoderProperties.coderDecodeEncodeEqual(coder, KV.of("key", 3L)); byte[] encoded2 = CoderUtils.encodeToByteArray(coder, KV.of("ignored", 3L)); Assert.assertEquals(KV.of("key", 3L), CoderUtils.decodeFromByteArray(coder, encoded2)); }
@Test public void testCheckpointCoderNulls() throws Exception { CheckpointCoder<String> coder = new CheckpointCoder<>(StringUtf8Coder.of()); Checkpoint<String> emptyCheckpoint = new Checkpoint<>(null, null); Checkpoint<String> decodedEmptyCheckpoint = CoderUtils.decodeFromByteArray(coder, CoderUtils.encodeToByteArray(coder, emptyCheckpoint)); assertNull(decodedEmptyCheckpoint.getResidualElements()); assertNull(decodedEmptyCheckpoint.getResidualSource()); }
@Test public void testCoder_nullCell() throws CoderException { TableRow row = new TableRow(); row.set("temperature", Data.nullOf(Object.class)); row.set("max_temperature", Data.nullOf(Object.class)); byte[] bytes = CoderUtils.encodeToByteArray(TableRowJsonCoder.of(), row); TableRow newRow = CoderUtils.decodeFromByteArray(TableRowJsonCoder.of(), bytes); byte[] newBytes = CoderUtils.encodeToByteArray(TableRowJsonCoder.of(), newRow); Assert.assertArrayEquals(bytes, newBytes); }