Example usage for io.netty.buffer ByteBuf getByte

List of usage examples for io.netty.buffer ByteBuf getByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf getByte.

Prototype

public abstract byte getByte(int index);

Source Link

Document

Gets a byte at the specified absolute index in this buffer.

Usage

From source file:org.apache.drill.exec.expr.fn.impl.StringFunctionUtil.java

License:Apache License

public static int utf8CharLen(ByteBuf buffer, int idx) {
    byte firstByte = buffer.getByte(idx);
    if (firstByte >= 0) { // 1-byte char. First byte is 0xxxxxxx.
        return 1;
    } else if ((firstByte & 0xE0) == 0xC0) { // 2-byte char. First byte is 110xxxxx
        return 2;
    } else if ((firstByte & 0xF0) == 0xE0) { // 3-byte char. First byte is 1110xxxx
        return 3;
    } else if ((firstByte & 0xF8) == 0xF0) { //4-byte char. First byte is 11110xxx
        return 4;
    }/*from   ww  w .  j  a va 2 s.  co m*/
    throw new DrillRuntimeException("Unexpected byte 0x" + Integer.toString((int) firstByte & 0xff, 16)
            + " at position " + idx + " encountered while decoding UTF8 string.");
}

From source file:org.apache.drill.exec.memory.TestEndianess.java

License:Apache License

@Test
public void testLittleEndian() {
    final BufferAllocator a = new RootAllocator(DrillConfig.getMaxDirectMemory());
    final ByteBuf b = a.buffer(4);
    b.setInt(0, 35);/*w w  w  .j  a  va2  s .  c  o m*/
    assertEquals(b.getByte(0), 35);
    assertEquals(b.getByte(1), 0);
    assertEquals(b.getByte(2), 0);
    assertEquals(b.getByte(3), 0);
    b.release();
    DrillAutoCloseables.closeNoChecked(a);
}

From source file:org.apache.jackrabbit.oak.plugins.segment.NetworkErrorProxy.java

License:Apache License

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBuf) {
        ByteBuf bb = (ByteBuf) msg;
        log.debug("FlipHandler. Got Buffer size: " + bb.readableBytes());
        if (this.startingPos >= 0) {
            if (this.transferredBytes + bb.readableBytes() >= this.startingPos) {
                int i = this.startingPos - (int) this.transferredBytes;
                log.info("FlipHandler flips byte at offset " + (this.transferredBytes + i));
                byte b = (byte) (bb.getByte(i) ^ 0x01);
                bb.setByte(i, b);/*  ww  w  . j  ava 2  s.  co  m*/
                this.startingPos = -1;
            }
        }
    }
    super.channelRead(ctx, msg);
}

From source file:org.apache.tajo.storage.TestSplitProcessor.java

License:Apache License

@Test
public void testLineSplitProcessor() throws IOException {
    String data = "abc\r\n\n";
    final ByteBuf buf = releaseLater(Unpooled.copiedBuffer(data, CharsetUtil.ISO_8859_1));

    final int len = buf.readableBytes();
    LineSplitProcessor processor = new LineSplitProcessor();

    //find CR/* w  w w .  java2 s  . c  o m*/
    assertEquals(3, buf.forEachByte(0, len, processor));

    // find CRLF
    assertEquals(4, buf.forEachByte(4, len - 4, processor));
    assertEquals(buf.getByte(4), '\n');
    // need to skip LF
    assertTrue(processor.isPrevCharCR());

    // find LF
    assertEquals(5, buf.forEachByte(5, len - 5, processor)); //line length is zero
}

From source file:org.code_house.ebus.netty.codec.OutboundEBusHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // receive//  ww  w .jav  a2s .  co  m
    if (msg instanceof MasterHeader) {
        this.masterHeader = (MasterHeader) msg;
    } else if (msg instanceof MasterData) {
        this.masterData = (MasterData) msg;
    }

    if (msg instanceof ByteBuf) {
        ByteBuf buffer = (ByteBuf) msg;
        if (writeInProgress && buffer.capacity() == 1) {
            byte aByte = buffer.getByte(0);
            if (device.getMaster().getAddress() == aByte) {
                // we got access to bus, lets try to flush something
                accessGranted = true;
                flush(ctx);
                return;
            }
        }
        writeInProgress = false;

        if (buffer.isReadable()) {
            byte lastByte = buffer.getByte(buffer.readerIndex());
            if (lastByte == Constants.SYN) {
                // we received SYN symbol, try to write own address
                writeInProgress = true;
                ctx.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { device.getMaster().getAddress() })); // force writing master address to bus
            }
        }
    }

    ctx.fireChannelRead(msg);
}

From source file:org.dcache.xrootd.protocol.messages.MkDirRequest.java

License:Open Source License

public MkDirRequest(ByteBuf buffer) {
    super(buffer, kXR_mkdir);

    options = buffer.getByte(4);
    mode = buffer.getUnsignedShort(18);
}

From source file:org.dcache.xrootd.protocol.messages.SigverRequest.java

License:Open Source License

public SigverRequest(ByteBuf buffer) {
    super(buffer, kXR_sigver);

    expectrid = buffer.getShort(4);/*  w ww  . j  a  v  a2s  . com*/
    version = buffer.getByte(6);
    flags = buffer.getByte(7); // should == kXR_nodata if this is a write
    seqno = buffer.getLong(8);
    crypto = buffer.getByte(16);

    /*
     * skip reserved [bytes 17-19]
     */

    int dlen = buffer.getInt(20);
    signature = new byte[dlen];
    buffer.getBytes(24, signature);
}

From source file:org.dcache.xrootd.tpc.protocol.messages.InboundProtocolResponse.java

License:Open Source License

public InboundProtocolResponse(ByteBuf buffer) throws XrootdException {
    super(buffer);
    int len = buffer.getInt(4);

    byte secopt = (byte) 0;
    int seclvl = kXR_secNone;
    Map<Integer, Integer> overrides = new HashMap<>();

    if (len >= 14) {
        secopt = buffer.getByte(19);
        seclvl = buffer.getByte(20);//from w ww  . j a va 2s.  co  m
        int secvsz = buffer.getByte(21);
        int index = 22;
        for (int i = 0; i < secvsz; ++i) {
            overrides.put((int) buffer.getByte(index++), (int) buffer.getByte(index++));
        }
    }

    signingPolicy = new SigningPolicy(seclvl, secopt, overrides);
}

From source file:org.ebayopensource.scc.cache.CacheResultVerifier.java

License:Apache License

protected String match(FullHttpResponse actualResp) {
    String eTag = HttpHeaders.getHeader(m_cacheResp, "ETag");
    String actualETag = HttpHeaders.getHeader(actualResp, "ETag");
    if (actualETag != null && actualETag.equals(eTag)) {
        return null;
    } else if (actualETag != null && !actualETag.equals(eTag)) {
        return String.format("ETag mismatch: cache - %s; actual - %s", eTag, actualETag);
    } else if (actualETag == null && eTag == null) {
        // match status code
        if (actualResp.getStatus().compareTo(m_cacheResp.getStatus()) != 0) {
            return String.format("Status code mismatch: cache - %s; actual - %s",
                    m_cacheResp.getStatus().code(), actualResp.getStatus().code());
        }/*from  w  w  w  .  j a va2 s  .  c om*/

        // match headers
        HttpHeaders actualHeaders = actualResp.headers();
        HttpHeaders headers = m_cacheResp.headers();
        List<String> mh = new ArrayList<>();// mismatched headers
        checkHeaderDiffer(actualHeaders, headers, mh);
        if (!mh.isEmpty()) {
            return String.format("Mismatched headers: %s", mh.toString());
        }
        checkHeaderDiffer(headers, actualHeaders, mh);
        if (!mh.isEmpty()) {
            return String.format("Mismatched headers: %s", mh.toString());
        }

        // match entity
        ByteBuf actualContent = actualResp.content();
        ByteBuf content = m_cacheResp.content();
        int len = actualContent.readableBytes();
        if (len != content.readableBytes()) {
            return "Mismatched body input stream.";
        }

        for (int i = 0; i < len; i++) {
            if (actualContent.getByte(i) != content.getByte(i)) {
                return "Mismatched body input stream.";
            }
        }
    }
    return null;
}

From source file:org.eclipse.californium.elements.tcp.DatagramFramer.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    while (in.readableBytes() > 0) {
        byte firstByte = in.getByte(in.readerIndex());
        int lengthNibble = (firstByte & 0xF0) >>> 4;
        int tokenNibble = firstByte & 0x0F;

        int lengthFieldSize = getLengthFieldSize(lengthNibble);
        int coapHeaderSize = getCoapHeaderSize(lengthFieldSize, tokenNibble);
        if (in.readableBytes() < coapHeaderSize) {
            // Not enough data, no point in continuing.
            return;
        }/*from  w w w  . j  a  v  a  2 s.  c  o  m*/

        int bodyLength = getBodyLength(in, lengthNibble, lengthFieldSize);
        if (in.readableBytes() < coapHeaderSize + bodyLength) {
            // Whole body not available yet.
            return;
        }

        byte[] data = new byte[coapHeaderSize + bodyLength];
        in.readBytes(data);

        Channel channel = ctx.channel();
        EndpointContext endpointContext = contextUtil.buildEndpointContext(channel);
        RawData rawData = RawData.inbound(data, endpointContext, false, ClockUtil.nanoRealtime());
        out.add(rawData);
    }
}