Example usage for io.netty.buffer ByteBuf toString

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

Introduction

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

Prototype

public abstract String toString(Charset charset);

Source Link

Document

Decodes this buffer's readable bytes into a string with the specified character set name.

Usage

From source file:org.ebayopensource.scc.filter.NettyRequestProxyFilter.java

License:Apache License

private void debugRequestInfo(HttpObject httpObject, String key) {
    if (m_debugInfo && httpObject instanceof HttpRequest) {
        if (key != null) {
            LOGGER.debug("Cache Key: " + key);
        }//from  ww w.  jav  a2  s  .co m
        if (httpObject instanceof FullHttpRequest) {
            FullHttpRequest req = (FullHttpRequest) httpObject;
            HttpHeaders headers = req.headers();
            LOGGER.debug("Headers:");
            for (Iterator<Entry<String, String>> it = headers.iterator(); it.hasNext();) {
                Entry<String, String> entry = it.next();
                LOGGER.debug("\t" + entry.getKey() + ":\t" + entry.getValue());
            }
            ByteBuf content = req.content();
            int length = content.readableBytes();
            LOGGER.debug("Content Length: " + length);
            if (length != 0) {
                LOGGER.debug("Content: " + content.toString(Charset.forName("UTF-8")));
            }
        }
    }
}

From source file:org.eclipse.scada.protocol.relp.FrameCodec.java

License:Open Source License

private void processLENGTH(final ChannelHandlerContext ctx, final byte b) {
    final ByteBuf lengthBuffer = ctx.attr(ATTR_LENGTH_BUFFER).get();

    if (b == Constants.SP || lengthBuffer.readableBytes() > 0 && b == Constants.LF) {
        // either we have a SP or at least one byte (possibly "0") and a LF

        final int length = Integer.parseInt(lengthBuffer.toString(LENGTH_CHARSET));

        ctx.attr(ATTR_EXPECTED_LENGTH).set(length);
        ctx.attr(ATTR_STATE).set(State.DATA);
    } else {/*from w ww  .j  a  va 2  s . c o  m*/
        lengthBuffer.writeByte(b);
    }
}

From source file:org.eclipse.scada.protocol.syslog.SyslogCodec.java

License:Open Source License

private String decodeMessage(final ByteBuf msg) {
    final String data = msg.toString(StandardCharsets.UTF_8);

    final StringBuilder sb = new StringBuilder();

    for (int i = 0; i < data.length(); i++) {
        final char c = data.charAt(i);
        if (c == '#') {
            try {
                final int code = Integer.parseInt(data.substring(i + 1, i + 4), 8);
                sb.append((char) code);
                i += 3;/*from  www . j a v a2s.  c  o m*/
            } catch (final NumberFormatException e) {
                sb.append(c);
            }
        } else {
            sb.append(c);
        }
    }

    return sb.toString();
}

From source file:org.graylog.collector.file.splitters.NewlineChunkSplitter.java

License:Open Source License

@Override
public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) {
    return new Iterable<String>() {
        @Override/*from w  ww  . ja  va  2s . c  o m*/
        public Iterator<String> iterator() {
            return new AbstractIterator<String>() {

                @Override
                protected String computeNext() {
                    try {
                        if (!buffer.isReadable()) {
                            return endOfData();
                        }
                        final int i = buffer.forEachByte(ByteBufProcessor.FIND_CRLF);
                        if (i == -1) {
                            if (includeRemainingData) {
                                final ByteBuf remaining = buffer.readBytes(buffer.readableBytes());
                                return remaining.toString(charset);
                            } else {
                                return endOfData();
                            }
                        }
                        final ByteBuf fullLine = buffer.readBytes(i);
                        // Strip the \r/\n bytes from the buffer.
                        final byte readByte = buffer.readByte(); // the \r or \n byte
                        if (readByte == '\r') {
                            buffer.readByte(); // the \n byte if previous was \r
                        }
                        return fullLine.toString(charset);
                    } finally {
                        buffer.discardReadBytes();
                    }
                }
            };
        }
    };
}

From source file:org.graylog.collector.file.splitters.PatternChunkSplitter.java

License:Open Source License

@Override
public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) {
    return new Iterable<String>() {
        @Override/*from   w  w  w.  j  a  va 2s  .c  o m*/
        public Iterator<String> iterator() {
            return new AbstractIterator<String>() {
                // TODO Might throw an exception if multibyte charset is used and buffer is not complete.
                //      Use CharsetDecoder to create a CharBuffer and match on that!
                private final String inputAsString = buffer.toString(charset);
                final Matcher matcher = pattern.matcher(inputAsString);
                private int positionInString = 0;

                @Override
                protected String computeNext() {
                    try {
                        if (!buffer.isReadable()) {
                            return endOfData();
                        }
                        if (matcher.find()) {
                            int firstByte = matcher.start();
                            if (firstByte == 0) {
                                // advance further, the buffer begins with our pattern.
                                if (matcher.find()) {
                                    firstByte = matcher.start();
                                } else {
                                    if (!includeRemainingData) {
                                        // couldn't find the end of the entry (i.e. there wasn't a next line yet)
                                        return endOfData();
                                    } else {
                                        // couldn't find another line, but we are asked to finish up, include everything that remains
                                        return getRemainingContent();
                                    }
                                }
                            }
                            if (firstByte == 0) {
                                // still haven't found a non-zero length string, keep waiting for more data.
                                return endOfData();
                            }
                            final String substring = inputAsString.substring(positionInString, firstByte);
                            positionInString = firstByte;
                            buffer.skipBytes(substring.getBytes(charset).length); // TODO performance
                            return substring;
                        } else {
                            if (includeRemainingData) {
                                return getRemainingContent();
                            }
                            return endOfData();
                        }
                    } catch (IllegalStateException e) {
                        // the cause contains the CharacterCodingException from the ChannelBuffer.toString() methods
                        // this usually means the buffer ended with an incomplete encoding of a unicode character.
                        // WHY U SO SUCK CHARACTER ENCODINGS?
                        // we need to wait until more data is available
                        return endOfData();
                    } finally {
                        buffer.discardReadBytes();
                    }
                }

                private String getRemainingContent() {
                    final ByteBuf channelBuffer = buffer.readBytes(buffer.readableBytes());
                    return channelBuffer.toString(charset);
                }
            };
        }
    };
}

From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoderTest.java

License:Open Source License

@Test
public void testDecode() throws Exception {
    final ByteBuf buf1 = Unpooled.copiedBuffer(
            "123 <45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            StandardCharsets.US_ASCII);
    final ByteBuf buf2 = Unpooled.copiedBuffer(
            "186 <45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"2\"] Syslog connection established; fd='9', server='AF_INET(172.17.42.1:6666)', local='AF_INET(0.0.0.0:0)'\n",
            StandardCharsets.US_ASCII);
    final ByteBuf buf3 = Unpooled.copiedBuffer(buf1, buf2, buf1);

    assertTrue(channel.writeInbound(buf1, buf2, buf3));

    final ByteBuf actual1 = channel.readInbound();
    assertEquals(/* ww  w . j av a  2 s . co m*/
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            actual1.toString(StandardCharsets.US_ASCII));
    final ByteBuf actual2 = channel.readInbound();
    assertEquals(
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"2\"] Syslog connection established; fd='9', server='AF_INET(172.17.42.1:6666)', local='AF_INET(0.0.0.0:0)'\n",
            actual2.toString(StandardCharsets.US_ASCII));

    final ByteBuf actual3 = channel.readInbound();
    assertEquals(
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            actual3.toString(StandardCharsets.US_ASCII));
    final ByteBuf actual4 = channel.readInbound();
    assertEquals(
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"2\"] Syslog connection established; fd='9', server='AF_INET(172.17.42.1:6666)', local='AF_INET(0.0.0.0:0)'\n",
            actual4.toString(StandardCharsets.US_ASCII));
    final ByteBuf actual5 = channel.readInbound();
    assertEquals(
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            actual5.toString(StandardCharsets.US_ASCII));

    assertNull(channel.readInbound());
}

From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoderTest.java

License:Open Source License

@Test
public void testIncompleteFrameLengthValue() throws Exception {
    final ByteBuf buf1 = Unpooled.copiedBuffer("12", StandardCharsets.US_ASCII);
    final ByteBuf buf2 = Unpooled.copiedBuffer(
            "3 <45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            StandardCharsets.US_ASCII);

    assertFalse(channel.writeInbound(buf1));
    assertNull(channel.readInbound());//from  w  ww. ja  v a2 s.  c  o  m

    assertTrue(channel.writeInbound(buf2));
    final ByteBuf actual = channel.readInbound();
    assertEquals(
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            actual.toString(StandardCharsets.US_ASCII));
}

From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoderTest.java

License:Open Source License

@Test
public void testIncompleteFrames() throws Exception {
    final ByteBuf buf1 = Unpooled.copiedBuffer(
            "123 <45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - ", StandardCharsets.US_ASCII);
    final ByteBuf buf2 = Unpooled.copiedBuffer(
            "[meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n", StandardCharsets.US_ASCII);

    assertFalse(channel.writeInbound(buf1));
    assertNull(channel.readInbound());/*from w w w. jav  a 2  s  . c  o m*/

    assertTrue(channel.writeInbound(buf2));
    final ByteBuf actual = channel.readInbound();
    assertEquals(
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            actual.toString(StandardCharsets.US_ASCII));
}

From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoderTest.java

License:Open Source License

@Test
public void testIncompleteByteBufByteBufFramesAndSmallBuffer() throws Exception {
    /*//from  www .  j a  va  2s  .  c  om
     * This test has been added to reproduce this issue: https://github.com/Graylog2/graylog2-server/issues/1105
     *
     * It triggers an edge case where the buffer is missing <frame size value length + 1> bytes.
     * The SyslogOctetCountFrameDecoder was handling this wrong in previous versions and tried to read more from
     * the buffer than there was available after the frame size value bytes have been skipped.
     */
    final ByteBuf messagePart1 = Unpooled.copiedBuffer(
            "123 <45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.",
            StandardCharsets.US_ASCII);
    final ByteBuf messagePart2 = Unpooled.copiedBuffer("3'\n", StandardCharsets.US_ASCII);

    assertFalse(channel.writeInbound(messagePart1));
    assertNull(channel.readInbound());
    assertTrue(channel.writeInbound(messagePart2));

    final ByteBuf actual = channel.readInbound();
    assertEquals(
            "<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - [meta sequenceId=\"1\"] syslog-ng starting up; version='3.5.3'\n",
            actual.toString(StandardCharsets.US_ASCII));
}

From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoderTest.java

License:Open Source License

@Test
public void testDecodeSupportsMessagesLongerThan1024Bytes() throws Exception {
    // All transport receiver
    // implementations SHOULD be able to accept messages of up to and
    // including 2048 octets in length.  Transport receivers MAY receive
    // messages larger than 2048 octets in length.
    // -- https://tools.ietf.org/html/rfc5424#section-6.1
    final byte[] bytes = new byte[2048];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) ('A' + (i % 26));
    }/*from   www. ja va 2 s.c o  m*/
    final String longString = new String(bytes, StandardCharsets.US_ASCII);
    final ByteBuf buffer = Unpooled.copiedBuffer(
            "2111 <45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - " + longString + "\n",
            StandardCharsets.US_ASCII);

    assertTrue(channel.writeInbound(buffer));
    channel.finish();

    final ByteBuf actual = channel.readInbound();
    assertEquals("<45>1 2014-10-21T10:21:09+00:00 c4dc57ba1ebb syslog-ng 7120 - " + longString + "\n",
            actual.toString(StandardCharsets.US_ASCII));
    assertNull(channel.readInbound());
}