Package org.apache.avro.io

Examples of org.apache.avro.io.Decoder


    out.close();
// ^^ AvroGenericRecordSerialization
   
// vv AvroGenericRecordDeserialization
    DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema);
    Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);
    GenericRecord result = reader.read(null, decoder);
    assertThat(result.get("left").toString(), is("L"));
    assertThat(result.get("right").toString(), is("R"));
// ^^ AvroGenericRecordDeserialization
  }
View Full Code Here


    encoder.flush();
    out.close();
   
    /*[*/DatumReader<StringPair> reader =
      new SpecificDatumReader<StringPair>(StringPair.class);/*]*/
    Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);
    StringPair result = reader.read(null, decoder);
    assertThat(result./*[*/left/*]*/.toString(), is("L"));
    assertThat(result./*[*/right/*]*/.toString(), is("R"));
// ^^ AvroSpecificStringPair
  }
View Full Code Here

    encoder.flush();
   
// vv AvroSchemaResolution
    DatumReader<GenericRecord> reader =
      /*[*/new GenericDatumReader<GenericRecord>(schema, newSchema);/*]*/
    Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);
    GenericRecord result = reader.read(null, decoder);
    assertThat(result.get("left").toString(), is("L"));
    assertThat(result.get("right").toString(), is("R"));
    /*[*/assertThat(result.get("description").toString(), is(""));/*]*/
// ^^ AvroSchemaResolution
View Full Code Here

    writer.write(datum, encoder);
    encoder.flush();
   
    DatumReader<GenericRecord> reader =
      new GenericDatumReader<GenericRecord>(schema, newSchema);
    Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);
    GenericRecord result = reader.read(null, decoder);
    assertThat(result.get("first").toString(), is("L"));
    assertThat(result.get("second").toString(), is("R"));

    // old field names don't work
View Full Code Here

    datum.put("right", "R");
    writer.write(datum, encoder);
    encoder.flush();
   
    DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema, newSchema); // write schema, read schema
    Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);
    GenericRecord result = reader.read(null, decoder);
    assertThat(result.get("left").toString(), is("L"));
    assertThat(result.get("right").toString(), is("R"));
    assertThat(result.get("description"), is((Object) null));
  }
View Full Code Here

    datum.put("right", "R");
    writer.write(datum, encoder);
    encoder.flush();
   
    DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema, newSchema); // write schema, read schema
    Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);
    try {
      reader.read(null, decoder);
      fail("Expected AvroTypeException");
    } catch (AvroTypeException e) {
      // expected
View Full Code Here

    return out.toByteArray();
  }

  @Override
  public T fromBytes(byte[] bytes) {
    Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null);
    try {
      return getReader().read(null, decoder);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
View Full Code Here

   * a response or error. */
  public List<ByteBuffer> respond(Transceiver transceiver) throws IOException {
    ByteBufferInputStream bbi =
      new ByteBufferInputStream(transceiver.readBuffers());
   
    Decoder in = new BinaryDecoder(bbi);
    ByteBufferOutputStream bbo =
      new ByteBufferOutputStream();
    Encoder out = new BinaryEncoder(bbo);
    AvroRemoteException error = null;
    RPCContext context = new RPCContext();
    try {
      Protocol remote = handshake(transceiver, in, out);
      if (remote == null)                        // handshake failed
        return bbo.getBufferList();

      // read request using remote protocol specification
      context.setRequestCallMeta(META_READER.read(null, in));
      String messageName = in.readString(null).toString();
      Message m = remote.getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("No such remote message: "+messageName);
     
      Object request = readRequest(m.getRequest(), in);
View Full Code Here

  }

  /** Writes a request message and reads a response or error message. */
  public Object request(String messageName, Object request)
    throws IOException {
    Decoder in;
    Message m;
    RPCContext context = new RPCContext();
    do {
      ByteBufferOutputStream bbo = new ByteBufferOutputStream();
      Encoder out = new BinaryEncoder(bbo);

      if (!established)                           // if not established
        writeHandshake(out);                      // prepend handshake

      // use local protocol to write request
      m = getLocal().getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("Not a local message: "+messageName);
     
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientSendRequest(context);
      }
     
      META_WRITER.write(context.requestCallMeta(), out);
      out.writeString(m.getName());       // write message name
      writeRequest(m.getRequest(), request, out); // write request payload
     
      List<ByteBuffer> response =                 // transceive
        getTransceiver().transceive(bbo.getBufferList());
     
      ByteBufferInputStream bbi = new ByteBufferInputStream(response);
      in = new BinaryDecoder(bbi);
      if (!established)                           // if not established
        readHandshake(in);                        // process handshake
    } while (!established);

    // use remote protocol to read response
    m = getRemote().getMessages().get(messageName);
    if (m == null)
      throw new AvroRuntimeException("Not a remote message: "+messageName);
    context.setRequestCallMeta(META_READER.read(null, in));
   
    if (!in.readBoolean()) {                      // no error
      Object response = readResponse(m.getResponse(), in);
      context.setResponse(response);
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.clientReceiveResponse(context);
      }
View Full Code Here

        encoder.flush();
    }

    public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
        DatumReader<GenericRecord> reader = new SpecificDatumReader<GenericRecord>(getSchema(exchange, null));
        Decoder decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
        Object result = reader.read(null, decoder);
        return result;
    }
View Full Code Here

TOP

Related Classes of org.apache.avro.io.Decoder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.