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:net.smert.frameworkgl.examples.networklinerepeater.LineRepeaterServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf buf = (ByteBuf) msg;
    int totalBytes = buf.readableBytes();
    if (totalBytes > 2) {
        byte last = buf.getByte(totalBytes - 1);
        byte nextToLast = buf.getByte(totalBytes - 2);
        if ((nextToLast == 13) && (last == 10)) {
            ctx.write(msg);/*ww w .  ja v a2 s  .  co  m*/
        }
    }
}

From source file:netty.syslog.DecoderUtil.java

License:Open Source License

static byte peek(ByteBuf buffer) {
    return buffer.getByte(buffer.readerIndex());
}

From source file:netty.syslog.DecoderUtil.java

License:Open Source License

static String readStringToSpace(ByteBuf buffer, boolean checkNull) {
    if (checkNull && peek(buffer) == '-') {
        buffer.readByte();/*from   w w  w  . jav a2 s.  c  o m*/
        return null;
    }
    int length = -1;
    for (int i = buffer.readerIndex(); i < buffer.capacity(); i++) {
        if (buffer.getByte(i) == ' ') {
            length = i - buffer.readerIndex();
            break;
        }
    }
    if (length < 0) {
        length = buffer.readableBytes();
    }
    final String s = buffer.toString(buffer.readerIndex(), length, CharsetUtil.UTF_8);
    buffer.skipBytes(length);
    return s;
}

From source file:nl.thijsalders.spigotproxy.haproxy.HAProxyMessageDecoder.java

License:Apache License

/**
 * Returns the proxy protocol specification version in the buffer if the version is found.
 * Returns -1 if no version was found in the buffer.
 *//*from   ww w  .  j  ava2 s.  c om*/
private static int findVersion(final ByteBuf buffer) {
    final int n = buffer.readableBytes();
    // per spec, the version number is found in the 13th byte
    if (n < 13) {
        return -1;
    }

    int idx = buffer.readerIndex();
    return match(BINARY_PREFIX, buffer, idx) ? buffer.getByte(idx + BINARY_PREFIX_LENGTH) : 1;
}

From source file:nl.thijsalders.spigotproxy.haproxy.HAProxyMessageDecoder.java

License:Apache License

/**
 * Returns the index in the buffer of the end of line found.
 * Returns -1 if no end of line was found in the buffer.
 *//*  www . ja  v a  2s.  co m*/
private static int findEndOfLine(final ByteBuf buffer) {
    final int n = buffer.writerIndex();
    for (int i = buffer.readerIndex(); i < n; i++) {
        final byte b = buffer.getByte(i);
        if (b == '\r' && i < n - 1 && buffer.getByte(i + 1) == '\n') {
            return i; // \r\n
        }
    }
    return -1; // Not found.
}

From source file:nl.thijsalders.spigotproxy.haproxy.HAProxyMessageDecoder.java

License:Apache License

/**
 * Create a frame out of the {@link ByteBuf} and return it.
 * Based on code from {@link LineBasedFrameDecoder#decode(ChannelHandlerContext, ByteBuf)}.
 *
 * @param ctx     the {@link ChannelHandlerContext} which this {@link HAProxyMessageDecoder} belongs to
 * @param buffer  the {@link ByteBuf} from which to read data
 * @return frame  the {@link ByteBuf} which represent the frame or {@code null} if no frame could
 *                be created//from  w  ww .  ja  v a 2 s  .  c  o m
 */
private ByteBuf decodeLine(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    final int eol = findEndOfLine(buffer);
    if (!discarding) {
        if (eol >= 0) {
            final int length = eol - buffer.readerIndex();
            if (length > V1_MAX_LENGTH) {
                buffer.readerIndex(eol + DELIMITER_LENGTH);
                failOverLimit(ctx, length);
                return null;
            }
            ByteBuf frame = buffer.readSlice(length);
            buffer.skipBytes(DELIMITER_LENGTH);
            return frame;
        } else {
            final int length = buffer.readableBytes();
            if (length > V1_MAX_LENGTH) {
                discardedBytes = length;
                buffer.skipBytes(length);
                discarding = true;
                failOverLimit(ctx, "over " + discardedBytes);
            }
            return null;
        }
    } else {
        if (eol >= 0) {
            final int delimLength = buffer.getByte(eol) == '\r' ? 2 : 1;
            buffer.readerIndex(eol + delimLength);
            discardedBytes = 0;
            discarding = false;
        } else {
            discardedBytes = buffer.readableBytes();
            buffer.skipBytes(discardedBytes);
        }
        return null;
    }
}

From source file:nl.thijsalders.spigotproxy.haproxy.HAProxyMessageDecoder.java

License:Apache License

private static boolean match(byte[] prefix, ByteBuf buffer, int idx) {
    for (int i = 0; i < prefix.length; i++) {
        final byte b = buffer.getByte(idx + i);
        if (b != prefix[i]) {
            return false;
        }/*from w w  w. j a v a2s. co  m*/
    }
    return true;
}

From source file:org.apache.activemq.artemis.protocol.amqp.proton.handler.ProtonHandler.java

License:Apache License

public void inputBuffer(ByteBuf buffer) {
    dataReceived = true;/*  www  .j a v  a2 s . com*/
    lock.lock();
    try {
        while (buffer.readableBytes() > 0) {
            int capacity = transport.capacity();

            if (!receivedFirstPacket) {
                try {
                    byte auth = buffer.getByte(4);
                    if (auth == SASL || auth == BARE) {
                        if (isServer) {
                            dispatchAuth(auth == SASL);
                        } else if (auth == BARE && clientSASLMechanism == null) {
                            dispatchAuthSuccess();
                        }
                        /*
                        * there is a chance that if SASL Handshake has been carried out that the capacity may change.
                        * */
                        capacity = transport.capacity();
                    }
                } catch (Throwable e) {
                    log.warn(e.getMessage(), e);
                }

                receivedFirstPacket = true;
            }

            if (capacity > 0) {
                ByteBuffer tail = transport.tail();
                int min = Math.min(capacity, buffer.readableBytes());
                tail.limit(min);
                buffer.readBytes(tail);

                flush();
            } else {
                if (capacity == 0) {
                    log.debugf("abandoning: readableBytes=%d", buffer.readableBytes());
                } else {
                    log.debugf("transport closed, discarding: readableBytes=%d, capacity=%d",
                            buffer.readableBytes(), transport.capacity());
                }
                break;
            }
        }
    } finally {
        lock.unlock();
    }
}

From source file:org.apache.activemq.artemis.tests.integration.transports.netty.ActiveMQFrameDecoder2Test.java

License:Apache License

@Test
public void testExtremeFragmentation() throws Exception {
    final EmbeddedChannel decoder = new EmbeddedChannel(new ActiveMQFrameDecoder2());

    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 4 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 5 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 6 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 7 }));
    Assert.assertNull(decoder.readInbound());
    decoder.writeInbound(Unpooled.wrappedBuffer(new byte[] { 8 }));

    ByteBuf frame = (ByteBuf) decoder.readInbound();
    Assert.assertEquals(4, frame.readerIndex());
    Assert.assertEquals(4, frame.readableBytes());
    Assert.assertEquals(5, frame.getByte(4));
    Assert.assertEquals(6, frame.getByte(5));
    Assert.assertEquals(7, frame.getByte(6));
    Assert.assertEquals(8, frame.getByte(7));
    frame.release();//from ww w.ja v a2 s  . co m
}

From source file:org.apache.activemq.artemis.utils.AbstractByteBufPool.java

License:Apache License

private static int byteBufHashCode(final ByteBuf byteBuf, final int offset, final int length) {
    final int intCount = length >>> 1;
    final int byteCount = length & 1;
    int hashCode = 1;
    int arrayIndex = offset;
    for (int i = 0; i < intCount; i++) {
        final short shortLE = byteBuf.getShortLE(arrayIndex);
        final short nativeShort = PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes(shortLE)
                : shortLE;//  w  ww.  j  a v a 2  s.  com
        hashCode = 31 * hashCode + nativeShort;
        arrayIndex += 2;
    }
    for (int i = 0; i < byteCount; i++) {
        hashCode = 31 * hashCode + byteBuf.getByte(arrayIndex++);
    }
    return hashCode;
}