@Override public Float decode(InputStream inStream) throws IOException, CoderException { try { return new DataInputStream(inStream).readFloat(); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } }
@Override public Integer decode(InputStream inStream) throws IOException, CoderException { try { return new DataInputStream(inStream).readInt(); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } }
@Override public void encode(Long value, OutputStream outStream) throws IOException, CoderException { if (value == null) { throw new CoderException("cannot encode a null Long"); } new DataOutputStream(outStream).writeLong(value); }
@Override public void encode(Float value, OutputStream outStream) throws IOException, CoderException { if (value == null) { throw new CoderException("cannot encode a null Float"); } new DataOutputStream(outStream).writeFloat(value); }
@Override public T decode(InputStream inStream) throws IOException, CoderException { try { ObjectInputStream ois = new ObjectInputStream(inStream); return type.cast(ois.readObject()); } catch (ClassNotFoundException e) { throw new CoderException("unable to deserialize record", e); } }
@Override protected long getEncodedElementByteSize(byte[] value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null byte[]"); } return (long) VarInt.getLength(value.length) + value.length; } }
private <InputT, OutputT> OutputT applyAndWrapExceptions( CodingFunction<InputT, OutputT> fn, InputT input) throws CoderException, IOException { try { return fn.apply(input); } catch (IOException exc) { throw exc; } catch (Exception exc) { throw new CoderException(exc); } }
@Override public void encode(Long value, OutputStream outStream) throws IOException, CoderException { if (value == null) { throw new CoderException("cannot encode a null Long"); } VarInt.encode(value, outStream); }
@Override protected long getEncodedElementByteSize(Long value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null Long"); } return VarInt.getLength(value); } }
@Override public void encode(BoundedWindow value, OutputStream outStream) throws CoderException, IOException { throw new CoderException("Test Encode Exception"); }
@Override public BoundedWindow decode(InputStream inStream) throws CoderException, IOException { throw new CoderException("Test Decode Exception"); } }
@Override public Long decode(InputStream inStream) throws IOException, CoderException { try { return VarInt.decodeLong(inStream); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. throw new CoderException(exn); } }
@Override protected long getEncodedElementByteSize(Integer value) throws Exception { if (value == null) { throw new CoderException("cannot encode a null Integer"); } return VarInt.getLength(value.longValue()); } }
@Override public K decode(InputStream inStream) throws CoderException, IOException { int marker = inStream.read(); if (marker == 0) { return (K) getMetadataKey(); } else if (marker == 1) { return keyCoder.decode(inStream); } else { throw new CoderException(String.format("Expected marker but got %s.", marker)); } }
@Override public void encode(Integer value, OutputStream outStream, Context context) throws IOException, CoderException { if (value == null) { throw new CoderException("cannot encode a null Integer"); } String textualValue = value.toString(); StringUtf8Coder.of().encode(textualValue, outStream, context); }
private static String readString(InputStream dis) throws IOException { int len = VarInt.decodeInt(dis); if (len < 0) { throw new CoderException("Invalid encoded string length: " + len); } byte[] bytes = new byte[len]; ByteStreams.readFully(dis, bytes); return new String(bytes, StandardCharsets.UTF_8); }
@Override public Integer decode(InputStream inStream, Context context) throws IOException, CoderException { String textualValue = StringUtf8Coder.of().decode(inStream, context); try { return Integer.valueOf(textualValue); } catch (NumberFormatException exn) { throw new CoderException("error when decoding a textual integer", exn); } }
@Override public void encode(BitSet value, OutputStream outStream, Context context) throws CoderException, IOException { if (value == null) { throw new CoderException("cannot encode a null BitSet"); } BYTE_ARRAY_CODER.encodeAndOwn(value.toByteArray(), outStream, context); }
@Override public void encode(ReadableDuration value, OutputStream outStream) throws CoderException, IOException { if (value == null) { throw new CoderException("cannot encode a null ReadableDuration"); } LONG_CODER.encode(toLong(value), outStream); }
@Override public void encode(KV<K, V> kv, OutputStream outStream, Context context) throws IOException, CoderException { if (kv == null) { throw new CoderException("cannot encode a null KV"); } keyCoder.encode(kv.getKey(), outStream); valueCoder.encode(kv.getValue(), outStream, context); }