Example usage for io.netty.handler.codec CodecException CodecException

List of usage examples for io.netty.handler.codec CodecException CodecException

Introduction

In this page you can find the example usage for io.netty.handler.codec CodecException CodecException.

Prototype

public CodecException(Throwable cause) 

Source Link

Document

Creates a new instance.

Usage

From source file:com.flysoloing.learning.network.netty.redis.RedisClientHandler.java

License:Apache License

private static void printAggregatedRedisResponse(RedisMessage msg) {
    if (msg instanceof SimpleStringRedisMessage) {
        System.out.println(((SimpleStringRedisMessage) msg).content());
    } else if (msg instanceof ErrorRedisMessage) {
        System.out.println(((ErrorRedisMessage) msg).content());
    } else if (msg instanceof IntegerRedisMessage) {
        System.out.println(((IntegerRedisMessage) msg).value());
    } else if (msg instanceof FullBulkStringRedisMessage) {
        System.out.println(getString((FullBulkStringRedisMessage) msg));
    } else if (msg instanceof ArrayRedisMessage) {
        for (RedisMessage child : ((ArrayRedisMessage) msg).children()) {
            printAggregatedRedisResponse(child);
        }//from   ww w .j  av  a 2s .c o  m
    } else {
        throw new CodecException("unknown message type: " + msg);
    }
}

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

License:Open Source License

private void processTRAILER(final ChannelHandlerContext ctx, final byte b, final ByteBuf msg) {
    if (b != Constants.LF) {
        throw new CodecException(
                String.format("Expected trailer byte (LF) but found 0x%02X: Remaining buffer: %s", b,
                        ByteBufUtil.hexDump(msg, msg.readerIndex(), msg.readableBytes())));
    }/*from  w w  w  .  j  av a  2s  .c o m*/

    final int length = ctx.attr(ATTR_EXPECTED_LENGTH).get();
    final long txnr = Long.parseLong(ctx.attr(ATTR_TXNR_BUFFER).get().toString(TXNR_CHARSET));
    final String command = ctx.attr(ATTR_COMMAND_BUFFER).get().toString(COMMAND_CHARSET);
    final ByteBuf data = ctx.attr(ATTR_DATA_BUFFER).get().readSlice(length);

    final Frame frame = new Frame(txnr, command, data);

    ctx.fireChannelRead(frame);

    ctx.attr(ATTR_STATE).set(State.TXNR);
    ctx.attr(ATTR_TXNR_BUFFER).get().clear();
    ctx.attr(ATTR_COMMAND_BUFFER).get().clear();
    ctx.attr(ATTR_LENGTH_BUFFER).get().clear();
    ctx.attr(ATTR_DATA_BUFFER).get().clear();
}

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

License:Open Source License

private void processTXNR(final ChannelHandlerContext ctx, final byte b) {
    if (b == Constants.SP) {
        ctx.attr(ATTR_STATE).set(State.COMMAND);
    } else {//  w  w  w .j a  v  a 2 s. c o m
        if (b < 0x30 || b > 0x39) {
            throw new CodecException(String.format("Invalid character found: 0x%1$02x (%1$s)", b, (char) b));
        }
        ctx.attr(ATTR_TXNR_BUFFER).get().writeByte(b);
    }
}

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

License:Open Source License

protected void processMessage(final ChannelHandlerContext ctx, final ByteBuf msg) {
    if (msg.readByte() != PRI_START) {
        throw new CodecException("PRI start not found");
    }//  w  w w  . j  a v a  2s. co m

    final int prival = decodePrival(msg);

    final Severity severity = Severity.values()[prival % 8];
    final Facility facility = Facility.values()[prival / 8];

    final Calendar timestamp = this.timestampParser.parseTimestamp(msg);
    final String hostname = decodeHostname(msg);
    final String[] process = decodeProcess(msg);
    final String processName = process[0];
    final Long processId = process.length > 1 ? Long.parseLong(process[1]) : null;
    final String message = decodeMessage(msg);

    ctx.fireChannelRead(
            new SyslogMessage(facility, severity, timestamp, hostname, processName, processId, message));
}

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");
    }//ww w  . j a  va  2s.  c o  m

    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 av  a 2s. com*/

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

    msg.skipBytes(1); // SPACE

    return hostname;
}

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

License:Open Source License

private int decodePrival(final ByteBuf msg) {
    final ByteBuffer privalBuffer = ByteBuffer.wrap(new byte[3]);
    byte b;// ww w. ja  va 2s  .co m
    do {
        b = msg.readByte();
        if (b == PRI_END) {
            break;
        }
        if (!privalBuffer.hasRemaining()) {
            throw new CodecException("PRI value must be <=3 bytes");
        }
        privalBuffer.put(b);
    } while (true);

    privalBuffer.flip();
    final int prival = Integer.parseInt(StandardCharsets.US_ASCII.decode(privalBuffer).toString());
    return prival;
}

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");
    }//  w w  w . j a v a  2s. co  m

    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;
}

From source file:org.lanternpowered.server.network.buffer.LanternByteBuffer.java

License:MIT License

@Override
public LanternByteBuffer writeDataView(@Nullable DataView data) {
    if (data == null) {
        this.buf.writeByte(0);
        return this;
    }/*  www . j a  v  a2 s .c o m*/
    try {
        NbtStreamUtils.write(data, new ByteBufOutputStream(this.buf), false);
    } catch (IOException e) {
        throw new CodecException(e);
    }
    return this;
}

From source file:org.lanternpowered.server.network.buffer.LanternByteBuffer.java

License:MIT License

@Nullable
@Override/* w  w  w . j  a  va  2s. c  o m*/
public DataView readLimitedDataView(int maximumDepth, int maxBytes) {
    final int index = this.buf.readerIndex();
    if (this.buf.readByte() == 0) {
        return null;
    }
    this.buf.readerIndex(index);
    try {
        try (NbtDataContainerInputStream input = new NbtDataContainerInputStream(
                new LimitInputStream(new ByteBufInputStream(this.buf), maxBytes), false, maximumDepth)) {
            return input.read();
        }
    } catch (IOException e) {
        throw new CodecException(e);
    }
}