List of usage examples for io.netty.buffer ByteBuf readUnsignedShort
public abstract int readUnsignedShort();
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static String readString(ByteBuf cb) { try {//from w ww. ja v a2 s. c om int length = cb.readUnsignedShort(); return readString(cb, length); } catch (IndexOutOfBoundsException e) { throw new DriverInternalError( "Not enough bytes to read an UTF8 serialized string preceded by it's 2 bytes length"); } }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static byte[] readBytes(ByteBuf cb) { try {//w w w. j av a 2s. c o m int length = cb.readUnsignedShort(); byte[] bytes = new byte[length]; cb.readBytes(bytes); return bytes; } catch (IndexOutOfBoundsException e) { throw new DriverInternalError("Not enough bytes to read a byte array preceded by it's 2 bytes length"); } }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static Map<String, ByteBuffer> readBytesMap(ByteBuf cb) { int length = cb.readUnsignedShort(); ImmutableMap.Builder<String, ByteBuffer> builder = ImmutableMap.builder(); for (int i = 0; i < length; i++) { String key = readString(cb); ByteBuffer value = readValue(cb); if (value == null) value = Statement.NULL_PAYLOAD_VALUE; builder.put(key, value);/*from ww w.j a va 2 s . co m*/ } return builder.build(); }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static ConsistencyLevel readConsistencyLevel(ByteBuf cb) { return ConsistencyLevel.fromCode(cb.readUnsignedShort()); }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static List<String> readStringList(ByteBuf cb) { int length = cb.readUnsignedShort(); List<String> l = new ArrayList<String>(length); for (int i = 0; i < length; i++) l.add(readString(cb));//w w w . ja v a 2 s. com return l; }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static Map<String, String> readStringMap(ByteBuf cb) { int length = cb.readUnsignedShort(); Map<String, String> m = new HashMap<String, String>(length); for (int i = 0; i < length; i++) { String k = readString(cb).toUpperCase(); String v = readString(cb); m.put(k, v);//from w w w . ja v a 2 s . co m } return m; }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static Map<String, List<String>> readStringToStringListMap(ByteBuf cb) { int length = cb.readUnsignedShort(); Map<String, List<String>> m = new HashMap<String, List<String>>(length); for (int i = 0; i < length; i++) { String k = readString(cb).toUpperCase(); List<String> v = readStringList(cb); m.put(k, v);// w w w . j a v a 2 s .co m } return m; }
From source file:com.datastax.driver.core.CBUtil.java
License:Apache License
public static List<ByteBuffer> readValueList(ByteBuf cb) { int size = cb.readUnsignedShort(); if (size == 0) return Collections.<ByteBuffer>emptyList(); List<ByteBuffer> l = new ArrayList<ByteBuffer>(size); for (int i = 0; i < size; i++) l.add(readValue(cb));/*from ww w. j a va 2s. c o m*/ return l; }
From source file:com.datastax.driver.core.DataType.java
License:Apache License
static DataType decode(ByteBuf buffer) { Name name = Name.fromProtocolId(buffer.readUnsignedShort()); switch (name) { case CUSTOM:/*w w w .j a va2 s . c om*/ String className = CBUtil.readString(buffer); return CassandraTypeParser.isUserType(className) || CassandraTypeParser.isTupleType(className) ? CassandraTypeParser.parseOne(className) : custom(className); case LIST: return list(decode(buffer)); case SET: return set(decode(buffer)); case MAP: DataType keys = decode(buffer); DataType values = decode(buffer); return map(keys, values); case UDT: String keyspace = CBUtil.readString(buffer); String type = CBUtil.readString(buffer); int nFields = buffer.readShort() & 0xffff; List<UserType.Field> fields = new ArrayList<UserType.Field>(nFields); for (int i = 0; i < nFields; i++) { String fieldName = CBUtil.readString(buffer); DataType fieldType = decode(buffer); fields.add(new UserType.Field(fieldName, fieldType)); } return new UserType(keyspace, type, fields); case TUPLE: nFields = buffer.readShort() & 0xffff; List<DataType> types = new ArrayList<DataType>(nFields); for (int i = 0; i < nFields; i++) { types.add(decode(buffer)); } return new TupleType(types); default: return primitiveTypeMap.get(name); } }
From source file:com.dempe.chat.common.mqtt.codec.ConnectDecoder.java
License:Open Source License
@Override void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex();/*from w w w. j av a 2 s . com*/ //Common decoding part ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); int protocolNameLen = in.readUnsignedShort(); byte[] encProtoName; String protoName; Attribute<Integer> versionAttr = ctx.attr(MQTTDecoder.PROTOCOL_VERSION); switch (protocolNameLen) { case 6: //MQTT version 3.1 "MQIsdp" //ProtocolName 8 bytes or 6 bytes if (in.readableBytes() < 10) { in.resetReaderIndex(); return; } encProtoName = new byte[6]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) Utils.VERSION_3_1); break; case 4: //MQTT version 3.1.1 "MQTT" //ProtocolName 6 bytes if (in.readableBytes() < 8) { in.resetReaderIndex(); return; } encProtoName = new byte[4]; in.readBytes(encProtoName); protoName = new String(encProtoName, "UTF-8"); if (!"MQTT".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); versionAttr.set((int) Utils.VERSION_3_1_1); break; default: //protocol broken throw new CorruptedFrameException("Invalid protoName size: " + protocolNameLen); } //ProtocolVersion 1 byte (value 0x03 for 3.1, 0x04 for 3.1.1) message.setProtocolVersion(in.readByte()); if (message.getProtocolVersion() == Utils.VERSION_3_1_1) { //if 3.1.1, check the flags (dup, retain and qos == 0) if (message.isDupFlag() || message.isRetainFlag() || message.getQos() != AbstractMessage.QOSType.MOST_ONE) { throw new CorruptedFrameException("Received a CONNECT with fixed header flags != 0"); } //check if this is another connect from the same client on the same session Attribute<Boolean> connectAttr = ctx.attr(ConnectDecoder.CONNECT_STATUS); Boolean alreadyConnected = connectAttr.get(); if (alreadyConnected == null) { //never set connectAttr.set(true); } else if (alreadyConnected) { throw new CorruptedFrameException("Received a second CONNECT on the same network connection"); } } //Connection flag byte connFlags = in.readByte(); if (message.getProtocolVersion() == Utils.VERSION_3_1_1) { if ((connFlags & 0x01) != 0) { //bit(0) of connection flags is != 0 throw new CorruptedFrameException("Received a CONNECT with connectionFlags[0(bit)] != 0"); } } boolean cleanSession = ((connFlags & 0x02) >> 1) == 1; boolean willFlag = ((connFlags & 0x04) >> 2) == 1; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1; boolean userFlag = ((connFlags & 0x80) >> 7) == 1; //a password is true iff user is true. if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException( "Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); //Keep Alive timer 2 bytes //int keepAlive = Utils.readWord(in); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if ((remainingLength == 12 && message.getProtocolVersion() == Utils.VERSION_3_1) || (remainingLength == 10 && message.getProtocolVersion() == Utils.VERSION_3_1_1)) { out.add(message); return; } //Decode the ClientID String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return; } message.setClientID(clientID); //Decode willTopic if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return; } message.setWillTopic(willTopic); } //Decode willMessage if (willFlag) { byte[] willMessage = Utils.readFixedLengthContent(in); if (willMessage == null) { in.resetReaderIndex(); return; } message.setWillMessage(willMessage); } //Compatibility check with v3.0, remaining length has precedence over //the user and password flags int readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } //Decode username if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } //Decode password if (passwordFlag) { byte[] password = Utils.readFixedLengthContent(in); if (password == null) { in.resetReaderIndex(); return; } message.setPassword(password); } out.add(message); }