@Override public void encode(UnserializableRecord value, OutputStream outStream) throws CoderException, IOException { stringCoder.encode(value.myString, outStream); }
@Override public void encode(T value, OutputStream outStream, Context context) throws CoderException, IOException { coder.encode(applyAndWrapExceptions(toFn, value), outStream, context); }
@Override public void encode(PubsubMessage value, OutputStream outStream, Context context) throws IOException { PAYLOAD_CODER.encode(value.getPayload(), outStream, context); }
@Override public void serialize(T t, DataOutputView dataOutputView) throws IOException { DataOutputViewWrapper outputWrapper = new DataOutputViewWrapper(dataOutputView); coder.encode(t, outputWrapper); }
@Override public Slice toByteArray(Object wv) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { coder.encode(wv, bos, Context.OUTER); } catch (IOException e) { throw new RuntimeException(e); } return new Slice(bos.toByteArray()); }
@Override public void encode(Params value, OutputStream outStream) throws IOException { if (value == null) { throw new CoderException("cannot encode a null value"); } stringCoder.encode(value.baseFilename.get().toString(), outStream); stringCoder.encode(value.shardTemplate, outStream); stringCoder.encode(value.suffix, outStream); }
@Override public void encode(ValueWithRecordId<ValueT> value, OutputStream outStream, Context context) throws IOException { valueCoder.encode(value.value, outStream); idCoder.encode(value.id, outStream, context); }
@Override public void encode(T value, OutputStream outStream) throws CoderException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); valueCoder.encode(value, bos, Context.OUTER); VarInt.encode(bos.size(), outStream); bos.writeTo(outStream); }
@Override public void encode(CustomWindow window, OutputStream outStream) throws IOException { INTERVAL_WINDOW_CODER.encode(window, outStream); VAR_INT_CODER.encode(window.isBig ? 1 : 0, outStream); }
@Override public void encode(ShardedKey<KeyT> key, OutputStream outStream) throws IOException { keyCoder.encode(key.getKey(), outStream); shardNumberCoder.encode(key.getShardNumber(), outStream); }
@Override public void encode(WindowedValue<T> windowedElem, OutputStream outStream, Context context) throws CoderException, IOException { InstantCoder.of().encode(windowedElem.getTimestamp(), outStream); windowsCoder.encode(windowedElem.getWindows(), outStream); PaneInfoCoder.INSTANCE.encode(windowedElem.getPane(), outStream); valueCoder.encode(windowedElem.getValue(), outStream, context); }
@Override public void encode(TimestampedValue<T> windowedElem, OutputStream outStream) throws IOException { valueCoder.encode(windowedElem.getValue(), outStream); InstantCoder.of().encode(windowedElem.getTimestamp(), outStream); }
@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"); }
/** Encodes the given element using the given coder and hashes the encoding. */ static <T> long hash(T element, Coder<T> coder) throws CoderException, IOException { try (HashingOutputStream stream = new HashingOutputStream(Hashing.murmur3_128(), ByteStreams.nullOutputStream())) { coder.encode(element, stream, Context.OUTER); return stream.hash().asLong(); } } }