/** * 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)); }
/** * Create a mutation detector for the provided {@code value}, using the provided {@link Coder} * for cloning and checking serialized forms for equality. */ public CodedValueMutationDetector(T value, Coder<T> coder) throws CoderException { this.coder = coder; // We need to clone the original value before getting it's structural value. // If the object is consistent with equals, the Structural value will be the // exact same object reference making it impossible to detect changes. clonedOriginalValue = CoderUtils.clone(coder, value); this.originalStructuralValue = coder.structuralValue(clonedOriginalValue); this.possiblyModifiedObject = value; this.encodedOriginalObject = CoderUtils.encodeToByteArray(coder, value); this.clonedOriginalObject = CoderUtils.decodeFromByteArray(coder, encodedOriginalObject); }
@Override public String stringKey() { try { return String.format(WINDOW_FORMAT, CoderUtils.encodeToBase64(windowCoder, window)); } catch (CoderException e) { throw new RuntimeException("Unable to generate string key from window " + window, e); } }
private void verifyUnmodifiedThrowingCheckedExceptions() throws CoderException { // Since there is no guarantee that cloning an object via the coder will // return the exact same type as value, We are cloning the possiblyModifiedObject // before getting it's structural value. This way we are guaranteed to compare the same // types. T possiblyModifiedClonedValue = CoderUtils.clone(coder, possiblyModifiedObject); Object newStructuralValue = coder.structuralValue(possiblyModifiedClonedValue); if (originalStructuralValue.equals(newStructuralValue)) { return; } else if (Objects.deepEquals( encodedOriginalObject, CoderUtils.encodeToByteArray(coder, possiblyModifiedObject))) { LOG.warn( "{} of type {} has a #structuralValue method which does not return true when the " + "encoding of the elements is equal. Element {}", Coder.class.getSimpleName(), coder.getClass(), possiblyModifiedObject); return; } illegalMutation(clonedOriginalObject, possiblyModifiedClonedValue); }
/** * 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); }
/** 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 copy(T t) { try { return CoderUtils.clone(coder, t); } catch (CoderException e) { throw new RuntimeException("Could not clone.", e); } }
public static <T> void coderDecodesBase64(Coder<T> coder, String base64Encoding, T value) throws Exception { assertThat( DECODING_WIRE_FORMAT_MESSAGE, CoderUtils.decodeFromBase64(coder, base64Encoding), equalTo(value)); }
private SingletonViewFn(boolean hasDefault, T defaultValue, Coder<T> valueCoder) { this.hasDefault = hasDefault; this.defaultValue = defaultValue; this.valueCoder = valueCoder; if (hasDefault) { try { this.encodedDefaultValue = CoderUtils.encodeToByteArray(valueCoder, defaultValue); } catch (IOException e) { throw new RuntimeException("Unexpected IOException: ", e); } } }
@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; } }
/** Like {@link CoderUtils#clone} but without a checked exception. */ private static <T> T uncheckedClone(Coder<T> coder, T value) { try { return CoderUtils.clone(coder, value); } catch (CoderException e) { throw new RuntimeException(e); } } }
W window = CoderUtils.decodeFromBase64(windowCoder, parts.get(1)); if (parts.size() > 3) { int index = Integer.parseInt(parts.get(2), WindowAndTriggerNamespace.TRIGGER_RADIX);
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)); }
private Slice getKeyBytes(K key) { try { return new Slice(CoderUtils.encodeToByteArray(keyCoder, key)); } catch (CoderException e) { throw new RuntimeException(e); } }
@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 T copy(T t) { try { return CoderUtils.clone(coder, t); } catch (CoderException e) { throw new RuntimeException("Could not clone.", e); } }
private void illegalMutation(T previousValue, T newValue) throws CoderException { throw new IllegalMutationException( String.format( "Value %s mutated illegally, new value was %s." + " Encoding was %s, now %s.", previousValue, newValue, CoderUtils.encodeToBase64(coder, previousValue), CoderUtils.encodeToBase64(coder, newValue)), previousValue, newValue); }
@SuppressWarnings("unchecked") public static <T, IterableT extends Iterable<T>> void coderDecodesBase64ContentsEqual( Coder<IterableT> coder, String base64Encoding, IterableT expected) throws Exception { IterableT result = CoderUtils.decodeFromBase64(coder, base64Encoding); if (Iterables.isEmpty(expected)) { assertThat(ENCODING_WIRE_FORMAT_MESSAGE, result, emptyIterable()); } else { assertThat( ENCODING_WIRE_FORMAT_MESSAGE, result, containsInAnyOrder((T[]) Iterables.toArray(expected, Object.class))); } }
@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))); }
private CoderStructuralKey(Coder<K> coder, K key) throws Exception { this.coder = coder; this.structuralValue = coder.structuralValue(key); this.encoded = CoderUtils.encodeToByteArray(coder, key); }