Package org.apache.avro.io

Examples of org.apache.avro.io.Decoder


   * @return Decoded Avro schema entry.
   * @throws java.io.IOException on I/O error.
   */
  public static SchemaTableEntry decodeSchemaEntry(final byte[] bytes) throws IOException {
    final SchemaTableEntry entry = new SchemaTableEntry();
    final Decoder decoder =
        DECODER_FACTORY.directBinaryDecoder(new ByteArrayInputStream(bytes), null);
    return SCHEMA_ENTRY_READER.read(entry, decoder);
  }
View Full Code Here


   * @throws IOException on error.
   */
  @SuppressWarnings({ "rawtypes", "unchecked" })
  public static Object fromAvroJsonString(String json, Schema schema) throws IOException {
    final InputStream jsonInput = new ByteArrayInputStream(json.getBytes("UTF-8"));
    final Decoder decoder = DecoderFactory.get().jsonDecoder(schema, jsonInput);
    final SpecificDatumReader reader = new SpecificDatumReader(schema);
    return reader.read(null, decoder);
  }
View Full Code Here

              if (len > valuebytes.length) {
                valuebytes = new byte[len];
              }
              dis.readFully(valuebytes, 0, len);
              DecoderFactory decoderFactory = new DecoderFactory();
              Decoder d = decoderFactory.binaryDecoder(new ByteArrayInputStream(valuebytes, 0, len), null);
              // Read the data
              SpecificDatumReader<Beacon> sdr = new SpecificDatumReader<Beacon>(writerSchema, Beacon.SCHEMA$);
              Beacon read = sdr.read(null, d);
              beaconFAB.put(row, read);
            }
View Full Code Here

  /** Called by a server to deserialize a request, compute and serialize a
   * response or error.  Transciever is used by connection-based servers to
   * track handshake status of connection. */
  public List<ByteBuffer> respond(List<ByteBuffer> buffers,
                                  Transceiver connection) throws IOException {
    Decoder in = DecoderFactory.defaultFactory().createBinaryDecoder(
        new ByteBufferInputStream(buffers), null);
    ByteBufferOutputStream bbo = new ByteBufferOutputStream();
    BinaryEncoder out = new BinaryEncoder(bbo);
    Exception error = null;
    RPCContext context = new RPCContext();
    List<ByteBuffer> payload = null;
    List<ByteBuffer> handshake = null;
    boolean wasConnected = connection != null && connection.isConnected();
    try {
      Protocol remote = handshake(in, out, connection);
      if (remote == null)                        // handshake failed
        return bbo.getBufferList();
      handshake = bbo.getBufferList();
     
      // read request using remote protocol specification
      context.setRequestCallMeta(META_READER.read(null, in));
      String messageName = in.readString(null).toString();
      Message rm = remote.getMessages().get(messageName);
      if (rm == null)
        throw new AvroRuntimeException("No such remote message: "+messageName);
     
      Object request = readRequest(rm.getRequest(), in);
View Full Code Here

      DataInputStream din = new DataInputStream(input);
      DataFileWriter<Object> writer =
        new DataFileWriter<Object>(new GenericDatumWriter<Object>());
      writer.setCodec(CodecFactory.fromString(codec.value(opts)));
      writer.create(schema, out);
      Decoder decoder = new JsonDecoder(schema, din);
      Object datum;
      while (true) {
        try {
          datum = reader.read(null, decoder);
        } catch (EOFException e) {
View Full Code Here

          "Unable to serialize avro object to byte buffer - "
              + "please report this issue to the Gora bugtracker "
              + "or your administrator.");
    }
    byte[] value = bos.toByteArray();
    Decoder dec = DecoderFactory.get().binaryDecoder(value, null);
    @SuppressWarnings("unchecked")
    SpecificDatumReader<T> reader = new SpecificDatumReader<T>(
        (Class<T>) persistent.getClass());
    try {
      return reader.read(null, dec);
View Full Code Here

      DataInputStream din = new DataInputStream(input);
      DataFileWriter<Object> writer =
        new DataFileWriter<Object>(new GenericDatumWriter<Object>());
      writer.setCodec(CodecFactory.fromString(codec.value(opts)));
      writer.create(schema, out);
      Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din);
      Object datum;
      while (true) {
        try {
          datum = reader.read(null, decoder);
        } catch (EOFException e) {
View Full Code Here

    for (GenericData.Record record : records) {
      datumWriter.write(record, encoder);
    }
    encoder.flush();

    Decoder decoder = DecoderFactory.get().binaryDecoder(new ByteArrayInputStream(bout.toByteArray()), null);
    DatumReader<GenericData.Record> datumReader = new GenericDatumReader<GenericData.Record>(schema);
    for (int i = 0; i < records.length; i++) {
      GenericData.Record record3 = datumReader.read(null, decoder);
      assertEquals(records[i], record3);
    }
View Full Code Here

  @Test
  public void testDecodeInt() throws Exception {
    InputStream in = new ByteArrayInputStream(new byte[] { (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x01});
    Decoder decoder = new ColumnDecoder(in);
    int i = decoder.readInt();
    assertEquals(1, i);

    in = new ByteArrayInputStream(new byte[] { (byte) 0xff, (byte) 0xff,
        (byte) 0xff, (byte) 0xff });
    decoder = new ColumnDecoder(in);
    i = decoder.readInt();
    assertEquals(-1, i);

    in = new ByteArrayInputStream(new byte[] { (byte) 0x00, (byte) 0x00,
        (byte) 0x00, (byte) 0x00 });
    decoder = new ColumnDecoder(in);
    i = decoder.readInt();
    assertEquals(0, i);
  }
View Full Code Here

  @Test
  public void testDecodeLong() throws Exception {
    InputStream in = new ByteArrayInputStream(new byte[] { (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
        (byte) 0x00, (byte) 0x01 });
    Decoder decoder = new ColumnDecoder(in);
    long i = decoder.readLong();
    assertEquals(1L, i);

    in = new ByteArrayInputStream(new byte[] { (byte) 0xff, (byte) 0xff,
        (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
        (byte) 0xff });
    decoder = new ColumnDecoder(in);
    i = decoder.readLong();
    assertEquals(-1L, i);

    in = new ByteArrayInputStream(new byte[] { (byte) 0x00, (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
        (byte) 0x00 });
    decoder = new ColumnDecoder(in);
    i = decoder.readLong();
    assertEquals(0L, i);
  }
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.