List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:io.airlift.drift.transport.netty.HeaderMessageEncoding.java
License:Apache License
private static int readVarint(ByteBuf messageHeader) { int result = 0; int shift = 0; while (true) { byte b = messageHeader.readByte(); result |= (b & 0x7f) << shift; if ((b & 0x80) != 0x80) { break; }//from www . j a v a2 s.c om shift += 7; } return result; }
From source file:io.andromeda.logcollector.LocalFileReader.java
License:Apache License
private Observable.Operator<String, Buffer> extractLines() { ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(8192); return subscriber -> new Subscriber<Buffer>() { @Override//from w w w. j ava 2s . c o m public void onCompleted() { if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } @Override public void onError(Throwable e) { if (!subscriber.isUnsubscribed()) { subscriber.onError(e); } } @Override public void onNext(Buffer buffer) { ByteBuf _buf = ((io.vertx.core.buffer.Buffer) buffer.getDelegate()).getByteBuf(); while (_buf.readableBytes() > 0) { byte b = _buf.readByte(); if ((b == '\n') || (b == '\r')) { byte[] _bArr = new byte[buf.readableBytes()]; buf.readBytes(_bArr); subscriber.onNext(new String(_bArr)); } else { buf.writeByte(b); } } buf.clear(); } }; }
From source file:io.aos.netty5.socksproxy.SocksPortUnificationServerHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { ChannelPipeline p = ctx.pipeline();/*from w w w . j a va2s .co m*/ SocksProtocolVersion version = SocksProtocolVersion.valueOf(in.readByte()); System.out.println(version); in.resetReaderIndex(); switch (version) { case SOCKS4a: p.addLast(new Socks4CmdRequestDecoder()); p.addLast(Socks4MessageEncoder.INSTANCE); break; case SOCKS5: p.addLast(new Socks5InitRequestDecoder()); p.addLast(Socks5MessageEncoder.INSTANCE); break; case UNKNOWN: in.clear(); ctx.close(); return; } p.addLast(SocksServerHandler.INSTANCE); p.remove(this); }
From source file:io.atomix.catalyst.transport.netty.NettyConnection.java
License:Apache License
/** * Handles response.//w ww . j a va 2s . c o m */ void handleResponse(ByteBuf response) { long requestId = response.readLong(); byte status = response.readByte(); switch (status) { case SUCCESS: try { handleResponseSuccess(requestId, readResponse(response)); } catch (SerializationException e) { handleResponseFailure(requestId, e); } break; case FAILURE: try { handleResponseFailure(requestId, readError(response)); } catch (SerializationException e) { handleResponseFailure(requestId, e); } break; } response.release(); }
From source file:io.atomix.catalyst.transport.netty.NettyHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext context, Object message) { ByteBuf buffer = (ByteBuf) message; int type = buffer.readByte(); switch (type) { case NettyConnection.REQUEST: handleRequest(buffer, context);/* ww w . java 2 s . c om*/ break; case NettyConnection.RESPONSE: handleResponse(buffer, context); break; } }
From source file:io.atomix.catalyst.transport.NettyConnection.java
License:Apache License
/** * Handles response./*from w w w .j a va 2s. c o m*/ */ void handleResponse(ByteBuf response) { long requestId = response.readLong(); byte status = response.readByte(); switch (status) { case SUCCESS: handleResponseSuccess(requestId, readResponse(response)); break; case FAILURE: handleResponseFailure(requestId, readError(response)); break; } response.release(); }
From source file:io.atomix.cluster.messaging.impl.MessageDecoder.java
License:Apache License
@Override @SuppressWarnings("squid:S128") // suppress switch fall through warning protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception { switch (currentState) { case READ_SENDER_VERSION: if (buffer.readableBytes() < SHORT_SIZE) { return; }// w w w .j a v a2s.c o m version = buffer.readShort(); currentState = DecoderState.READ_SENDER_IP; case READ_SENDER_IP: if (buffer.readableBytes() < BYTE_SIZE) { return; } buffer.markReaderIndex(); int octetsLength = buffer.readByte(); if (buffer.readableBytes() < octetsLength) { buffer.resetReaderIndex(); return; } byte[] octets = new byte[octetsLength]; buffer.readBytes(octets); senderIp = InetAddress.getByAddress(octets); currentState = DecoderState.READ_SENDER_PORT; case READ_SENDER_PORT: if (buffer.readableBytes() < INT_SIZE) { return; } senderPort = buffer.readInt(); address = new Address(senderIp.getHostName(), senderPort, senderIp); currentState = DecoderState.READ_TYPE; case READ_TYPE: if (buffer.readableBytes() < BYTE_SIZE) { return; } type = InternalMessage.Type.forId(buffer.readByte()); currentState = DecoderState.READ_PREAMBLE; case READ_PREAMBLE: if (buffer.readableBytes() < INT_SIZE) { return; } preamble = buffer.readInt(); currentState = DecoderState.READ_MESSAGE_ID; case READ_MESSAGE_ID: if (buffer.readableBytes() < LONG_SIZE) { return; } messageId = buffer.readLong(); currentState = DecoderState.READ_CONTENT_LENGTH; case READ_CONTENT_LENGTH: if (buffer.readableBytes() < INT_SIZE) { return; } contentLength = buffer.readInt(); currentState = DecoderState.READ_CONTENT; case READ_CONTENT: if (buffer.readableBytes() < contentLength) { return; } if (contentLength > 0) { // TODO: Perform a sanity check on the size before allocating content = new byte[contentLength]; buffer.readBytes(content); } else { content = EMPTY_PAYLOAD; } switch (type) { case REQUEST: currentState = DecoderState.READ_SUBJECT_LENGTH; break; case REPLY: currentState = DecoderState.READ_STATUS; break; default: checkState(false, "Must not be here"); } break; default: break; } switch (type) { case REQUEST: switch (currentState) { case READ_SUBJECT_LENGTH: if (buffer.readableBytes() < SHORT_SIZE) { return; } subjectLength = buffer.readShort(); currentState = DecoderState.READ_SUBJECT; case READ_SUBJECT: if (buffer.readableBytes() < subjectLength) { return; } final String subject = readString(buffer, subjectLength, UTF_8); InternalRequest message = new InternalRequest(preamble, messageId, address, subject, content); out.add(message); currentState = DecoderState.READ_TYPE; break; default: break; } break; case REPLY: switch (currentState) { case READ_STATUS: if (buffer.readableBytes() < BYTE_SIZE) { return; } InternalReply.Status status = InternalReply.Status.forId(buffer.readByte()); InternalReply message = new InternalReply(preamble, messageId, content, status); out.add(message); currentState = DecoderState.READ_TYPE; break; default: break; } break; default: checkState(false, "Must not be here"); } }
From source file:io.codis.nedis.handler.RedisResponseDecoder.java
License:Apache License
private Long decodeLong(ByteBuf in) throws ProtocolException { byte sign = in.readByte(); final MutableLong l; boolean negative; if (sign == '-') { negative = true;//from w w w . j av a 2 s .c om l = new MutableLong(0); } else { negative = false; l = new MutableLong(toDigit(sign)); } final MutableBoolean reachCR = new MutableBoolean(false); setReaderIndex(in, in.forEachByte(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { if (value == '\r') { reachCR.setTrue(); return false; } else { if (value >= '0' && value <= '9') { l.setValue(l.longValue() * 10 + toDigit(value)); } else { throw new ProtocolException("Response is not ended by CRLF"); } return true; } } })); if (!reachCR.booleanValue()) { return null; } if (!in.isReadable()) { return null; } if (in.readByte() != '\n') { throw new ProtocolException("Response is not ended by CRLF"); } return negative ? -l.longValue() : l.longValue(); }
From source file:io.codis.nedis.handler.RedisResponseDecoder.java
License:Apache License
private boolean decode(ByteBuf in, List<Object> out, Object nullValue) throws Exception { if (in.readableBytes() < 2) { return false; }//from w w w . ja va 2 s . c o m byte b = in.readByte(); switch (b) { case '+': { String reply = decodeString(in); if (reply == null) { return false; } out.add(reply); return true; } case '-': { String reply = decodeString(in); if (reply == null) { return false; } out.add(new RedisResponseException(reply)); return true; } case ':': { Long reply = decodeLong(in); if (reply == null) { return false; } out.add(reply); return true; } case '$': { Long numBytes = decodeLong(in); if (numBytes == null) { return false; } if (numBytes.intValue() == -1) { out.add(nullValue); return true; } if (in.readableBytes() < numBytes.intValue() + 2) { return false; } if (in.getByte(in.readerIndex() + numBytes.intValue()) != '\r' || in.getByte(in.readerIndex() + numBytes.intValue() + 1) != '\n') { throw new ProtocolException("Response is not ended by CRLF"); } byte[] reply = new byte[numBytes.intValue()]; in.readBytes(reply); // skip CRLF in.skipBytes(2); out.add(reply); return true; } case '*': { Long numReplies = decodeLong(in); if (numReplies == null) { return false; } if (numReplies.intValue() == -1) { out.add(nullValue); return true; } List<Object> replies = new ArrayList<>(); for (int i = 0; i < numReplies.intValue(); i++) { if (!decode(in, replies, null)) { return false; } } out.add(replies); return true; } default: throw new ProtocolException("Unknown leading char: " + (char) b); } }
From source file:io.crate.protocols.postgres.MessagesTest.java
License:Apache License
@Test public void testNullValuesAddToLength() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(); Messages.sendDataRow(channel, new RowN($(10, null)), Arrays.asList(DataTypes.INTEGER, DataTypes.STRING), null);/* w ww .j a va 2 s. c o m*/ channel.flush(); ByteBuf buffer = channel.readOutbound(); // message type assertThat((char) buffer.readByte(), is('D')); // size of the message assertThat(buffer.readInt(), is(16)); assertThat(buffer.readableBytes(), is(12)); // 16 - INT4 because the size was already read }