List of usage examples for io.netty.buffer ByteBuf readByte
public abstract byte readByte();
From source file:herddb.utils.ByteBufUtils.java
License:Apache License
private static final long readVLong(ByteBuf buffer, boolean allowNegative) { byte b = buffer.readByte(); if (b >= 0) { return b; }/*from w w w . j a v a 2s . com*/ long i = b & 0x7FL; b = buffer.readByte(); i |= (b & 0x7FL) << 7; if (b >= 0) { return i; } b = buffer.readByte(); i |= (b & 0x7FL) << 14; if (b >= 0) { return i; } b = buffer.readByte(); i |= (b & 0x7FL) << 21; if (b >= 0) { return i; } b = buffer.readByte(); i |= (b & 0x7FL) << 28; if (b >= 0) { return i; } b = buffer.readByte(); i |= (b & 0x7FL) << 35; if (b >= 0) { return i; } b = buffer.readByte(); i |= (b & 0x7FL) << 42; if (b >= 0) { return i; } b = buffer.readByte(); i |= (b & 0x7FL) << 49; if (b >= 0) { return i; } b = buffer.readByte(); i |= (b & 0x7FL) << 56; if (b >= 0) { return i; } if (allowNegative) { b = buffer.readByte(); i |= (b & 0x7FL) << 63; if (b == 0 || b == 1) { return i; } throw new IllegalArgumentException("Invalid vLong detected (more than 64 bits)"); } else { throw new IllegalArgumentException("Invalid vLong detected (negative values disallowed)"); } }
From source file:hivemall.mix.MixMessageDecoder.java
License:Open Source License
@Override protected MixMessage decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; }/*from www. j a v a 2 s . c o m*/ byte b = frame.readByte(); MixEventName event = MixEventName.resolve(b); Object feature = decodeObject(frame); float weight = frame.readFloat(); float covariance = frame.readFloat(); short clock = frame.readShort(); int deltaUpdates = frame.readInt(); boolean cancelRequest = frame.readBoolean(); String groupID = readString(frame); MixMessage msg = new MixMessage(event, feature, weight, covariance, clock, deltaUpdates, cancelRequest); msg.setGroupID(groupID); return msg; }
From source file:hivemall.mix.MixMessageDecoder.java
License:Open Source License
private static Object decodeObject(final ByteBuf in) throws IOException { final byte type = in.readByte(); switch (type) { case INTEGER_TYPE: { int i = in.readInt(); return Integer.valueOf(i); }//from w w w . j a v a 2 s.com case TEXT_TYPE: { int length = in.readInt(); byte[] b = new byte[length]; in.readBytes(b, 0, length); Text t = new Text(b); return t; } case STRING_TYPE: { return readString(in); } case INT_WRITABLE_TYPE: { int i = in.readInt(); return new IntWritable(i); } case LONG_WRITABLE_TYPE: { long l = in.readLong(); return new LongWritable(l); } default: break; } throw new IllegalStateException("Illegal type: " + type); }
From source file:impl.underdark.transport.bluetooth.discovery.ble.ManufacturerData.java
License:Open Source License
public static ManufacturerData parse(byte[] data) { if (data == null || data.length < (1 + 6 + 4)) return null; ByteBuf buf = Unpooled.wrappedBuffer(data); byte version = buf.readByte(); if (version != 1) return null; int appId = buf.readInt(); byte[] address = new byte[deviceAddressLen]; buf.readBytes(address);//from w ww . j av a 2 s . c o m int channelsMask = buf.readInt(); List<Integer> channels = new ArrayList<>(); for (int channel = 0; channelsMask != 0; ++channel) { if (channelsMask % 2 != 0) channels.add(channel); channelsMask >>= 1; } return new ManufacturerData(appId, address, channels); }
From source file:info.zhoumin.dat.AbstractDoubleArrayTrie.java
License:Apache License
private boolean branchInTail(int sepNode, int c, ByteBuf suffix, V data) { /* adjust separate point in old path */ int oldTail = getTailIndex(sepNode); ByteBuf oldSuffix = tail.getSuffix(oldTail); if (oldSuffix == null) return false; byte oldByte, newByte; int s = sepNode; suffix.resetReaderIndex();/* w ww .j a v a2s . com*/ oldSuffix.resetReaderIndex(); do { newByte = suffix.readByte(); oldByte = oldSuffix.readByte(); if (newByte != oldByte) break; int t = insertBranch(s, newByte + 1); if (TRIE_INDEX_ERROR == t) { // /* failed, undo previous insertions and return error */ // da_prune_upto (trie->da, sep_node, s); // trie_da_set_tail_index (trie->da, sep_node, old_tail); // throw new RuntimeException("error happened!"); return false; } s = t; } while (suffix.isReadable() && oldSuffix.isReadable()); int oldDA = insertBranch(s, oldByte + 1); if (TRIE_INDEX_ERROR == oldDA) { // /* failed, undo previous insertions and return error */ // da_prune_upto (trie->da, sep_node, s); // trie_da_set_tail_index (trie->da, sep_node, old_tail); // throw new RuntimeException("error happened!"); return false; } tail.setSuffix(oldTail, oldSuffix.discardReadBytes().copy()); setTailIndex(oldDA, oldTail); /* insert the new branch at the new separate point */ return branchInBranch(s, newByte + 1, suffix.discardReadBytes().copy(), data); }
From source file:info.zhoumin.dat.AbstractDoubleArrayTrie.java
License:Apache License
@Override public V put(K key, V value) { if (key == null) { throw new NullPointerException("Key cannot be null"); }//from w w w . j a v a 2 s .c om int s = DOUBLE_ARRAY_ROOT; keyAnalyzer.setValue(key); while (keyAnalyzer.hasNext()) { if (daIsSeperate(s)) break; int c = (int) keyAnalyzer.next() + 1; int next = getNext(s, c); if (next < 0) { branchInBranch(s, c, keyAnalyzer.rest(), value); return value; } s = next; } /* walk through tail */ ByteBuf suffix = tail.getSuffix(getTailIndex(s)); suffix.resetReaderIndex(); ByteBuf tailBytes = keyAnalyzer.rest(); int c = (keyAnalyzer.hasNext() ? keyAnalyzer.next() : 0) + 1; while (suffix.isReadable() && tailBytes.isReadable()) { byte b = suffix.readByte(); if (b != tailBytes.readByte()) { branchInTail(s, c, tailBytes, value); return value; } } // , ? this.isDirty = true; return null; }
From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java
License:Apache License
/** * Decodes the ByteBuf into a HeaderFrame transferring the reference ownership. * @param buffer buffer to be decoded; reference count ownership is transferred to this method * @return the decoded frame; caller is responsible for releasing this object *//*from w w w.ja v a 2s. c o m*/ public static ThriftFrame decodeFrame(ByteBuf buffer) { ByteBuf messageHeader = null; try { // frame header short magic = buffer.readShort(); verify(magic == HEADER_MAGIC, "Invalid header magic"); short flags = buffer.readShort(); boolean outOfOrderResponse; switch (flags) { case FLAGS_NONE: outOfOrderResponse = false; break; case FLAG_SUPPORT_OUT_OF_ORDER: outOfOrderResponse = true; break; default: throw new IllegalArgumentException("Unsupported header flags: " + flags); } int frameSequenceId = buffer.readInt(); int headerSize = buffer.readShort() << 2; messageHeader = buffer.readBytes(headerSize); // encoding info byte protocolId = messageHeader.readByte(); Protocol protocol = Protocol.getProtocolByHeaderTransportId(protocolId); byte numberOfTransforms = messageHeader.readByte(); if (numberOfTransforms > 0) { // currently there are only two transforms, a cryptographic extension which is deprecated, and gzip which is too expensive throw new IllegalArgumentException("Unsupported transform"); } // headers // todo what about duplicate headers? ImmutableMap.Builder<String, String> allHeaders = ImmutableMap.builder(); allHeaders.putAll(decodeHeaders(NORMAL_HEADERS, messageHeader)); allHeaders.putAll(decodeHeaders(PERSISTENT_HEADERS, messageHeader)); // message ByteBuf message = buffer.readBytes(buffer.readableBytes()); // header frame wraps message byte buffer, so message should not be release yet return new ThriftFrame(frameSequenceId, message, allHeaders.build(), HEADER, protocol, outOfOrderResponse); } finally { // message header in an independent buffer and must be released if (messageHeader != null) { messageHeader.release(); } // input buffer has been consumed and transformed into a HeaderFrame, so release it buffer.release(); } }
From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java
License:Apache License
private static Map<String, String> decodeHeaders(int expectedHeadersType, ByteBuf messageHeader) { if (messageHeader.readableBytes() == 0) { return ImmutableMap.of(); }/*from www . j a v a2 s . c o m*/ byte headersType = messageHeader.readByte(); if (headersType != expectedHeadersType) { return ImmutableMap.of(); } ImmutableMap.Builder<String, String> headers = ImmutableMap.builder(); int headerCount = readVariableLengthInt(messageHeader); for (int i = 0; i < headerCount; i++) { String key = readString(messageHeader); String value = readString(messageHeader); headers.put(key, value); } return headers.build(); }
From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java
License:Apache License
private static int readVariableLengthInt(ByteBuf messageHeader) { int result = 0; int shift = 0; while (true) { byte b = messageHeader.readByte(); result |= (b & 0x7f) << shift; if ((b & 0x80) != 0x80) { break; }/*from w w w .j a v a 2 s . co m*/ shift += 7; } return result; }
From source file:io.airlift.drift.transport.netty.HeaderMessageEncoding.java
License:Apache License
private static Map<String, String> decodeHeaders(int expectedHeadersType, ByteBuf messageHeader) { if (messageHeader.readableBytes() == 0) { return ImmutableMap.of(); }/*from ww w. jav a 2s . c o m*/ byte headersType = messageHeader.readByte(); if (headersType != expectedHeadersType) { return ImmutableMap.of(); } ImmutableMap.Builder<String, String> headers = ImmutableMap.builder(); int headerCount = readVarint(messageHeader); for (int i = 0; i < headerCount; i++) { String key = readString(messageHeader); String value = readString(messageHeader); headers.put(key, value); } return headers.build(); }