List of usage examples for io.netty.buffer ByteBuf getByte
public abstract byte getByte(int index);
From source file:TestTCPServer.java
License:Open Source License
public static void main(String... args) throws Throwable { IOService service = new IOService(); TCPAcceptor acceptor = new TCPAcceptor(service); acceptor.setOption(ChannelOption.SO_BACKLOG, 3); acceptor.setChildOption(ChannelOption.TCP_NODELAY, true); acceptor.setChildOption(ChannelOption.SO_KEEPALIVE, true); acceptor.bind(4321);//w w w. j a v a 2 s .c om TCPSocket con = acceptor.accept(); ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16 * 1000); byte[] bytea = new byte[buffer.capacity()]; long bytes; System.out.println("Connected " + con.remoteEndpoint()); bytes = con.receive(buffer); while (bytes != -1 && buffer.getByte(0) != 4) { buffer.readBytes(bytea, 0, (int) bytes); System.out.print(new String(bytea, 0, (int) bytes)); buffer.readerIndex(0).writerIndex(0); con.send(buffer, (int) bytes); buffer.readerIndex(0).writerIndex(0); bytes = con.receive(buffer); } System.out.println("Connection closed"); con.close(); acceptor.close(); service.cancel(); }
From source file:alluxio.worker.grpc.AbstractWriteHandlerTest.java
License:Apache License
/** * @param buffer buffer to get checksum/* w w w . j a v a 2 s . co m*/ * @return the checksum */ public static long getChecksum(ByteBuf buffer) { long ret = 0; for (int i = 0; i < buffer.capacity(); i++) { ret += BufferUtils.byteToInt(buffer.getByte(i)); } return ret; }
From source file:cat.tbq.hospital.nio.echoExample.EchoClient2Handler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf byteBuf = (ByteBuf) msg; System.out.println(byteBuf.getByte(2)); ;// w ww . j av a 2 s. c om ctx.writeAndFlush(msg); }
From source file:cat.tbq.hospital.nio.echoExample.EchoClientHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf byteBuf = (ByteBuf) msg; int size = byteBuf.readableBytes(); for (int i = 0; i < size; i++) { System.out.println("client===" + (char) byteBuf.getByte(i)); ;//from w w w .j av a 2s .c om } // ctx.write(msg); }
From source file:cat.tbq.hospital.nio.echoExample.EchoServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf byteBuf = (ByteBuf) msg; int size = byteBuf.readableBytes(); for (int i = 0; i < size; i++) { System.out.println("===" + (char) byteBuf.getByte(i)); ;//from w w w . j a v a 2 s. co m } ByteBuf firstMessage = Unpooled.buffer(20); firstMessage.writeBytes("hello how are you".getBytes()); ctx.writeAndFlush(firstMessage); }
From source file:cloudeventbus.codec.Decoder.java
License:Open Source License
/** * Returns the number of bytes between the readerIndex of the haystack and * the first needle found in the haystack. -1 is returned if no needle is * found in the haystack.// www. j a v a2 s .c o m * <p/> * Copied from {@link io.netty.handler.codec.DelimiterBasedFrameDecoder}. */ private int indexOf(ByteBuf haystack, byte[] needle) { for (int i = haystack.readerIndex(); i < haystack.writerIndex(); i++) { int haystackIndex = i; int needleIndex; for (needleIndex = 0; needleIndex < needle.length; needleIndex++) { if (haystack.getByte(haystackIndex) != needle[needleIndex]) { break; } else { haystackIndex++; if (haystackIndex == haystack.writerIndex() && needleIndex != needle.length - 1) { return -1; } } } if (needleIndex == needle.length) { // Found the needle from the haystack! return i - haystack.readerIndex(); } } return -1; }
From source file:com.addthis.basis.chars.ReadOnlyAsciiBuf.java
License:Apache License
@Override public String toString() { // TODO: if our ByteBuf has a backing array, then we can use the deprecated, ascii-only // String constructor : new String(byte[], int, int, int) // Can't find a good way around String's stupid always-copy constructor, but by // not using content().toString(UTF8), we can at least prevent one extra allocation. //// w w w . ja v a 2 s .com // ((CharBuffer alloc, CharBuffer toString, new String) -> (char[] alloc, new String)) // // If desperate, _might_ be able to hack it with a dummy CharacterEncoder if there is // no security manager. Otherwise have to class path boot etc to get into the lang // package. I suppose annoyances like these are why I made this package. ByteBuf slice = content().slice(); char[] values = new char[slice.capacity()]; if (slice.readableBytes() > 0) { for (int i = 0; i < slice.capacity(); i++) { values[i] = (char) slice.getByte(i); } } else { return ""; } return new String(values); }
From source file:com.addthis.hydra.data.util.KeyTopper.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { map = new HashMap<>(); errors = null;//from w w w. j a v a2 s.c o m if (b.length == 0) { return; } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { byte marker = byteBuf.getByte(byteBuf.readerIndex()); if (marker == 0) { errors = new HashMap<>(); // Consume the sentinel byte value byteBuf.readByte(); } int mapSize = Varint.readUnsignedVarInt(byteBuf); try { if (mapSize > 0) { for (int i = 0; i < mapSize; i++) { int keyLength = Varint.readUnsignedVarInt(byteBuf); byte[] keybytes = new byte[keyLength]; byteBuf.readBytes(keybytes); String k = new String(keybytes, "UTF-8"); long value = Varint.readUnsignedVarLong(byteBuf); map.put(k, value); if (hasErrors()) { long error = Varint.readUnsignedVarLong(byteBuf); if (error != 0) { errors.put(k, error); } } } } } catch (Exception e) { throw Throwables.propagate(e); } } finally { byteBuf.release(); } }
From source file:com.alibaba.dubbo.qos.server.handler.QosProcessHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 1) { return;/*from w w w . j av a 2s. com*/ } // read one byte to guess protocol final int magic = in.getByte(in.readerIndex()); ChannelPipeline p = ctx.pipeline(); p.addLast(new LocalHostPermitHandler(acceptForeignIp)); if (isHttp(magic)) { // no welcome output for http protocol if (welcomeFuture != null && welcomeFuture.isCancellable()) { welcomeFuture.cancel(false); } p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(1048576)); p.addLast(new HttpProcessHandler()); p.remove(this); } else { p.addLast(new LineBasedFrameDecoder(2048)); p.addLast(new StringDecoder(CharsetUtil.UTF_8)); p.addLast(new StringEncoder(CharsetUtil.UTF_8)); p.addLast(new IdleStateHandler(0, 0, 5 * 60)); p.addLast(new TelnetProcessHandler()); p.remove(this); } }
From source file:com.allanbank.mongodb.netty.ByteToMessageDecoderTest.java
License:Apache License
/** * Test method for/*from w w w . j ava2s.c o m*/ * {@link ByteToMessageDecoder#extractFrame(ChannelHandlerContext, ByteBuf, int, int)} * . */ @Test public void testExtractFrameChannelHandlerContextByteBufIntInt() { final ByteBuf buffer = ourAllocator.buffer(); buffer.writeBytes(new byte[1000]); final ChannelHandlerContext mockContext = createMock(ChannelHandlerContext.class); replay(mockContext); final ByteToMessageDecoder decoder = new ByteToMessageDecoder(new StringDecoderCache()); final ByteBuf result = decoder.extractFrame(mockContext, buffer, 100, 200); assertThat(result, instanceOf(SlicedByteBuf.class)); assertThat(buffer.getByte(100), is((byte) 0)); result.setByte(0, 1); assertThat(buffer.getByte(100), is((byte) 1)); verify(mockContext); }