@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!"); } } }
/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }
@Override public ByteBuffer read(ByteBuffer old, int length) throws IOException { if (old != null) { return super.read(old, length); } else { return bbi.readBuffer(length); } }
/** @see InputStream#read() * @throws EOFException if EOF is reached. */ @Override public int read() throws IOException { return getBuffer().get() & 0xff; }
/** @see InputStream#read() * @throws EOFException if EOF is reached. */ @Override public int read() throws IOException { return getBuffer().get() & 0xff; }
@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!"); } }
/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }
/** @see InputStream#read(byte[], int, int) * @throws EOFException if EOF is reached before reading all the bytes. */ @Override public int read(byte[] b, int off, int len) throws IOException { if (len == 0) return 0; ByteBuffer buffer = getBuffer(); int remaining = buffer.remaining(); if (len > remaining) { buffer.get(b, off, remaining); return remaining; } else { buffer.get(b, off, len); return len; } }
@Override public ByteBuffer read(ByteBuffer old, int length) throws IOException { if (old != null) { return super.read(old, length); } else { return bbi.readBuffer(length); } }
try (ByteBufferInputStream inputStream = new ByteBufferInputStream(Collections.singletonList(bytes))) { return reader.read(null, DecoderFactory.get().binaryDecoder(inputStream, null));
/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }
/** @see InputStream#read(byte[], int, int) * @throws EOFException if EOF is reached before reading all the bytes. */ @Override public int read(byte[] b, int off, int len) throws IOException { if (len == 0) return 0; ByteBuffer buffer = getBuffer(); int remaining = buffer.remaining(); if (len > remaining) { buffer.get(b, off, remaining); return remaining; } else { buffer.get(b, off, len); return len; } }
@Override public ByteBuffer read(ByteBuffer old, int length) throws IOException { if (old != null) { return super.read(old, length); } else { return bbi.readBuffer(length); } }
/** 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(); } }
/** Read a buffer from the input without copying, if possible. * @throws EOFException if EOF is reached before reading all the bytes. */ public ByteBuffer readBuffer(int length) throws IOException { if (length == 0) return ByteBuffer.allocate(0); ByteBuffer buffer = getBuffer(); if (buffer.remaining() == length) { // can return current as-is? current++; return buffer; // return w/o copying } // punt: allocate a new buffer & copy into it ByteBuffer result = ByteBuffer.allocate(length); int start = 0; while (start < length) start += read(result.array(), start, length-start); return result; }
/** @see InputStream#read() * @throws EOFException if EOF is reached. */ @Override public int read() throws IOException { return getBuffer().get() & 0xff; }
@Override public ByteBuffer read(ByteBuffer old, int length) throws IOException { if (old != null) { return super.read(old, length); } else { return bbi.readBuffer(length); } }
ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(response);
/** @see InputStream#read() * @throws EOFException if EOF is reached. */ @Override public int read() throws IOException { return getBuffer().get() & 0xff; }
@Override @SuppressWarnings("unchecked") public void handleResult(List<ByteBuffer> responseBytes) { ByteBufferInputStream bbi = new ByteBufferInputStream(responseBytes); BinaryDecoder in = DecoderFactory.get().binaryDecoder(bbi, null); try {