Exemplo n.º 1
0
 @Override
 public RawUnionValue decode(InputStream inStream, Context context)
     throws IOException, CoderException {
   int index = VarInt.decodeInt(inStream);
   Object value = elementCoders.get(index).decode(inStream, context);
   return new RawUnionValue(index, value);
 }
Exemplo n.º 2
0
 /** Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder. */
 @Override
 public void registerByteSizeObserver(
     RawUnionValue union, ElementByteSizeObserver observer, Context context) throws Exception {
   int index = getIndexForEncoding(union);
   // Write out the union tag.
   observer.update(VarInt.getLength(index));
   // Write out the actual value.
   @SuppressWarnings("unchecked")
   Coder<Object> coder = (Coder<Object>) elementCoders.get(index);
   coder.registerByteSizeObserver(union.getValue(), observer, context);
 }
Exemplo n.º 3
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);
  }
Exemplo n.º 4
0
  @Override
  public T decode(InputStream inStream, Context context) throws CoderException, IOException {
    try {
      JAXBContext jaxbContext = getContext();
      // TODO: Consider caching in a ThreadLocal if this impacts performance
      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

      InputStream stream = inStream;
      if (!context.isWholeStream) {
        long limit = VarInt.decodeLong(inStream);
        stream = ByteStreams.limit(inStream, limit);
      }
      @SuppressWarnings("unchecked")
      T obj = (T) jaxbUnmarshaller.unmarshal(new CloseIgnoringInputStream(stream));
      return obj;
    } catch (JAXBException e) {
      throw new CoderException(e);
    }
  }
Exemplo n.º 5
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);
    }
  }