Пример #1
0
  @SuppressWarnings("unchecked")
  @Override
  public void encode(RawUnionValue union, OutputStream outStream, Context context)
      throws IOException, CoderException {
    int index = getIndexForEncoding(union);
    // Write out the union tag.
    VarInt.encode(index, outStream);

    // Write out the actual value.
    Coder<Object> coder = (Coder<Object>) elementCoders.get(index);
    coder.encode(union.getValue(), outStream, context);
  }
Пример #2
0
  @Override
  public void encode(T value, OutputStream outStream, Context context)
      throws CoderException, IOException {
    try {
      JAXBContext jaxbContext = getContext();
      // TODO: Consider caching in a ThreadLocal if this impacts performance
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
      if (!context.isWholeStream) {
        try {
          long size = getEncodedElementByteSize(value, Context.OUTER);
          // record the number of bytes the XML consists of so when reading we only read the encoded
          // value
          VarInt.encode(size, outStream);
        } catch (Exception e) {
          throw new CoderException(
              "An Exception occured while trying to get the size of an encoded representation", e);
        }
      }

      jaxbMarshaller.marshal(value, new CloseIgnoringOutputStream(outStream));
    } catch (JAXBException e) {
      throw new CoderException(e);
    }
  }