Example usage for io.netty.buffer ByteBuf bytesBefore

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

Introduction

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

Prototype

public abstract int bytesBefore(byte value);

Source Link

Document

Locates the first occurrence of the specified value in this buffer.

Usage

From source file:io.crate.protocols.postgres.PostgresWireProtocol.java

License:Apache License

@Nullable
private static char[] readCharArray(ByteBuf buffer) {
    byte[] bytes = new byte[buffer.bytesBefore((byte) 0) + 1];
    if (bytes.length == 0) {
        return null;
    }/*from w w w .ja  v a 2  s  . com*/
    buffer.readBytes(bytes);
    return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).array();
}

From source file:io.liveoak.container.protocols.ProtocolDetector.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    int nonNewlineBytes = in.bytesBefore((byte) '\n');

    in.markReaderIndex();/*from w  w w  .  j av a  2  s.  c om*/

    if (nonNewlineBytes > 0) {
        ByteBuf lineBuffer = in.readBytes(nonNewlineBytes);
        String line = lineBuffer.toString(UTF_8);

        //SslHandler sslHandler = context.getPipeline().writeState( SslHandler.class );

        in.resetReaderIndex();
        ByteBuf fullBuffer = in.readBytes(super.actualReadableBytes());

        if (line.startsWith("CONNECT") || line.startsWith("STOMP")) {
            this.configurator.switchToPureStomp(ctx.pipeline());
        } else {
            this.configurator.switchToHttpWebSockets(ctx.pipeline());
        }

        ctx.pipeline().fireChannelRead(fullBuffer);
    }
}

From source file:io.liveoak.stomp.common.StompFrameDecoder.java

License:Open Source License

protected ByteBuf readUntilNull(ByteBuf buffer) {
    int nonNullBytes = buffer.bytesBefore((byte) 0x00);

    ByteBuf content = null;/*from  ww  w .  j a  v  a  2  s.  c o  m*/
    if (nonNullBytes == 0) {
        content = Unpooled.EMPTY_BUFFER;
    } else {
        content = buffer.readBytes(nonNullBytes);
    }

    buffer.readByte();
    return content;
}

From source file:io.liveoak.stomp.common.StompFrameDecoder.java

License:Open Source License

protected FrameHeader decodeHeader(ByteBuf buffer) {
    FrameHeader header = null;//from   w w w  . jav a2s. c om

    while (header == null || buffer.isReadable()) {
        int nonNewLineBytes = buffer.bytesBefore((byte) '\n');

        if (nonNewLineBytes == 0) {
            buffer.readByte();
            break;
        }
        if (nonNewLineBytes >= 0) {
            ByteBuf line = buffer.readBytes(nonNewLineBytes);
            buffer.readByte();
            header = processHeaderLine(header, line.toString(UTF_8));
        }
    }

    return header;
}

From source file:io.reactiverse.pgclient.impl.codec.util.Util.java

License:Apache License

public static String readCString(ByteBuf src, Charset charset) {
    int len = src.bytesBefore(ZERO);
    String s = src.readCharSequence(len, charset).toString();
    src.readByte();//  w ww . j av a2  s  .  co  m
    return s;
}

From source file:io.reactiverse.pgclient.impl.codec.util.Util.java

License:Apache License

public static String readCStringUTF8(ByteBuf src) {
    int len = src.bytesBefore(ZERO);
    String s = src.readCharSequence(len, UTF_8).toString();
    src.readByte();/*w  w w.j  av a  2 s .c o  m*/
    return s;
}

From source file:org.apache.camel.component.hl7.HL7MLLPNettyDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    ByteBuf buf = (ByteBuf) super.decode(ctx, buffer);
    if (buf != null) {
        int pos = buf.bytesBefore((byte) config.getStartByte());
        if (pos >= 0) {
            ByteBuf msg = buf.readerIndex(pos + 1).slice();
            LOG.debug("Message ends with length {}", msg.readableBytes());
            return config.isProduceString() ? asString(msg) : asByteArray(msg);
        } else {//w  ww  . j a v a  2  s.c o m
            throw new DecoderException("Did not find start byte " + (int) config.getStartByte());
        }
    }
    // Message not complete yet - return null to be called again
    LOG.debug("No complete messages yet at position {}", buffer.readableBytes());
    return null;
}

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

License:Open Source License

private String[] decodeProcess(final ByteBuf msg) {
    // split by colon
    final int spaceIndex = msg.bytesBefore(COLON);
    if (spaceIndex < 0) {
        throw new CodecException("Unable to find process name");
    }/* w  ww  .  j a  v  a  2s .com*/

    final String process = msg.readSlice(spaceIndex).toString(StandardCharsets.US_ASCII);
    msg.skipBytes(1); // COLON
    if (msg.isReadable()) {
        msg.skipBytes(1); // SPACE
    }

    final Matcher m = PROCESS_PATTERN.matcher(process);
    if (m.matches()) {
        return new String[] { m.group(1), m.group(2) };
    }

    return new String[] { process };
}

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

License:Open Source License

private String decodeHostname(final ByteBuf msg) {
    // split by first space
    final int spaceIndex = msg.bytesBefore(Constants.SP);
    if (spaceIndex < 0) {
        throw new CodecException("Unable to find hostname");
    }//from  w ww .  j  a v a  2  s  .c  o m

    final String hostname = msg.readSlice(spaceIndex).toString(StandardCharsets.US_ASCII);

    msg.skipBytes(1); // SPACE

    return hostname;
}

From source file:org.eclipse.scada.protocol.syslog.time.PatternTimestampParser.java

License:Open Source License

@Override
public Calendar parseTimestamp(final ByteBuf data) {
    final int index = data.bytesBefore(this.endMarker);
    if (index < 0) {
        throw new CodecException("Unable to find timestamp");
    }/*from  w w w.  jav a  2s.com*/

    final String timestampString = data.readSlice(index).toString(this.charset);

    logger.debug("Timestamp string: '{}'", timestampString);

    final Matcher m = this.pattern.matcher(timestampString);
    if (!m.matches()) {
        throw new CodecException("Timestamp string does not match pattern: " + this.pattern.pattern());
    }

    final int year = Integer.parseInt(m.group("year"));
    final int month = Integer.parseInt(m.group("month")) - 1;
    final int day = Integer.parseInt(m.group("day"));
    final int hour = Integer.parseInt(m.group("hour"));
    final int minute = Integer.parseInt(m.group("minute"));
    final int second = Integer.parseInt(m.group("second"));
    final int ms = Integer.parseInt(m.group("subsec")) / 1000;

    TimeZone timezone = TimeZone.getDefault();
    final String tz = m.group("tz");
    if (!tz.isEmpty()) {
        // FIXME: implement
        if ("Z".equals(tz)) {
            timezone = TimeZone.getTimeZone("UTC");
        } else {
            timezone = TimeZone.getTimeZone("GMT" + tz);
        }
    }

    final Calendar c = new GregorianCalendar(year, month, day, hour, minute, second);
    c.setTimeZone(timezone);
    c.set(Calendar.MILLISECOND, ms);

    // skip marker byte
    data.skipBytes(1);

    return c;
}