Example usage for io.netty.buffer ByteBuf readByte

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

Introduction

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

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:com.eightkdata.mongowp.bson.netty.DefaultNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonBoolean readBoolean(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    byte readByte = byteBuf.readByte();
    if (readByte == 0x00) {
        return FalseBsonBoolean.getInstance();
    }//from w  ww  . j  ava 2  s .co  m
    if (readByte == 0x01) {
        return TrueBsonBoolean.getInstance();
    }
    throw new NettyBsonReaderException("Unexpected boolean byte. 0x00 or " + "0x01 was expected, but 0x"
            + UnsignedBytes.toString(readByte, 16) + " was read");
}

From source file:com.eightkdata.mongowp.bson.netty.NettyBsonLowLevelReader.java

License:Open Source License

public BsonValue<?> readArrayEntry(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    BsonType bsonType = ParsingTools.getBsonType(byteBuf.readByte());
    stringReader.skipCString(byteBuf);/*  w w  w .j  a va2  s.c o m*/
    switch (bsonType) {
    case ARRAY:
        return readArray(byteBuf);
    case BINARY:
        return readBinary(byteBuf);
    case DATETIME:
        return readDateTime(byteBuf);
    case DB_POINTER:
        return readDbPointer(byteBuf);
    case DEPRECATED:
        return readDeprecated(byteBuf);
    case DOCUMENT:
        return readDocument(byteBuf);
    case DOUBLE:
        return readDouble(byteBuf);
    case BOOLEAN:
        return readBoolean(byteBuf);
    case INT32:
        return readInt32(byteBuf);
    case INT64:
        return readInt64(byteBuf);
    case JAVA_SCRIPT:
        return readJavaScript(byteBuf);
    case JAVA_SCRIPT_WITH_SCOPE:
        return readJavaScriptWithScope(byteBuf);
    case MAX:
        return readMax(byteBuf);
    case MIN:
        return readMin(byteBuf);
    case NULL:
        return readNull(byteBuf);
    case OBJECT_ID:
        return readObjectId(byteBuf);
    case REGEX:
        return readRegex(byteBuf);
    case STRING:
        return readString(byteBuf);
    case TIMESTAMP:
        return readTimestamp(byteBuf);
    case UNDEFINED:
        return readUndefined(byteBuf);
    default:
        throw new NettyBsonReaderException("Unexpected bson type " + bsonType);
    }
}

From source file:com.eightkdata.mongowp.bson.netty.NettyBsonLowLevelReader.java

License:Open Source License

public Entry<?> readDocumentEntry(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    BsonType bsonType = ParsingTools.getBsonType(byteBuf.readByte());
    String key = stringReader.readCString(byteBuf, true);
    switch (bsonType) {
    case ARRAY:// www.j av a2 s .c om
        return new SimpleEntry<>(key, readArray(byteBuf));
    case BINARY:
        return new SimpleEntry<>(key, readBinary(byteBuf));
    case DATETIME:
        return new SimpleEntry<>(key, readDateTime(byteBuf));
    case DB_POINTER:
        return new SimpleEntry<>(key, readDbPointer(byteBuf));
    case DEPRECATED:
        return new SimpleEntry<>(key, readDeprecated(byteBuf));
    case DOCUMENT:
        return new SimpleEntry<>(key, readDocument(byteBuf));
    case DOUBLE:
        return new SimpleEntry<>(key, readDouble(byteBuf));
    case BOOLEAN:
        return new SimpleEntry<>(key, readBoolean(byteBuf));
    case INT32:
        return new SimpleEntry<>(key, readInt32(byteBuf));
    case INT64:
        return new SimpleEntry<>(key, readInt64(byteBuf));
    case JAVA_SCRIPT:
        return new SimpleEntry<>(key, readJavaScript(byteBuf));
    case JAVA_SCRIPT_WITH_SCOPE:
        return new SimpleEntry<>(key, readJavaScriptWithScope(byteBuf));
    case MAX:
        return new SimpleEntry<>(key, readMax(byteBuf));
    case MIN:
        return new SimpleEntry<>(key, readMin(byteBuf));
    case NULL:
        return new SimpleEntry<>(key, readNull(byteBuf));
    case OBJECT_ID:
        return new SimpleEntry<>(key, readObjectId(byteBuf));
    case REGEX:
        return new SimpleEntry<>(key, readRegex(byteBuf));
    case STRING:
        return new SimpleEntry<>(key, readString(byteBuf));
    case TIMESTAMP:
        return new SimpleEntry<>(key, readTimestamp(byteBuf));
    case UNDEFINED:
        return new SimpleEntry<>(key, readUndefined(byteBuf));
    default:
        throw new NettyBsonReaderException("Unexpected bson type " + bsonType);
    }
}

From source file:com.eightkdata.mongowp.bson.netty.OffHeapValuesNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonDocument readDocument(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4 - 1;

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    byte b = byteBuf.readByte();
    assert b == 0x00;

    ArrayList<BsonDocument.Entry<?>> list = new ArrayList<>();
    while (significantSlice.readableBytes() > 0) {
        Entry<?> entry = readDocumentEntry(significantSlice);
        list.add(entry);//w  w w. j ava2s.c o  m
    }
    return new ListBasedBsonDocument(list);
}

From source file:com.eightkdata.mongowp.bson.netty.OffHeapValuesNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonBinary readBinary(@Loose @ModifiesIndexes ByteBuf byteBuf) {
    int length = byteBuf.readInt();
    byte subtype = byteBuf.readByte();

    ByteBuf content = byteBuf.readSlice(length);

    return new NettyBsonBsonBinary(subtype, ParsingTools.getBinarySubtype(subtype), content);
}

From source file:com.eightkdata.mongowp.bson.netty.PooledNettyStringReader.java

License:Open Source License

/**
 * A method that reads a C-string from a ByteBuf. This method modified the internal state of the
 * ByteBuf, advancing the read pointer to the position after the cstring.
 *
 * @param buffer//from   ww  w.j a v  a  2  s .c o m
 * @param likelyCacheable
 * @return The C-String as a String object or null if there was no C-String in the ByteBuf
 * @throws com.eightkdata.mongowp.bson.netty.NettyBsonReaderException
 */
@Override
public String readCString(ByteBuf buffer, boolean likelyCacheable) throws NettyBsonReaderException {
    int pos = buffer.bytesBefore(CSTRING_BYTE_TERMINATION);
    if (pos == -1) {
        throw new NettyBsonReaderException("A cstring was expected but no 0x00 byte was found");
    }

    String result = stringPool.fromPool(likelyCacheable, buffer.readSlice(pos));

    buffer.readByte(); // Discard the termination byte

    return result;
}

From source file:com.eightkdata.mongowp.bson.netty.PooledNettyStringReader.java

License:Open Source License

@Override
public ByteBuf readStringAsSlice(@Loose @ModifiesIndexes ByteBuf byteBuf) {
    int stringLength = byteBuf.readInt();
    ByteBuf result = byteBuf.readSlice(stringLength - 1);
    byte b = byteBuf.readByte(); // discard the last 0x00
    assert b == 0x00;

    return result;
}

From source file:com.eightkdata.mongowp.mongoserver.util.ByteBufUtil.java

License:Open Source License

/**
 * A method that reads a C-string from a ByteBuf.
 * This method modified the internal state of the ByteBuf, advancing the read pointer to the position after the
 * cstring./*from  w  w w  .  j  a v a 2s. c  om*/
 *
 * @param buffer
 * @return The C-String as a String object or null if there was no C-String in the ByteBuf
 */
public static String readCString(ByteBuf buffer) {
    int pos = buffer.bytesBefore(CSTRING_BYTE_TERMINATION);
    if (pos == -1) {
        return null;
    }
    byte[] bytes = new byte[pos];
    buffer.readBytes(bytes);
    buffer.readByte(); // Discard the termination byte

    return new String(bytes, Charsets.UTF_8);
}

From source file:com.example.discard.DiscardServerHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    log.info("message coming.");
    final ByteBuf byteBuf = (ByteBuf) msg;
    final EmitterProcessor<Byte> emitter = EmitterProcessor.create();

    // ??? ByteBuf.toString(StandardCharset.UTF_8) ????
    final Mono<String> messageMono = emitter.collectList().map(DiscardServerHandler::unBoxing)
            .map(bs -> new String(bs, StandardCharsets.UTF_8));
    messageMono.subscribe(message -> log.info("received message: {}", message));
    messageMono.doOnTerminate(() -> ReferenceCountUtil.release(byteBuf));

    while (byteBuf.isReadable()) {
        emitter.onNext(byteBuf.readByte());
    }//www  .  ja  va2  s . co  m
    emitter.onComplete();
}

From source file:com.farsunset.cim.sdk.android.filter.ClientMessageDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception {

    /**//ww  w . jav a2  s  .  c  o  m
     * ?3?
     */
    if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) {
        return;
    }

    buffer.markReaderIndex();

    buffer.markReaderIndex();

    byte conetnType = buffer.readByte();

    byte lv = buffer.readByte();// int ?
    byte hv = buffer.readByte();// int ?

    int conetnLength = getContentLength(lv, hv);

    // ?????
    if (conetnLength > buffer.readableBytes()) {
        buffer.resetReaderIndex();
        return;
    }

    byte[] dataBytes = new byte[conetnLength];
    buffer.readBytes(dataBytes);

    Object message = mappingMessageObject(dataBytes, conetnType);

    if (message != null) {
        queue.add(message);
    }

}