List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract ByteBuf readerIndex(int readerIndex);
From source file:io.jsync.buffer.Buffer.java
License:Open Source License
/** * Appends the specified {@code Buffer} to the end of this Buffer. The buffer will expand as necessary to accommodate * any bytes written.<p>/*from w w w . j a v a 2 s .c om*/ * Returns a reference to {@code this} so multiple operations can be appended together. */ public Buffer appendBuffer(Buffer buff) { ByteBuf cb = buff.getByteBuf(); buffer.writeBytes(buff.getByteBuf()); cb.readerIndex(0); // Need to reset readerindex since Netty write modifies readerIndex of source! return this; }
From source file:io.lettuce.core.protocol.RedisStateMachine.java
License:Apache License
private ByteBuffer readBytes(ByteBuf buffer, int count) { ByteBuffer bytes = null;/* w ww . j a v a 2s . c o m*/ if (buffer.readableBytes() >= count) { responseElementBuffer.clear(); int size = count - 2; if (responseElementBuffer.capacity() < size) { responseElementBuffer.capacity(size); } buffer.readBytes(responseElementBuffer, size); bytes = responseElementBuffer.internalNioBuffer(0, size); buffer.readerIndex(buffer.readerIndex() + 2); } return bytes; }
From source file:io.reactivex.netty.protocol.http.sse.ServerSentEventDecoder.java
License:Apache License
protected static boolean skipTillMatching(ByteBuf byteBuf, ByteBufProcessor processor) { final int lastIndexProcessed = byteBuf.forEachByte(processor); if (-1 == lastIndexProcessed) { byteBuf.readerIndex(byteBuf.readerIndex() + byteBuf.readableBytes()); // If all the remaining bytes are to be ignored, discard the buffer. } else {/* w w w . j a v a2 s . c om*/ byteBuf.readerIndex(lastIndexProcessed); } return -1 != lastIndexProcessed; }
From source file:io.reactivex.netty.protocol.text.StringLineDecoder.java
License:Apache License
private void skipLineDelimiters(ByteBuf in) { for (;;) {//from w ww. j a va2 s . c om char c = (char) in.readByte(); if (isLineDelimiter(c)) { continue; } // Leave the reader index at the first letter of the next line, if any in.readerIndex(in.readerIndex() - 1); checkpoint(State.NEW_LINE); break; } }
From source file:me.melchor9000.net.resolver.DNSUtils.java
License:Open Source License
private static String readName(ByteBuf b, int position, int finalPos) { int length;// w w w . ja v a2 s . co m b.readerIndex(position); StringBuilder domain = new StringBuilder(); while ((length = b.readByte()) != 0) { if ((length & 0xC0) != 0xC0) { if (b.readableBytes() < length) throw new DataNotRepresentsObject("Incomplete name", b); byte label[] = new byte[length]; b.readBytes(label); domain.append(".").append(new String(label)); } else { domain.append(".").append(readName(b, b.readByte(), b.readerIndex())); } } if (finalPos != -1) b.readerIndex(finalPos); return domain.length() > 0 ? domain.substring(1) : domain.toString(); }
From source file:mysql.client.Session_Old.java
public void send(ByteBuf packet) throws IOException { this.packetSequence++; int position = packet.writerIndex(); packet.readerIndex(0); packet.writerIndex(0);//from w w w. ja va 2s. co m int size = position - 4; packet.writeByte((byte) (size & 0xff)); packet.writeByte((byte) (size >>> 8)); packet.writeByte((byte) (size >>> 16)); packet.writeByte(packetSequence); socket.getOutputStream().write(packet.array()); socket.getOutputStream().flush(); }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterResponseDecoderTest.java
License:Open Source License
public static ByteBuf createBuf() { ByteBuf buf = Unpooled.buffer(1024); int randShift = new Random().nextInt(10) + 7; buf.writerIndex(randShift);//from www .ja va 2 s . co m buf.readerIndex(randShift); return buf; }
From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveRequestDecoderTest.java
License:Open Source License
public static ByteBuf createBuf() { ByteBuf buf = Unpooled.buffer(); int randShift = new Random().nextInt(10) + 7; buf.writerIndex(randShift);//from ww w .j a v a2s. c o m buf.readerIndex(randShift); return buf; }
From source file:net.epsilony.utils.codec.modbus.reqres.ModbusResponseTest.java
License:Open Source License
@Test public void testEncode() { TestData[] datas = new TestData[] { new TestData(0xFEED, 0x83, 0x81, new int[] { 0xFE, 0xED, 0x00, 0x00, 0x00, 0x05, 0x83, 0x81, 0xAB, 0xCD, 0xEF }) }; ByteBuf buf = Unpooled.buffer(); int randShift = new Random().nextInt(10) + 7; buf.writerIndex(randShift);/*from ww w. ja va2 s.c o m*/ buf.readerIndex(randShift); for (TestData data : datas) { ModbusResponse response = data.createResponse(); response.encode(buf); data.assertBuffer(buf); } }
From source file:net.ieldor.utility.flooder.FlooderChannelHandler.java
License:Open Source License
@Override public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf buf) throws IOException { Channel channel = ctx.channel(); if (state == State.READ_SERVER_SESSION_KEY) { if (buf.readableBytes() >= 9) { if (buf.readUnsignedByte() != 0) throw new IOException("expecting EXCHANGE_KEYS opcode"); serverSessionKey = buf.readLong(); ByteBuf payload = Unpooled.buffer(); payload.writeInt(530);// w w w.ja v a 2 s. c om payload.writeByte(0); payload.writeByte(0); payload.writeByte(0); payload.writeByte(0); payload.writeShort(765); payload.writeShort(503); payload.writeByte(0); for (int i = 0; i < 24; i++) payload.writeByte(0); ByteBufUtils.writeString(payload, "kKmok3kJqOeN6D3mDdihco3oPeYN2KFy6W5--vZUbNA"); payload.writeInt(0); payload.writeInt(0); payload.writeShort(0); for (int i = 0; i < 28; i++) { payload.writeInt(crc[i]); } payload.writeByte(10); payload.writeLong(clientSessionKey); payload.writeLong(serverSessionKey); payload.writeLong(encodedUsername); ByteBufUtils.writeString(payload, password); ByteBuf packet = Unpooled.buffer(); packet.writeByte(18); packet.writeShort(payload.readableBytes()); packet.writeBytes(payload); channel.write(packet); int[] seed = new int[4]; seed[0] = (int) (clientSessionKey >> 32); seed[1] = (int) clientSessionKey; seed[2] = (int) (serverSessionKey >> 32); seed[3] = (int) serverSessionKey; state = State.READ_LOGIN_STATUS; } } if (state == State.READ_LOGIN_STATUS) { if (buf.readable()) { int status = buf.readUnsignedByte(); if (status != 2) throw new IOException("expecting OK login response"); state = State.READ_LOGIN_PAYLOAD; } } if (state == State.READ_LOGIN_PAYLOAD) { if (buf.readableBytes() >= 11) { buf.readerIndex(buf.readerIndex() + 11); state = State.READ_GAME_OPCODE; } } if (state == State.READ_GAME_OPCODE) { } }