List of usage examples for io.netty.buffer ByteBuf forEachByte
public abstract int forEachByte(ByteProcessor processor);
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 a 2s .c o m byteBuf.readerIndex(lastIndexProcessed); } return -1 != lastIndexProcessed; }
From source file:io.reactivex.netty.protocol.http.sse.ServerSentEventEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, ServerSentEvent serverSentEvent, ByteBuf out) throws Exception { if (serverSentEvent.hasEventType()) { // Write event type, if available out.writeBytes(EVENT_PREFIX_BYTES); out.writeBytes(serverSentEvent.getEventType()); out.writeBytes(NEW_LINE_AS_BYTES); }//from w w w . j ava 2 s .c o m if (serverSentEvent.hasEventId()) { // Write event id, if available out.writeBytes(ID_PREFIX_AS_BYTES); out.writeBytes(serverSentEvent.getEventId()); out.writeBytes(NEW_LINE_AS_BYTES); } final ByteBuf content = serverSentEvent.content(); if (splitSseData) { while (content.isReadable()) { // Scan the buffer and split on new line into multiple data lines. final int readerIndexAtStart = content.readerIndex(); int newLineIndex = content.forEachByte(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { return (char) value != '\n'; } }); if (-1 == newLineIndex) { // No new line, write the buffer as is. out.writeBytes(DATA_PREFIX_AS_BYTES); out.writeBytes(content); out.writeBytes(NEW_LINE_AS_BYTES); } else { // Write the buffer till the new line and then iterate this loop out.writeBytes(DATA_PREFIX_AS_BYTES); out.writeBytes(content, newLineIndex - readerIndexAtStart); content.readerIndex(content.readerIndex() + 1); out.writeBytes(NEW_LINE_AS_BYTES); } } } else { // write the buffer with data prefix and new line post fix. out.writeBytes(DATA_PREFIX_AS_BYTES); out.writeBytes(content); out.writeBytes(NEW_LINE_AS_BYTES); } }
From source file:io.scalecube.socketio.serialization.PacketDecoder.java
License:Apache License
public static Packet decodePacket(final ByteBuf payload) throws IOException { int payloadSize = payload.readableBytes(); // Decode packet type int typeDelimiterIndex = payload.forEachByte(packetDelimiterFinder); if (typeDelimiterIndex == -1) { return Packet.NULL_INSTANCE; }/*from w w w. ja v a 2 s. co m*/ ByteBuf typeBytes = payload.slice(0, typeDelimiterIndex); String typeString = typeBytes.toString(CharsetUtil.UTF_8); int typeId = Integer.valueOf(typeString); PacketType type = PacketType.valueOf(typeId); // Skip message id int messageIdDelimiterIndex = payload.forEachByte(typeDelimiterIndex + 1, payloadSize - typeDelimiterIndex - 1, packetDelimiterFinder); if (messageIdDelimiterIndex == -1) { return Packet.NULL_INSTANCE; } // Skip endpoint int endpointDelimiterIndex = payload.forEachByte(messageIdDelimiterIndex + 1, payloadSize - messageIdDelimiterIndex - 1, packetDelimiterFinder); // Create instance of packet Packet packet = new Packet(type); // Decode data boolean messagingType = type == PacketType.MESSAGE || type == PacketType.JSON; if (endpointDelimiterIndex != -1 && messagingType) { int dataLength = payloadSize - endpointDelimiterIndex - 1; if (dataLength > 0) { ByteBuf data = payload.copy(endpointDelimiterIndex + 1, dataLength); packet.setData(data); } } return packet; }
From source file:jj.repl.telnet.TelnetConnectionHandler.java
License:Apache License
private void dumpBuffer(String message, ByteBuf buffer) { System.out.println(message);/* ww w . ja v a2s. c o m*/ buffer.forEachByte(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { // TODO Auto-generated method stub System.out.print(Integer.toHexString(((int) value) & 255)); System.out.print(" "); return true; } }); System.out.println(); // and linebreak }
From source file:org.asynchttpclient.netty.util.UsAsciiByteBufDecoder.java
License:Open Source License
public String decode(Iterable<ByteBuf> bufs) { for (ByteBuf buf : bufs) { buf.forEachByte(b -> { sb.append((char) b); return true; });/*from ww w . j a va2 s . c om*/ } return sb.toString(); }
From source file:org.asynchttpclient.netty.util.Utf8ByteBufDecoder.java
License:Open Source License
public String decode(Iterable<ByteBuf> bufs) throws CharacterCodingException { for (ByteBuf buf : bufs) { buf.forEachByte(value -> { write(value);/*w w w .j a va 2 s . c om*/ return true; }); } if (state == UTF8_ACCEPT) { return sb.toString(); } else { throw new CharacterCodingException(); } }
From source file:org.codice.alliance.video.stream.mpegts.netty.StartCodeTest.java
License:Open Source License
@Test public void testBasicCase() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 0x00, 0x00, 0x01, 0x42 }); int position = byteBuf.forEachByte(startCode); assertThat(position, is(3));//from w w w. j av a 2s. c om }
From source file:org.codice.alliance.video.stream.mpegts.netty.StartCodeTest.java
License:Open Source License
@Test public void testExtraLeadingZeroes() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0x42 }); int position = byteBuf.forEachByte(startCode); assertThat(position, is(5));/*from w ww. java 2 s . com*/ }
From source file:org.codice.alliance.video.stream.mpegts.netty.StartCodeTest.java
License:Open Source License
@Test public void testVariousLeadingBytes() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 0x04, 0x05, 0x00, 0x00, 0x01, 0x42 }); int position = byteBuf.forEachByte(startCode); assertThat(position, is(5));// www .j a v a 2 s . c o m }
From source file:org.codice.alliance.video.stream.mpegts.netty.StartCodeTest.java
License:Open Source License
@Test public void testCodeDoesntExist() { ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 0x04, 0x05, 0x02, 0x00, 0x01, 0x42 }); int position = byteBuf.forEachByte(startCode); assertThat(position, is(-1));// ww w. j av a2s . c o m }