Java Code Examples for io.netty.buffer.ByteBuf.bytesBefore()

The following are Jave code examples for showing how to use bytesBefore() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: neoscada   File: SyslogCodec.java   Source Code and License Vote up 6 votes
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" );
    }

    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 };
}
 
Example 2
Project: neoscada   File: SyslogCodec.java   Source Code and License Vote up 5 votes
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" );
    }

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

    msg.skipBytes ( 1 ); // SPACE

    return hostname;
}
 
Example 3
Project: neoscada   File: PatternTimestampParser.java   Source Code and License Vote up 4 votes
@Override
public Calendar parseTimestamp ( final ByteBuf data )
{
    final int index = data.bytesBefore ( this.endMarker );
    if ( index < 0 )
    {
        throw new CodecException ( "Unable to find timestamp" );
    }

    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;
}
 
Example 4
Project: reactive-pg-client   File: Util.java   Source Code and License Vote up 4 votes
public static String readCString(ByteBuf src, Charset charset) {
  int len = src.bytesBefore(ZERO);
  String s = src.readCharSequence(len, charset).toString();
  src.readByte();
  return s;
}
 
Example 5
Project: reactive-pg-client   File: Util.java   Source Code and License Vote up 4 votes
public static String readCStringUTF8(ByteBuf src) {
  int len = src.bytesBefore(ZERO);
  String s = src.readCharSequence(len, UTF_8).toString();
  src.readByte();
  return s;
}