List of usage examples for io.netty.buffer ByteBufProcessor ByteBufProcessor
ByteBufProcessor
From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java
License:Apache License
/** * Parse out the info portion from the header part of the query response. * * This includes the total rows, but also debug info if attached. *//*www . ja v a 2 s . co m*/ private void parseViewInfo() { int rowsStart = -1; for (int i = responseContent.readerIndex(); i < responseContent.writerIndex() - 2; i++) { byte curr = responseContent.getByte(i); byte f1 = responseContent.getByte(i + 1); byte f2 = responseContent.getByte(i + 2); if (curr == '"' && f1 == 'r' && f2 == 'o') { rowsStart = i; break; } } if (rowsStart == -1) { return; } ByteBuf info = responseContent.readBytes(rowsStart - responseContent.readerIndex()); int closingPointer = info.forEachByteDesc(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { return value != ','; } }); if (closingPointer > 0) { info.setByte(closingPointer, '}'); viewInfoObservable.onNext(info); } else { viewInfoObservable.onNext(Unpooled.EMPTY_BUFFER); } viewInfoObservable.onCompleted(); viewParsingState = QUERY_STATE_ROWS; }
From source file:com.xx_dev.apn.socks.local.FakeHttpClientDecoder.java
License:Apache License
protected void _decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { switch (this.state()) { case READ_FAKE_HTTP: { int fakeHttpHeadStartIndex = in.readerIndex(); int fakeHttpHeadEndIndex = in.forEachByte(new ByteBufProcessor() { int c = 0; @Override// w w w. java 2s . c o m public boolean process(byte value) throws Exception { if (value == '\r' || value == '\n') { c++; } else { c = 0; } //logger.info("value=" + value + ", c=" + c); if (c >= 4) { return false; } else { return true; } } }); logger.debug("s: " + fakeHttpHeadStartIndex); logger.debug("e: " + fakeHttpHeadEndIndex); if (fakeHttpHeadEndIndex == -1) { logger.warn("w: " + fakeHttpHeadStartIndex); break; } byte[] buf = new byte[fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1]; in.readBytes(buf, 0, fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1); String s = TextUtil.fromUTF8Bytes(buf); //logger.info(s); String[] ss = StringUtils.split(s, "\r\n"); //System.out.println(s + "" + this + " " + Thread.currentThread().getName()); for (String line : ss) { if (StringUtils.startsWith(line, "X-C:")) { String lenStr = StringUtils.trim(StringUtils.split(line, ":")[1]); //System.out.println(lenStr + "" + this + " " + Thread.currentThread().getName()); //System.out.println("*****************************************"); try { length = Integer.parseInt(lenStr, 16); trafficLogger.info("D," + LocalConfig.ins().getUser() + "," + length); } catch (Throwable t) { logger.error("--------------------------------------"); logger.error(s + "" + this + " " + Thread.currentThread().getName()); logger.error("--------------------------------------"); } } } this.checkpoint(STATE.READ_CONTENT); } case READ_CONTENT: { if (length > 0) { byte[] buf = new byte[length]; in.readBytes(buf, 0, length); byte[] res = new byte[length]; for (int i = 0; i < length; i++) { res[i] = (byte) (buf[i] ^ (LocalConfig.ins().getEncryptKey() & 0xFF)); } ByteBuf outBuf = ctx.alloc().buffer(); outBuf.writeBytes(res); out.add(outBuf); } this.checkpoint(STATE.READ_FAKE_HTTP); break; } default: throw new Error("Shouldn't reach here."); } }
From source file:com.xx_dev.apn.socks.remote.FakeHttpServerDecoder.java
License:Apache License
protected void _decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { switch (this.state()) { case READ_FAKE_HTTP: { int fakeHttpHeadStartIndex = in.readerIndex(); int fakeHttpHeadEndIndex = in.forEachByte(new ByteBufProcessor() { int c = 0; @Override//w w w.j a v a 2s. c om public boolean process(byte value) throws Exception { if (value == '\r' || value == '\n') { c++; } else { c = 0; } //logger.info("value=" + value + ", c=" + c); if (c >= 4) { return false; } else { return true; } } }); logger.debug("s: " + fakeHttpHeadStartIndex); logger.debug("e: " + fakeHttpHeadEndIndex); if (fakeHttpHeadEndIndex == -1) { logger.warn("w: " + fakeHttpHeadStartIndex); break; } byte[] buf = new byte[fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1]; in.readBytes(buf, 0, fakeHttpHeadEndIndex - fakeHttpHeadStartIndex + 1); String s = TextUtil.fromUTF8Bytes(buf); //logger.info(s); String[] ss = StringUtils.split(s, "\r\n"); //System.out.println(s + "" + this + " " + Thread.currentThread().getName()); for (String line : ss) { if (StringUtils.startsWith(line, "X-C:")) { String lenStr = StringUtils.trim(StringUtils.split(line, ":")[1]); //System.out.println(lenStr + "" + this + " " + Thread.currentThread().getName()); //System.out.println("*****************************************"); try { length = Integer.parseInt(lenStr, 16); } catch (Throwable t) { logger.error("--------------------------------------"); logger.error(s + "" + this + " " + Thread.currentThread().getName()); logger.error("--------------------------------------"); } } if (StringUtils.startsWith(line, "X-U:")) { String user = StringUtils.trim(StringUtils.split(line, ":")[1]); ctx.channel().attr(NettyAttributeKey.LINK_USER).set(user); logger.info(user); } } this.checkpoint(STATE.READ_CONTENT); } case READ_CONTENT: { trafficLogger.info("U," + ctx.channel().attr(NettyAttributeKey.LINK_USER).get() + "," + length); if (length > 0) { byte[] buf = new byte[length]; in.readBytes(buf, 0, length); byte[] res = new byte[length]; for (int i = 0; i < length; i++) { res[i] = (byte) (buf[i] ^ (RemoteConfig.ins().getEncryptKey() & 0xFF)); } ByteBuf outBuf = ctx.alloc().buffer(); outBuf.writeBytes(res); out.add(outBuf); } this.checkpoint(STATE.READ_FAKE_HTTP); break; } default: throw new Error("Shouldn't reach here."); } }
From source file:io.codis.nedis.handler.RedisResponseDecoder.java
License:Apache License
private String decodeString(ByteBuf in) throws ProtocolException { final StringBuilder buffer = new StringBuilder(); final MutableBoolean reachCRLF = new MutableBoolean(false); setReaderIndex(in, in.forEachByte(new ByteBufProcessor() { @Override/*from w w w .j a v a 2 s. com*/ public boolean process(byte value) throws Exception { if (value == '\n') { if ((byte) buffer.charAt(buffer.length() - 1) != '\r') { throw new ProtocolException("Response is not ended by CRLF"); } else { buffer.setLength(buffer.length() - 1); reachCRLF.setTrue(); return false; } } else { buffer.append((char) value); return true; } } })); return reachCRLF.booleanValue() ? buffer.toString() : null; }
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 www. j a va 2 s . c o m*/ 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.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); }// ww w. j a v a 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:jj.repl.telnet.TelnetConnectionHandler.java
License:Apache License
private void dumpBuffer(String message, ByteBuf buffer) { System.out.println(message);//from w w w.j av a2 s. 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:jj.repl.telnet.TelnetSessionStatus.java
License:Apache License
TelnetSessionStatus parseResponse(final ByteBuf byteBuf) { int index = Unpooled.wrappedBuffer(byteBuf).forEachByte(new ByteBufProcessor() { @Override/*from w w w . j a v a 2s . c o m*/ public boolean process(byte value) throws Exception { return nextToken(value); } }); System.out.println(index); return this; }
From source file:org.opendaylight.controller.netconf.ssh.threads.Handshaker.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException { ByteBuf bb = (ByteBuf) msg;//w ww . ja v a 2s.c o m // we can block the server here so that slow client does not cause memory pressure try { bb.forEachByte(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { remoteOutputStream.write(value); return true; } }); } finally { bb.release(); } }
From source file:reactor.net.tcp.TcpServerTests.java
License:Open Source License
@Test public void exposesNettyByteBuf() throws InterruptedException { final int port = SocketUtils.findAvailableTcpPort(); final CountDownLatch latch = new CountDownLatch(msgs); TcpServer<ByteBuf, ByteBuf> server = new TcpServerSpec<ByteBuf, ByteBuf>(NettyTcpServer.class).env(env) .listen(port).dispatcher(SynchronousDispatcher.INSTANCE) .consume(new Consumer<NetChannel<ByteBuf, ByteBuf>>() { @Override/* w w w . ja v a2s . co m*/ public void accept(NetChannel<ByteBuf, ByteBuf> ch) { ch.consume(new Consumer<ByteBuf>() { @Override public void accept(ByteBuf byteBuf) { byteBuf.forEachByte(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { if (value == '\n') { latch.countDown(); } return true; } }); byteBuf.release(); } }); } }).get(); log.info("Starting raw server on tcp://localhost:{}", port); server.start().await(); for (int i = 0; i < threads; i++) { threadPool.submit(new DataWriter(port)); } try { assertTrue("Latch was counted down", latch.await(10, TimeUnit.SECONDS)); } finally { server.shutdown().await(); } }