/** * Encodes the given value using the specified Coder, and returns the encoded bytes. * * <p>This function is not reentrant; it should not be called from methods of the provided {@link * Coder}. */ public static <T> byte[] encodeToByteArray(Coder<T> coder, T value) throws CoderException { return encodeToByteArray(coder, value, Coder.Context.OUTER); }
/** * 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)); }
private CoderStructuralKey(Coder<K> coder, K key) throws Exception { this.coder = coder; this.structuralValue = coder.structuralValue(key); this.encoded = CoderUtils.encodeToByteArray(coder, key); }
private Slice getKeyBytes(K key) { try { return new Slice(CoderUtils.encodeToByteArray(keyCoder, key)); } catch (CoderException e) { throw new RuntimeException(e); } }
@Override public ByteBuffer groupBy(WindowedValue<KV<K, V>> wv) { try { return ByteBuffer.wrap(CoderUtils.encodeToByteArray(keyCoder, wv.getValue().getKey())); } catch (CoderException e) { throw new RuntimeException(e); } } }
public SerializableArrayViaCoder(Coder<T> elementCoder, T[] value) { this.coder = ListCoder.of(elementCoder); this.value = value; try { this.encodedValue = CoderUtils.encodeToByteArray(coder, Arrays.asList(value)); } catch (CoderException exc) { throw UserCodeException.wrap(exc); } }
@Override public ByteBuffer getKey(WindowedValue<SingletonKeyedWorkItem<K, V>> value) throws Exception { K key = value.getValue().key(); byte[] keyBytes = CoderUtils.encodeToByteArray(keyCoder, key); return ByteBuffer.wrap(keyBytes); }
@Override public ByteBuffer getKey(WindowedValue<KV<K, V>> value) throws Exception { K key = value.getValue().getKey(); byte[] keyBytes = CoderUtils.encodeToByteArray(keyCoder, key); return ByteBuffer.wrap(keyBytes); }
@Override public ByteBuffer getKey(WindowedValue<KV<K, V>> value) throws Exception { K key = value.getValue().getKey(); byte[] keyBytes = CoderUtils.encodeToByteArray(keyCoder, key); return ByteBuffer.wrap(keyBytes); }
public static <W extends BoundedWindow> WindowSupplier of(Coder<W> coder, Iterable<W> windows) { ImmutableSet.Builder<byte[]> windowsBuilder = ImmutableSet.builder(); for (W window : windows) { try { windowsBuilder.add(CoderUtils.encodeToByteArray(coder, window)); } catch (CoderException e) { throw new IllegalArgumentException( "Could not encode provided windows with the provided window coder", e); } } return new WindowSupplier(coder, windowsBuilder.build()); }
private static <DestinationT> int hashDestination( DestinationT destination, Coder<DestinationT> destinationCoder) throws IOException { return Hashing.murmur3_32() .hashBytes(CoderUtils.encodeToByteArray(destinationCoder, destination)) .asInt(); }
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)); }
public static <T> CreateSource<T> fromIterable(Iterable<T> elements, Coder<T> elemCoder) throws CoderException, IOException { ImmutableList.Builder<byte[]> allElementsBytes = ImmutableList.builder(); long totalSize = 0L; for (T element : elements) { byte[] bytes = CoderUtils.encodeToByteArray(elemCoder, element); allElementsBytes.add(bytes); totalSize += bytes.length; } return new CreateSource<>(allElementsBytes.build(), totalSize, elemCoder); }
@Test public void testCoderExceptionPropagation() throws Exception { @SuppressWarnings("unchecked") Coder<String> crashingCoder = mock(Coder.class); doThrow(new CoderException("testing exception")) .when(crashingCoder) .encode(anyString(), any(OutputStream.class), any(Coder.Context.class)); expectedException.expect(CoderException.class); expectedException.expectMessage("testing exception"); CoderUtils.encodeToByteArray(crashingCoder, "hello"); }
@Test public void testEncodeAndOwn() throws Exception { for (byte[] value : TEST_VALUES) { byte[] encodedSlow = CoderUtils.encodeToByteArray(TEST_CODER, value); byte[] encodedFast = encodeToByteArrayAndOwn(TEST_CODER, value); assertThat(encodedSlow, equalTo(encodedFast)); } }
@Override public byte[] getCurrentRecordId() { try { return encodeToByteArray(KvCoder.of(VarIntCoder.of(), VarIntCoder.of()), getCurrent()); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testClosingCoderFailsWhenEncodingToByteArray() throws Exception { expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage("Caller does not own the underlying"); CoderUtils.encodeToByteArray(new ClosingCoder(), "test-value"); }
@Test public void testGetEncodedElementByteSize() throws Exception { TestElementByteSizeObserver observer = new TestElementByteSizeObserver(); for (BigDecimal value : TEST_VALUES) { TEST_CODER.registerByteSizeObserver(value, observer); observer.advance(); assertThat( observer.getSumAndReset(), equalTo( (long) CoderUtils.encodeToByteArray(TEST_CODER, value, Coder.Context.NESTED).length)); } }
@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); }
@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)); }