@Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket dp) throws Exception { ByteBufInputStream bis = new ByteBufInputStream(dp.content()); try { ClusterStatusProtos.ClusterStatus csp = ClusterStatusProtos.ClusterStatus.parseFrom(bis); ClusterMetrics ncs = ClusterMetricsBuilder.toClusterMetrics(csp); receive(ncs); } finally { bis.close(); } } }
private void readResponse(ChannelHandlerContext ctx, ByteBuf buf) throws IOException { int totalSize = buf.readInt(); ByteBufInputStream in = new ByteBufInputStream(buf); ResponseHeader responseHeader = ResponseHeader.parseDelimitedFrom(in); int id = responseHeader.getCallId();
@Override public boolean readBoolean() throws IOException { checkAvailable(1); return read() != 0; }
@Override public long readLong() throws IOException { checkAvailable(8); return buffer.readLong(); }
private void checkAvailable(int fieldSize) throws IOException { if (fieldSize < 0) { throw new IndexOutOfBoundsException("fieldSize cannot be a negative number"); } if (fieldSize > available()) { throw new EOFException("fieldSize is too long! Length is " + fieldSize + ", but maximum is " + available()); } } }
@Override public void readFully(byte[] b) throws IOException { readFully(b, 0, b.length); }
@Override public int readUnsignedByte() throws IOException { return readByte() & 0xff; }
@Override public float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); }
@Override public double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); }
private void readResponse(ChannelHandlerContext ctx, ByteBuf buf) throws IOException { int totalSize = buf.readInt(); ByteBufInputStream in = new ByteBufInputStream(buf); ResponseHeader responseHeader = ResponseHeader.parseDelimitedFrom(in); int id = responseHeader.getCallId();
@Override public short readShort() throws IOException { checkAvailable(2); return buffer.readShort(); }
@Override public int skipBytes(int n) throws IOException { int nBytes = Math.min(available(), n); buffer.skipBytes(nBytes); return nBytes; }
@Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket dp) throws Exception { ByteBufInputStream bis = new ByteBufInputStream(dp.content()); try { ClusterStatusProtos.ClusterStatus csp = ClusterStatusProtos.ClusterStatus.parseFrom(bis); ClusterMetrics ncs = ClusterMetricsBuilder.toClusterMetrics(csp); receive(ncs); } finally { bis.close(); } } }
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; } ObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver); try { return ois.readObject(); } finally { ois.close(); } } }
@Override public void readFully(byte[] b, int off, int len) throws IOException { checkAvailable(len); buffer.readBytes(b, off, len); }
@Override public int read(byte[] b, int off, int len) throws IOException { int available = available(); if (available == 0) { return -1; } len = Math.min(available, len); buffer.readBytes(b, off, len); return len; }
@Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket dp) throws Exception { ByteBufInputStream bis = new ByteBufInputStream(dp.content()); try { ClusterStatusProtos.ClusterStatus csp = ClusterStatusProtos.ClusterStatus.parseFrom(bis); ClusterMetrics ncs = ClusterMetricsBuilder.toClusterMetrics(csp); receive(ncs); } finally { bis.close(); } } }
private static X509Certificate[] getCertificatesFromBuffers(ByteBuf[] certs) throws CertificateException { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate[] x509Certs = new X509Certificate[certs.length]; int i = 0; try { for (; i < certs.length; i++) { InputStream is = new ByteBufInputStream(certs[i], true); try { x509Certs[i] = (X509Certificate) cf.generateCertificate(is); } finally { try { is.close(); } catch (IOException e) { // This is not expected to happen, but re-throw in case it does. throw new RuntimeException(e); } } } } finally { for (; i < certs.length; i++) { certs[i].release(); } } return x509Certs; }
@Override public int readInt() throws IOException { checkAvailable(4); return buffer.readInt(); }
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { final int length = in.readableBytes(); InputStream bbIn = null; ByteBufOutputStream bbOut = null; try { bbIn = new ByteBufInputStream(in); bbOut = new ByteBufOutputStream(out); bbOut.writeByte(properties); bbOut.writeInt(littleEndianDictionarySize); bbOut.writeLong(Long.reverseBytes(length)); encoder.code(bbIn, bbOut, -1, -1, null); } finally { if (bbIn != null) { bbIn.close(); } if (bbOut != null) { bbOut.close(); } } }