Example usage for io.netty.buffer ByteBuf writerIndex

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

Introduction

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

Prototype

public abstract int writerIndex();

Source Link

Document

Returns the writerIndex of this buffer.

Usage

From source file:com.eightkdata.mongowp.server.decoder.InsertMessageDecoder.java

License:Open Source License

@Override
@SuppressFBWarnings(value = {/* w ww .  j a  v  a2  s . c o m*/
        "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT" }, justification = "Findbugs thinks ByteBuf#readerIndex(...) has no"
                + "side effect")
public InsertMessage decode(ByteBuf buffer, RequestBaseMessage requestBaseMessage)
        throws InvalidNamespaceException, InvalidBsonException {
    try {
        MyBsonContext context = new MyBsonContext(buffer);

        int flags = buffer.readInt();
        String fullCollectionName = stringReader.readCString(buffer, true);

        ByteBuf docBuf = buffer.slice(buffer.readerIndex(), buffer.readableBytes());
        docBuf.retain();

        buffer.readerIndex(buffer.writerIndex());

        ByteBufIterableDocumentProvider documents = new ByteBufIterableDocumentProvider(docBuf, docReader);

        //TODO: improve the way database and cache are pooled
        return new InsertMessage(requestBaseMessage, context, getDatabase(fullCollectionName).intern(),
                getCollection(fullCollectionName).intern(),
                EnumInt32FlagsUtil.isActive(Flag.CONTINUE_ON_ERROR, flags), documents);
    } catch (NettyBsonReaderException ex) {
        throw new InvalidBsonException(ex);
    }
}

From source file:com.flowpowered.examples.networking.TestProtocol.java

License:MIT License

@Override
public ByteBuf writeHeader(ByteBuf header, CodecRegistration codec, ByteBuf data) {
    header.writeShort(codec.getOpcode());
    header.writeInt(data.writerIndex());
    return header;
}

From source file:com.flowpowered.examples.networking.TestSecondProtocol.java

License:MIT License

@Override
public ByteBuf writeHeader(ByteBuf header, Codec.CodecRegistration codec, ByteBuf data) {
    header.writeShort(codec.getOpcode());
    header.writeInt(data.writerIndex());
    return header;
}

From source file:com.github.nettybook.ch0.LoggingHandler.java

License:Apache License

/**
 * Appends the prettifies multi-line hexadecimal dump of the specified {@link ByteBuf} to the specified
 * {@link StringBuilder}.//from w  w  w .  java  2s .c  o m
 */
protected static void appendHexDump(StringBuilder dump, ByteBuf buf) {
    dump.append(NEWLINE + "         +-------------------------------------------------+" + NEWLINE
            + "         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |" + NEWLINE
            + "+--------+-------------------------------------------------+----------------+");

    final int startIndex = buf.readerIndex();
    final int endIndex = buf.writerIndex();
    final int length = endIndex - startIndex;
    final int fullRows = length >>> 4;
    final int remainder = length & 0xF;

    // Dump the rows which have 16 bytes.
    for (int row = 0; row < fullRows; row++) {
        int rowStartIndex = row << 4;

        // Per-row prefix.
        appendHexDumpRowPrefix(dump, row, rowStartIndex);

        // Hex dump
        int rowEndIndex = rowStartIndex + 16;
        for (int j = rowStartIndex; j < rowEndIndex; j++) {
            dump.append(BYTE2HEX[buf.getUnsignedByte(j)]);
        }
        dump.append(" |");

        // ASCII dump
        for (int j = rowStartIndex; j < rowEndIndex; j++) {
            dump.append(BYTE2CHAR[buf.getUnsignedByte(j)]);
        }
        dump.append('|');
    }

    // Dump the last row which has less than 16 bytes.
    if (remainder != 0) {
        int rowStartIndex = fullRows << 4;
        appendHexDumpRowPrefix(dump, fullRows, rowStartIndex);

        // Hex dump
        int rowEndIndex = rowStartIndex + remainder;
        for (int j = rowStartIndex; j < rowEndIndex; j++) {
            dump.append(BYTE2HEX[buf.getUnsignedByte(j)]);
        }
        dump.append(HEXPADDING[remainder]);
        dump.append(" |");

        // Ascii dump
        for (int j = rowStartIndex; j < rowEndIndex; j++) {
            dump.append(BYTE2CHAR[buf.getUnsignedByte(j)]);
        }
        dump.append(BYTEPADDING[remainder]);
        dump.append('|');
    }

    dump.append(NEWLINE + "+--------+-------------------------------------------------+----------------+");
}

From source file:com.growcontrol.common.netty.JsonObjectDecoder.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {
    if (this.state == ST_CORRUPTED) {
        in.skipBytes(in.readableBytes());
        return;// ww  w  .  j  av a  2 s .  c  o  m
    }
    // index of next byte to process.
    int idx = this.idx;
    final int wrtIdx = in.writerIndex();
    if (wrtIdx > this.maxObjectLength) {
        // buffer size exceeded maxObjectLength; discarding the complete buffer.
        in.skipBytes(in.readableBytes());
        reset();
        throw new TooLongFrameException(
                "object length exceeds " + this.maxObjectLength + ": " + wrtIdx + " bytes discarded");
    }
    for (/* use current idx */; idx < wrtIdx; idx++) {
        final byte c = in.getByte(idx);
        if (this.state == ST_DECODING_NORMAL) {
            decodeByte(c, in, idx);
            // All opening braces/brackets have been closed. That's enough to conclude
            // that the JSON object/array is complete.
            if (this.openBraces == 0) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                // The JSON object/array was extracted => discard the bytes from
                // the input buffer.
                in.readerIndex(idx + 1);
                // Reset the object state to get ready for the next JSON object/text
                // coming along the byte stream.
                reset();
            }
        } else if (this.state == ST_DECODING_ARRAY_STREAM) {
            decodeByte(c, in, idx);
            if (!this.insideString && (this.openBraces == 1 && c == ',' || this.openBraces == 0 && c == ']')) {
                // skip leading spaces. No range check is needed and the loop will terminate
                // because the byte at position idx is not a whitespace.
                for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) {
                    in.skipBytes(1);
                }
                // skip trailing spaces.
                int idxNoSpaces = idx - 1;
                while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) {
                    idxNoSpaces--;
                }
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                in.readerIndex(idx + 1);
                if (c == ']') {
                    reset();
                }
            }
            // JSON object/array detected. Accumulate bytes until all braces/brackets are closed.
        } else if (c == '{' || c == '[') {
            initDecoding(c);
            // Discard the array bracket
            if (this.state == ST_DECODING_ARRAY_STREAM)
                in.skipBytes(1);
            // Discard leading spaces in front of a JSON object/array.
        } else if (Character.isWhitespace(c)) {
            in.skipBytes(1);
        } else {
            this.state = ST_CORRUPTED;
            throw new CorruptedFrameException(
                    "invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in));
        }
    }
    if (in.readableBytes() == 0)
        this.idx = 0;
    else
        this.idx = idx;
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}/*from  w w w  . j  av a 2s  . co  m*/
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(boolean, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final Boolean p, final ByteBuf b) throws SQLException {
    prefix(p, DBType.BOOLEAN, b);
    b.writeBoolean(p);
    return b.writerIndex();
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}/*w ww .j av a 2s  .  co m*/
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(boolean, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final boolean p, final ByteBuf b) throws SQLException {
    prefix(P, DBType.BOOLEAN, b);
    b.writeBoolean(p);
    return b.writerIndex();
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}/*  w  ww.j  a va2s  . co m*/
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(byte, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final Byte p, final ByteBuf b) throws SQLException {
    prefix(p, DBType.TINYINT, b);
    b.writeByte(p);
    return b.writerIndex();
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}// ww  w . ja v  a  2  s  . c om
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(byte, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final byte p, final ByteBuf b) throws SQLException {
    prefix(P, DBType.TINYINT, b);
    b.writeByte(p);
    return b.writerIndex();
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}/*  w  w  w  .  ja  v  a2s  . c  o  m*/
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(short, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final Short p, final ByteBuf b) throws SQLException {
    prefix(p, DBType.SMALLINT, b);
    b.writeShort(p);
    return b.writerIndex();
}