@Override public GenericRecord parse(ByteBuffer bytes) { try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) { return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null)); } catch (EOFException eof) { // waiting for avro v1.9.0 (#AVRO-813) throw new ParseException( eof, "Avro's unnecessary EOFException, detail: [%s]", "https://issues.apache.org/jira/browse/AVRO-813" ); } catch (Exception e) { throw new ParseException(e, "Fail to decode avro message!"); } } }
@Override public GenericRecord parse(ByteBuffer bytes) { Pair<SUBJECT, ID> subjectAndId = subjectAndIdConverter.getSubjectAndId(bytes); Schema schema = typedRepository.getSchema(subjectAndId.lhs, subjectAndId.rhs); DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) { return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null)); } catch (EOFException eof) { // waiting for avro v1.9.0 (#AVRO-813) throw new ParseException( eof, "Avro's unnecessary EOFException, detail: [%s]", "https://issues.apache.org/jira/browse/AVRO-813" ); } catch (IOException e) { throw new ParseException(e, "Fail to decode avro message!"); } }
try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) { return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null));
/** Return the remote protocol. Force a handshake if required. */ public Protocol getRemote() throws IOException { if (remote != null) return remote; // already have it MD5 remoteHash = REMOTE_HASHES.get(transceiver.getRemoteName()); if (remoteHash != null) { remote = REMOTE_PROTOCOLS.get(remoteHash); if (remote != null) return remote; // already cached } handshakeLock.lock(); try { // force handshake ByteBufferOutputStream bbo = new ByteBufferOutputStream(); // direct because the payload is tiny. Encoder out = ENCODER_FACTORY.directBinaryEncoder(bbo, null); writeHandshake(out); out.writeInt(0); // empty metadata out.writeString(""); // bogus message name List<ByteBuffer> response = getTransceiver().transceive(bbo.getBufferList()); ByteBufferInputStream bbi = new ByteBufferInputStream(response); BinaryDecoder in = DecoderFactory.get().binaryDecoder(bbi, null); readHandshake(in); return this.remote; } finally { handshakeLock.unlock(); } }
ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(response);
@Override @SuppressWarnings("unchecked") public void handleResult(List<ByteBuffer> responseBytes) { ByteBufferInputStream bbi = new ByteBufferInputStream(responseBytes); BinaryDecoder in = DecoderFactory.get().binaryDecoder(bbi, null); try {
@Test public void testReuse() throws IOException { ByteBufferOutputStream bbo1 = new ByteBufferOutputStream(); ByteBufferOutputStream bbo2 = new ByteBufferOutputStream(); byte[] b1 = new byte[] { 1, 2 }; BinaryEncoder e1 = e_factory.binaryEncoder(bbo1, null); e1.writeBytes(b1); e1.flush(); BinaryEncoder e2 = e_factory.binaryEncoder(bbo2, null); e2.writeBytes(b1); e2.flush(); DirectBinaryDecoder d = new DirectBinaryDecoder( new ByteBufferInputStream(bbo1.getBufferList())); ByteBuffer bb1 = d.readBytes(null); Assert.assertEquals(b1.length, bb1.limit() - bb1.position()); d.configure(new ByteBufferInputStream(bbo2.getBufferList())); ByteBuffer bb2 = d.readBytes(null); Assert.assertEquals(b1.length, bb2.limit() - bb2.position()); }
Transceiver connection) throws IOException { Decoder in = DecoderFactory.get().binaryDecoder( new ByteBufferInputStream(buffers), null); ByteBufferOutputStream bbo = new ByteBufferOutputStream(); BinaryEncoder out = EncoderFactory.get().binaryEncoder(bbo, null);
public static InputStream asInputStream(ByteBuffer buffer) { if (buffer.hasArray()) { // use heap buffer; no array is created; only the reference is used return new ByteArrayInputStream(buffer.array()); } return new ByteBufferInputStream(buffer); }
ByteBuffer buffer = ByteBuffer.allocate( 1000 ); ByteBufferOutputStream bufferOutput = new ByteBufferOutputStream( buffer ); GZIPOutputStream output = new GZIPOutputStream( bufferOutput ); output.write("stackexchange".getBytes()); output.close(); buffer.position( 0 ); byte[] result = new byte[ 1000 ]; ByteBufferInputStream bufferInput = new ByteBufferInputStream( buffer ); GZIPInputStream input = new GZIPInputStream( bufferInput ); input.read( result ); System.out.println( new String(result));
/** * @param buffer data * @param writer writer schema for the SpecificDatumReader, useful when producers may use different schema versions * @return deserialized object */ public DatumEnvelope deserializeDatumEnvelope(final ByteBuffer buffer, final Schema writer) { try (final InputStream byteBufferInputStream = new ByteBufferInputStream(Collections.singletonList(buffer))) { // hack alert: using old envelope to reconcile version diffs final DatumReader<DatumEnvelope> datumReader = new SpecificDatumReader<>(writer, DatumEnvelope.getClassSchema()); final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(byteBufferInputStream, null); return datumReader.read(null, decoder); } catch (final Exception e) { throw new RuntimeException("Could not deserialize datum envelope", e); } }
@Override public GenericRecord parse(ByteBuffer bytes) { try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) { return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null)); } catch (EOFException eof) { // waiting for avro v1.9.0 (#AVRO-813) throw new ParseException( eof, "Avro's unnecessary EOFException, detail: [%s]", "https://issues.apache.org/jira/browse/AVRO-813" ); } catch (Exception e) { throw new ParseException(e, "Fail to decode avro message!"); } } }
@Override public GenericRecord parse(ByteBuffer bytes) { Pair<SUBJECT, ID> subjectAndId = subjectAndIdConverter.getSubjectAndId(bytes); Schema schema = typedRepository.getSchema(subjectAndId.lhs, subjectAndId.rhs); DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) { return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null)); } catch (EOFException eof) { // waiting for avro v1.9.0 (#AVRO-813) throw new ParseException( eof, "Avro's unnecessary EOFException, detail: [%s]", "https://issues.apache.org/jira/browse/AVRO-813" ); } catch (IOException e) { throw new ParseException(e, "Fail to decode avro message!"); } }
try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) { return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null));
public TDomainClass deserializeDatum(final SerializedDatum serializedDatum) { try { final DatumTypeVersion datumTypeVersion = serializedDatum.getDatumTypeVersion(); final Schema datumWriterSchema = datumSchemaRepository.getSchema(datumTypeVersion); final Schema datumReaderSchema = datumSchemaRepository.getLatestSchema(datumTypeVersion.getDatumTypeId()); final DatumReader<? extends SpecificRecord> datumReader = new SpecificDatumReader<>(datumWriterSchema, datumReaderSchema); final InputStream byteBufferInputStream = new ByteBufferInputStream(Collections.singletonList(serializedDatum.getPayload())); final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(byteBufferInputStream, null); final SpecificRecord record = datumReader.read(null, decoder); byteBufferInputStream.close(); return avroRoundTripProjector.fromAvro(record); } catch (final IOException e) { throw new RuntimeException("Could not deserialize versioned payload to domain object", e); } } }
list.add(ByteBuffer.wrap(arr)); try (ByteBufferInputStream is = new ByteBufferInputStream(list)) { deserializer.open(is); T newObj = deserializer.deserialize(obj);
list.add(ByteBuffer.wrap(arr)); try (ByteBufferInputStream is = new ByteBufferInputStream(list)) { deserializer.open(is); T newObj = deserializer.deserialize(obj);
/** Return the remote protocol. Force a handshake if required. */ public Protocol getRemote() throws IOException { if (remote != null) return remote; // already have it MD5 remoteHash = REMOTE_HASHES.get(transceiver.getRemoteName()); if (remoteHash != null) { remote = REMOTE_PROTOCOLS.get(remoteHash); if (remote != null) return remote; // already cached } handshakeLock.lock(); try { // force handshake ByteBufferOutputStream bbo = new ByteBufferOutputStream(); // direct because the payload is tiny. Encoder out = ENCODER_FACTORY.directBinaryEncoder(bbo, null); writeHandshake(out); out.writeInt(0); // empty metadata out.writeString(""); // bogus message name List<ByteBuffer> response = getTransceiver().transceive(bbo.getBufferList()); ByteBufferInputStream bbi = new ByteBufferInputStream(response); BinaryDecoder in = DecoderFactory.get().binaryDecoder(bbi, null); readHandshake(in); return this.remote; } finally { handshakeLock.unlock(); } }
ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(response);
@Override @SuppressWarnings("unchecked") public void handleResult(List<ByteBuffer> responseBytes) { ByteBufferInputStream bbi = new ByteBufferInputStream(responseBytes); BinaryDecoder in = DecoderFactory.get().binaryDecoder(bbi, null); try {