Package org.apache.avro.io

Examples of org.apache.avro.io.BinaryEncoder


    return this;
  }

  private void init(OutputStream outs) throws IOException {
    this.out = new BufferedFileOutputStream(outs);
    this.vout = new BinaryEncoder(out);
    dout.setSchema(schema);
    buffer = new NonCopyingByteArrayOutputStream(
        Math.min((int)(syncInterval * 1.25), Integer.MAX_VALUE/2 -1));
    this.bufOut = new BinaryEncoder(buffer);
    if (this.codec == null) {
      this.codec = CodecFactory.nullCodec().createInstance();
    }
    this.isOpen = true;
  }
View Full Code Here


   * a response or error. */
  public List<ByteBuffer> respond(List<ByteBuffer> buffers) throws IOException {
    Decoder in = DecoderFactory.defaultFactory().createBinaryDecoder(
        new ByteBufferInputStream(buffers), null);
    ByteBufferOutputStream bbo = new ByteBufferOutputStream();
    Encoder out = new BinaryEncoder(bbo);
    Exception error = null;
    RPCContext context = new RPCContext();
    try {
      Protocol remote = handshake(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);
     
      context.setMessage(m);
     
      Object request = readRequest(m.getRequest(), in);
     
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.serverReceiveRequest(context);
      }

      // create response using local protocol specification
      m = getLocal().getMessages().get(messageName);
      if (m == null)
        throw new AvroRuntimeException("No message named "+messageName
                                       +" in "+getLocal());
      Object response = null;
      try {
        response = respond(m, request);
        context.setResponse(response);
      } catch (Exception e) {
        error = e;
        context.setError(error);
      }
     
      for (RPCPlugin plugin : rpcMetaPlugins) {
        plugin.serverSendResponse(context);
      }
     
      META_WRITER.write(context.responseCallMeta(), out);
      out.writeBoolean(error != null);
      if (error == null)
        writeResponse(m.getResponse(), response, out);
      else
        writeError(m.getErrors(), error, out);

    } catch (Exception e) {                       // system error
      LOG.warn("system error", e);
      context.setError(e);
      bbo = new ByteBufferOutputStream();
      out = new BinaryEncoder(bbo);
      META_WRITER.write(context.responseCallMeta(), out);
      out.writeBoolean(true);
      writeError(Protocol.SYSTEM_ERRORS, new Utf8(e.toString()), out);
    }
     
    return bbo.getBufferList();
  }
View Full Code Here

    BinaryDecoder in = null;
    Message m;
    RPCContext context = new RPCContext();
    do {
      ByteBufferOutputStream bbo = new ByteBufferOutputStream();
      Encoder out = new BinaryEncoder(bbo);

      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);
      context.setMessage(m);
     
      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());
     
View Full Code Here

        new GenericDatumReader<Object>(schema);
    Object datum = reader.read(null, new JsonDecoder(schema, input));
   
    GenericDatumWriter<Object> writer =
        new GenericDatumWriter<Object>(schema);
    writer.write(datum, new BinaryEncoder(out));
    } finally {
      if (needsClosing) {
        input.close();
      }
    }
View Full Code Here

  }

  private byte[] serializeAvro(Object datum, Schema schema) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ReflectDatumWriter<Object> writer = new ReflectDatumWriter<Object>(schema);
    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    out.reset();
    writer.write(datum, encoder);
    encoder.flush();
    return out.toByteArray();
  }
View Full Code Here

    Schema schm = reflectData.getSchema(A.class);
    LOG.info(schm);

    ReflectDatumWriter<A> writer = new ReflectDatumWriter<A>(schm);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Encoder json = new BinaryEncoder(out);
    writer.write(anA, json);

    byte[] bs = out.toByteArray();
    dump(bs);
    Assert.assertEquals(12, bs.length);
View Full Code Here

    LOG.info(schm);

    ReflectDatumWriter<EventImpl> writer = new ReflectDatumWriter<EventImpl>(
        schm);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.write(e, new BinaryEncoder(out));

    byte[] bs = out.toByteArray();
    dump(bs);
    Assert.assertEquals(60, bs.length);
View Full Code Here

    }

    @Override
    public void open(OutputStream out) throws IOException {
      outStream = out;
      encoder = new BinaryEncoder(out);
    }
View Full Code Here

    assertEquals(Schema.parse(schema), s);

    // check that value is serialized correctly
    ReflectDatumWriter<Object> writer = new ReflectDatumWriter<Object>(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.write(value, new BinaryEncoder(out));
    ReflectDatumReader<Object> reader = new ReflectDatumReader<Object>(s);
    Object after =
      reader.read(null, DecoderFactory.defaultFactory().createBinaryDecoder(
          out.toByteArray(), null));
    assertEquals(value, after);
View Full Code Here

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write( AvroSerializationProvider.getMajorVersion() );
    out.write( AvroSerializationProvider.getMinorVersion() );
    Schema msgSchema = protocol.getType( "Message" );
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>( msgSchema );
    BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder( out, null );
    GenericRecord message = new GenericData.Record( msgSchema );
    message.put( "classReferences", classReferences );
    message.put( "operations", operations );
    operations = null;
    try {
      writer.write( message, encoder );
      encoder.flush();
    }
    catch (IOException e) {
      throw log.unableToSerializeInAvro( e );
    }
    return out.toByteArray();
View Full Code Here

TOP

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

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.