Example usage for io.netty.buffer ByteBuf skipBytes

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

Introduction

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

Prototype

public abstract ByteBuf skipBytes(int length);

Source Link

Document

Increases the current readerIndex by the specified length in this buffer.

Usage

From source file:org.eclipse.milo.opcua.stack.server.transport.uasc.UascServerSymmetricHandler.java

License:Open Source License

private void onSecureMessage(ChannelHandlerContext ctx, ByteBuf buffer) throws UaException {
    buffer.skipBytes(3); // Skip messageType

    char chunkType = (char) buffer.readByte();

    if (chunkType == 'A') {
        chunkBuffers.forEach(ByteBuf::release);
        chunkBuffers.clear();/*  w  w  w .  j a  v  a 2s .co m*/
    } else {
        buffer.skipBytes(4); // Skip messageSize

        long secureChannelId = buffer.readUnsignedIntLE();
        if (secureChannelId != secureChannel.getChannelId()) {
            throw new UaException(StatusCodes.Bad_SecureChannelIdInvalid,
                    "invalid secure channel id: " + secureChannelId);
        }

        int chunkSize = buffer.readerIndex(0).readableBytes();
        if (chunkSize > maxChunkSize) {
            throw new UaException(StatusCodes.Bad_TcpMessageTooLarge,
                    String.format("max chunk size exceeded (%s)", maxChunkSize));
        }

        chunkBuffers.add(buffer.retain());

        if (maxChunkCount > 0 && chunkBuffers.size() > maxChunkCount) {
            throw new UaException(StatusCodes.Bad_TcpMessageTooLarge,
                    String.format("max chunk count exceeded (%s)", maxChunkCount));
        }

        if (chunkType == 'F') {
            final List<ByteBuf> buffersToDecode = chunkBuffers;
            chunkBuffers = new ArrayList<>();

            serializationQueue.decode((binaryDecoder, chunkDecoder) -> {
                try {
                    validateChunkHeaders(buffersToDecode);
                } catch (UaException e) {
                    logger.error("Error validating chunk headers: {}", e.getMessage(), e);
                    buffersToDecode.forEach(ReferenceCountUtil::safeRelease);
                    ctx.fireExceptionCaught(e);
                    return;
                }

                chunkDecoder.decodeSymmetric(secureChannel, buffersToDecode, new ChunkDecoder.Callback() {
                    @Override
                    public void onDecodingError(UaException ex) {
                        logger.error("Error decoding symmetric message: {}", ex.getMessage(), ex);

                        ctx.close();
                    }

                    @Override
                    public void onMessageAborted(MessageAbortedException ex) {
                        logger.warn("Received message abort chunk; error={}, reason={}", ex.getStatusCode(),
                                ex.getMessage());
                    }

                    @Override
                    public void onMessageDecoded(ByteBuf message, long requestId) {
                        UaRequestMessage request = (UaRequestMessage) binaryDecoder.setBuffer(message)
                                .readMessage(null);

                        stackServer.getConfig().getExecutor().execute(() -> {
                            try {
                                String endpointUrl = ctx.channel().attr(UascServerHelloHandler.ENDPOINT_URL_KEY)
                                        .get();

                                EndpointDescription endpoint = ctx.channel()
                                        .attr(UascServerAsymmetricHandler.ENDPOINT_KEY).get();

                                String path = EndpointUtil.getPath(endpointUrl);

                                InetSocketAddress remoteSocketAddress = (InetSocketAddress) ctx.channel()
                                        .remoteAddress();

                                ServiceRequest serviceRequest = new ServiceRequest(stackServer, request,
                                        endpoint, secureChannel.getChannelId(),
                                        remoteSocketAddress.getAddress(),
                                        secureChannel.getRemoteCertificateBytes());

                                serviceRequest.getFuture().whenComplete((response, fault) -> {
                                    if (response != null) {
                                        sendServiceResponse(ctx, requestId, request, response);
                                    } else {
                                        UInteger requestHandle = request.getRequestHeader().getRequestHandle();

                                        sendServiceFault(ctx, requestId, requestHandle, fault);
                                    }
                                });

                                stackServer.onServiceRequest(path, serviceRequest);
                            } catch (Throwable t) {
                                logger.error("Error decoding UaRequestMessage", t);

                                sendServiceFault(ctx, requestId, uint(0), t);
                            } finally {
                                message.release();
                                buffersToDecode.clear();
                            }
                        });
                    }
                });
            });
        }
    }
}

From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUDecoder.java

License:Open Source License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {
    if (logger.isTraceEnabled()) {
        logger.trace("decode - bytes: {}", ByteBufUtil.hexDump(in));
    }/*from www.j a va 2 s.  co m*/

    if (in.readableBytes() < Constants.APCI_MIN_LENGTH) {
        return;
    }

    final byte start = in.getByte(in.readerIndex() + 0);
    if (start != Constants.START_BYTE) {
        throw new DecoderException(
                String.format("ACPI must start with 0x%02x but did with 0x%02x", Constants.START_BYTE, start));
    }

    final short len = in.getUnsignedByte(in.readerIndex() + 1);

    if (len > Constants.APCI_MAX_DATA_LENGTH) {
        throw new DecoderException(String.format("APCI has a maximum length of %s bytes, but we received %s",
                Constants.APCI_MAX_DATA_LENGTH, len));
    }

    if (in.readableBytes() < len + 2) {
        return;
    }

    // we have the full InformationTransfer

    // skip start & len
    in.skipBytes(2);

    // read control fields
    final ByteBuf controlFields = in.readSlice(4);

    final ByteBuf data;
    if (len > 4) {
        data = Unpooled.copiedBuffer(in.readSlice(len - 4)).order(ByteOrder.LITTLE_ENDIAN);
    } else {
        data = null;
    }

    // now add the PDU

    out.add(decode(controlFields, data));
}

From source file:org.eclipse.neoscada.protocol.iec60870.apci.APDUDecoder.java

License:Open Source License

private APCIBase decode(ByteBuf controlFields, final ByteBuf data) {
    logger.trace("Control Fields: {}", ByteBufUtil.hexDump(controlFields));

    controlFields = controlFields.order(ByteOrder.LITTLE_ENDIAN);

    final byte first = controlFields.getByte(0);
    if ((first & 0x01) == 0) {
        // I format
        final int sendSequenceNumber = controlFields.readUnsignedShort() >> 1;
        final int receiveSequenceNumber = controlFields.readUnsignedShort() >> 1;
        logger.debug("S: {}, R: {}", sendSequenceNumber, receiveSequenceNumber);
        return new InformationTransfer(sendSequenceNumber, receiveSequenceNumber, data);
    } else if ((first & 0x03) == 1) {
        // S format
        controlFields.skipBytes(2);
        final int receiveSequenceNumber = controlFields.readUnsignedShort() >> 1;
        return new Supervisory(receiveSequenceNumber);
    } else if ((first & 0x03) == 3) {
        // U format
        final Function function = convertFunction(controlFields.readUnsignedByte());
        return new UnnumberedControl(function);
    }/*from  w  ww  . ja v a2  s. c om*/

    // this should actually never happen

    throw new DecoderException("Invalid control fields");
}

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");
    }//from   www  .j a  va2 s.co 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");
    }//w ww. j  ava2s .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");
    }/*ww  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.elasticsearch.hadoop.transport.netty4.Netty4SizeHeaderFrameDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    try {/*from  w  w w .  j  av a 2  s .  c o  m*/
        boolean continueProcessing = TcpTransport.validateMessageHeader(Netty4Utils.toBytesReference(in));
        final ByteBuf message = in.skipBytes(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE);
        if (!continueProcessing)
            return;
        out.add(message);
    } catch (IllegalArgumentException ex) {
        throw new TooLongFrameException(ex);
    } catch (IllegalStateException ex) {
        /* decode will be called until the ByteBuf is fully consumed; when it is fully
         * consumed, transport#validateMessageHeader will throw an IllegalStateException which
         * is okay, it means we have finished consuming the ByteBuf and we can get out
         */
    }
}

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// w w w  .j  a  v a  2  s  .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.SyslogOctetCountFrameDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    final int frameSizeValueLength = findFrameSizeValueLength(buffer);

    // We have not found the frame length value byte size yet.
    if (frameSizeValueLength <= 0) {
        return;/*from  ww w  .  j  av a2s.c  om*/
    }

    // Convert the frame length value bytes into an integer without mutating the buffer reader index.
    final String lengthString = buffer.slice(buffer.readerIndex(), frameSizeValueLength)
            .toString(StandardCharsets.UTF_8);
    final int length = Integer.parseInt(lengthString);
    final int skipLength = frameSizeValueLength + 1; // Frame length value bytes and the whitespace that follows it.

    // We have to take the skipped bytes (frame size value length + whitespace) into account when checking if
    // the buffer has enough data to read the complete message.
    if (buffer.readableBytes() - skipLength < length) {
        // We cannot read the complete frame yet.
        return;
    } else {
        // Skip the frame length value bytes and the whitespace that follows it.
        buffer.skipBytes(skipLength);
    }

    final ByteBuf frame = buffer.readRetainedSlice(length);
    out.add(frame);
}

From source file:org.graylog2.inputs.transports.netty.LenientDelimiterBasedFrameDecoder.java

License:Open Source License

/**
 * Create a frame out of the {@link ByteBuf} and return it.
 *
 * @param ctx    the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} 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.//  www  .ja va  2s .  co m
 */
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    if (lineBasedDecoder != null) {
        return lineBasedDecoder.decode(ctx, buffer);
    }
    // Try all delimiters and choose the delimiter which yields the shortest frame.
    int minFrameLength = Integer.MAX_VALUE;
    ByteBuf minDelim = null;
    for (ByteBuf delim : delimiters) {
        int frameLength = indexOf(buffer, delim);
        if (frameLength >= 0 && frameLength < minFrameLength) {
            minFrameLength = frameLength;
            minDelim = delim;
        }
    }

    if (minDelim != null) {
        int minDelimLength = minDelim.capacity();
        ByteBuf frame;

        if (discardingTooLongFrame) {
            // We've just finished discarding a very large frame.
            // Go back to the initial state.
            discardingTooLongFrame = false;
            buffer.skipBytes(minFrameLength + minDelimLength);

            int tooLongFrameLength = this.tooLongFrameLength;
            this.tooLongFrameLength = 0;
            if (!failFast) {
                fail(tooLongFrameLength);
            }
            return null;
        }

        if (minFrameLength > maxFrameLength) {
            // Discard read frame.
            buffer.skipBytes(minFrameLength + minDelimLength);
            fail(minFrameLength);
            return null;
        }

        if (stripDelimiter) {
            frame = buffer.readRetainedSlice(minFrameLength);
            buffer.skipBytes(minDelimLength);
        } else {
            frame = buffer.readRetainedSlice(minFrameLength + minDelimLength);
        }

        return frame;
    } else if (emitLastLineWithoutDelimiter && !ctx.channel().isActive()) {
        minFrameLength = buffer.readableBytes();
        ByteBuf frame;

        if (discardingTooLongFrame) {
            // We've just finished discarding a very large frame.
            // Go back to the initial state.
            discardingTooLongFrame = false;
            buffer.skipBytes(minFrameLength);

            int tooLongFrameLength = this.tooLongFrameLength;
            this.tooLongFrameLength = 0;
            if (!failFast) {
                fail(tooLongFrameLength);
            }
            return null;
        }

        if (minFrameLength > maxFrameLength) {
            // Discard read frame.
            buffer.skipBytes(minFrameLength);
            fail(minFrameLength);
            return null;
        }

        frame = buffer.readRetainedSlice(minFrameLength);

        return frame;
    } else {
        if (!discardingTooLongFrame) {
            if (buffer.readableBytes() > maxFrameLength) {
                // Discard the content of the buffer until a delimiter is found.
                tooLongFrameLength = buffer.readableBytes();
                buffer.skipBytes(buffer.readableBytes());
                discardingTooLongFrame = true;
                if (failFast) {
                    fail(tooLongFrameLength);
                }
            }
        } else {
            // Still discarding the buffer since a delimiter is not found.
            tooLongFrameLength += buffer.readableBytes();
            buffer.skipBytes(buffer.readableBytes());
        }
        return null;
    }
}