Java Code Examples for io.netty.buffer.ByteBuf#readUnsignedMediumLE()

The following examples show how to use io.netty.buffer.ByteBuf#readUnsignedMediumLE() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VarIntUtils.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
/**
 * Note: it will change {@code buf} readerIndex.
 *
 * @param buf a readable buffer include a var integer.
 * @return A var integer read from buffer.
 */
public static long readVarInt(ByteBuf buf) {
    requireNonNull(buf, "buf must not be null");

    short firstByte = buf.readUnsignedByte();

    if (firstByte < VAR_INT_2_BYTE_CODE) {
        return firstByte;
    } else if (firstByte == VAR_INT_2_BYTE_CODE) {
        return buf.readUnsignedShortLE();
    } else if (firstByte == VAR_INT_3_BYTE_CODE) {
        return buf.readUnsignedMediumLE();
    } else {
        return buf.readLongLE();
    }
}
 
Example 2
Source File: VarIntUtils.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
/**
 * Note: it will change {@code buf} readerIndex.
 *
 * @param buf a readable buffer include a var integer.
 * @return A var integer read from buffer.
 */
public static long readVarInt(ByteBuf buf) {
    requireNonNull(buf, "buf must not be null");

    short firstByte = buf.readUnsignedByte();

    if (firstByte < VAR_INT_2_BYTE_CODE) {
        return firstByte;
    } else if (firstByte == VAR_INT_2_BYTE_CODE) {
        return buf.readUnsignedShortLE();
    } else if (firstByte == VAR_INT_3_BYTE_CODE) {
        return buf.readUnsignedMediumLE();
    } else {
        return buf.readLongLE();
    }
}
 
Example 3
Source File: CodecUtils.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
public static long readLengthEncodedInteger(ByteBuf buf, int firstByte) {
	firstByte = firstByte & 0xff;
	if (firstByte < NULL_VALUE) {
		return firstByte;
	}
	if (firstByte == NULL_VALUE) {
		return -1;
	}
	if (firstByte == SHORT_VALUE) {
		return buf.readUnsignedShortLE();
	}
	if (firstByte == MEDIUM_VALUE) {
		return buf.readUnsignedMediumLE();
	}
	if (firstByte == LONG_VALUE) {
		long length = buf.readLongLE();
		if (length < 0) {
			throw new CodecException("Received a length value too large to handle: " + Long.toHexString(length));
		}
		return length;
	}
	throw new CodecException("Received an invalid length value " + firstByte);
}
 
Example 4
Source File: AbstractPacketDecoder.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
	if (in.isReadable(4)) {
		in.markReaderIndex();
		int packetSize = in.readUnsignedMediumLE();
		if (packetSize > maxPacketSize) {
			throw new TooLongFrameException("Received a packet of size " + packetSize + " but the maximum packet size is " + maxPacketSize);
		}
		int sequenceId = in.readByte();
		if (!in.isReadable(packetSize)) {
			in.resetReaderIndex();
			return;
		}
		ByteBuf packet = in.readSlice(packetSize);

		decodePacket(ctx, sequenceId, packet, out);
	}
}
 
Example 5
Source File: ServerMessageDecoder.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
private boolean readNotFinish(ByteBuf envelope, @Nullable SequenceIdProvider.Linkable idProvider) {
    try {
        int size = envelope.readUnsignedMediumLE();
        if (size < Envelopes.MAX_ENVELOPE_SIZE) {
            if (idProvider == null) {
                // Just ignore sequence id because of no need link any message.
                envelope.skipBytes(1);
            } else {
                // Link last message.
                idProvider.last(envelope.readUnsignedByte());
            }

            parts.add(envelope);
            // success, no need release
            envelope = null;
            return false;
        } else {
            // skip the sequence Id
            envelope.skipBytes(1);
            parts.add(envelope);
            // success, no need release
            envelope = null;
            return true;
        }
    } finally {
        if (envelope != null) {
            envelope.release();
        }
    }
}
 
Example 6
Source File: DefinitionMetadataMessage.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
private static DefinitionMetadataMessage decode320(ByteBuf buf, ConnectionContext context) {
    CharCollation collation = context.getClientCollation();
    Charset charset = collation.getCharset();
    String table = readVarIntSizedString(buf, charset);
    String column = readVarIntSizedString(buf, charset);

    buf.skipBytes(1); // Constant 0x3
    int size = buf.readUnsignedMediumLE();

    buf.skipBytes(1); // Constant 0x1
    short type = buf.readUnsignedByte();

    buf.skipBytes(1); // Constant 0x3
    short definitions = buf.readShortLE();
    short decimals = buf.readUnsignedByte();

    return new DefinitionMetadataMessage(
        null,
        table,
        null,
        column,
        null,
        collation.getId(),
        size,
        type,
        definitions,
        decimals
    );
}
 
Example 7
Source File: ServerMessageDecoder.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
private boolean readNotFinish(ByteBuf envelope, @Nullable SequenceIdProvider.Linkable idProvider) {
    try {
        int size = envelope.readUnsignedMediumLE();
        if (size < Envelopes.MAX_ENVELOPE_SIZE) {
            if (idProvider == null) {
                // Just ignore sequence id because of no need link any message.
                envelope.skipBytes(1);
            } else {
                // Link last message.
                idProvider.last(envelope.readUnsignedByte());
            }

            parts.add(envelope);
            // success, no need release
            envelope = null;
            return false;
        } else {
            // skip the sequence Id
            envelope.skipBytes(1);
            parts.add(envelope);
            // success, no need release
            envelope = null;
            return true;
        }
    } finally {
        if (envelope != null) {
            envelope.release();
        }
    }
}
 
Example 8
Source File: DefinitionMetadataMessage.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
private static DefinitionMetadataMessage decode320(ByteBuf buf, ConnectionContext context) {
    CharCollation collation = context.getClientCollation();
    Charset charset = collation.getCharset();
    String table = readVarIntSizedString(buf, charset);
    String column = readVarIntSizedString(buf, charset);

    buf.skipBytes(1); // Constant 0x3
    int size = buf.readUnsignedMediumLE();

    buf.skipBytes(1); // Constant 0x1
    short type = buf.readUnsignedByte();

    buf.skipBytes(1); // Constant 0x3
    short definitions = buf.readShortLE();
    short decimals = buf.readUnsignedByte();

    return new DefinitionMetadataMessage(
        null,
        table,
        null,
        column,
        null,
        collation.getId(),
        size,
        type,
        definitions,
        decimals
    );
}
 
Example 9
Source File: MSSQLDataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private static LocalTime decodeTimeN(TimeNDataType dataType, ByteBuf in) {
  int scale = dataType.scale();
  byte timeLength = in.readByte();
  long timeValue;
  switch (timeLength) {
    case 0:
      return null;
    case 3:
      timeValue = in.readUnsignedMediumLE();
      break;
    case 4:
      timeValue = in.readUnsignedIntLE();
      break;
    case 5:
      timeValue = readUnsignedInt40LE(in);
      break;
    default:
      throw new IllegalStateException("Unexpected timeLength of [" + timeLength + "]");
  }
  for (int i = 0; i < 7 - scale; i++) {
    timeValue *= 10;
  }
  timeValue = (long) (timeValue * Math.pow(10, 7 - scale));
  long secondsValue = timeValue / 100000000;
  long nanosValue = timeValue % 100000000;
  return LocalTime.ofSecondOfDay(secondsValue).plusNanos(nanosValue);
}
 
Example 10
Source File: MSSQLDataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private static LocalDate decodeDateN(ByteBuf in) {
  byte dateLength = in.readByte();
  if (dateLength == 0) {
    return null;
  } else if (dateLength == 3) {
    int days = in.readUnsignedMediumLE();
    return START_DATE.plus(days, ChronoUnit.DAYS);
  } else {
    throw new IllegalStateException("Unexpected dateLength of [" + dateLength + "]");
  }
}
 
Example 11
Source File: MySQLDecoder.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  if (in.readableBytes() > 4) {
    int packetStartIdx = in.readerIndex();
    int payloadLength = in.readUnsignedMediumLE();
    int sequenceId = in.readUnsignedByte();

    if (payloadLength >= PACKET_PAYLOAD_LENGTH_LIMIT && aggregatedPacketPayload == null) {
      aggregatedPacketPayload = ctx.alloc().compositeBuffer();
    }

    // payload
    if (in.readableBytes() >= payloadLength) {
      if (aggregatedPacketPayload != null) {
        // read a split packet
        aggregatedPacketPayload.addComponent(true, in.readRetainedSlice(payloadLength));

        if (payloadLength < PACKET_PAYLOAD_LENGTH_LIMIT) {
          // we have just read the last split packet and there will be no more split packet
          try {
            decodePacket(aggregatedPacketPayload, aggregatedPacketPayload.readableBytes(), sequenceId);
          } finally {
            ReferenceCountUtil.release(aggregatedPacketPayload);
            aggregatedPacketPayload = null;
          }
        }
      } else {
        // read a non-split packet
        decodePacket(in.readSlice(payloadLength), payloadLength, sequenceId);
      }
    } else {
      in.readerIndex(packetStartIdx);
    }
  }
}
 
Example 12
Source File: BufferUtils.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static long readLengthEncodedInteger(ByteBuf buffer) {
  short firstByte = buffer.readUnsignedByte();
  switch (firstByte) {
    case 0xFB:
      return -1;
    case 0xFC:
      return buffer.readUnsignedShortLE();
    case 0xFD:
      return buffer.readUnsignedMediumLE();
    case 0xFE:
      return buffer.readLongLE();
    default:
      return firstByte;
  }
}
 
Example 13
Source File: VarIntUtils.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
private static long crossReadLong0(ByteBuf firstPart, ByteBuf secondPart) {
    int readable = firstPart.readableBytes();

    if (readable == 0) {
        return secondPart.readLongLE();
    }

    long low, middle, high;

    switch (readable) {
        case 1:
            low = firstPart.readUnsignedByte();
            middle = (secondPart.readUnsignedIntLE() << Byte.SIZE);
            high = ((long) secondPart.readUnsignedMediumLE()) << (Byte.SIZE + Integer.SIZE);

            return high | middle | low;
        case 2:
            low = firstPart.readUnsignedShortLE();
            middle = secondPart.readUnsignedIntLE() << Short.SIZE;
            high = ((long) secondPart.readUnsignedShortLE()) << (Short.SIZE + Integer.SIZE);

            return high | middle | low;
        case 3:
            low = firstPart.readUnsignedMediumLE();
            middle = secondPart.readUnsignedIntLE() << MEDIUM_SIZE;
            high = ((long) secondPart.readUnsignedByte()) << (MEDIUM_SIZE + Integer.SIZE);

            return high | middle | low;
        case 4:
            low = firstPart.readUnsignedIntLE();
            high = secondPart.readUnsignedIntLE() << Integer.SIZE;

            return high | low;
        case 5:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedByte()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedMediumLE()) << (Integer.SIZE + Byte.SIZE);

            return high | middle | low;
        case 6:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedShortLE()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedShortLE()) << (Integer.SIZE + Short.SIZE);

            return high | middle | low;
        case 7:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedMediumLE()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedByte()) << (Integer.SIZE + MEDIUM_SIZE);

            return high | middle | low;
        default:
            return firstPart.readLongLE();
    }
}
 
Example 14
Source File: Snappy.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
/**
 * Reads a literal from the input buffer directly to the output buffer.
 * A "literal" is an uncompressed segment of data stored directly in the
 * byte stream.从输入缓冲区直接读入输出缓冲区。“文字”是直接存储在字节流中的未压缩数据段。
 *
 * @param tag The tag that identified this segment as a literal is also
 *            used to encode part of the length of the data
 * @param in The input buffer to read the literal from
 * @param out The output buffer to write the literal to
 * @return The number of bytes appended to the output buffer, or -1 to indicate "try again later"
 */
static int decodeLiteral(byte tag, ByteBuf in, ByteBuf out) {
    in.markReaderIndex();
    int length;
    switch(tag >> 2 & 0x3F) {
    case 60:
        if (!in.isReadable()) {
            return NOT_ENOUGH_INPUT;
        }
        length = in.readUnsignedByte();
        break;
    case 61:
        if (in.readableBytes() < 2) {
            return NOT_ENOUGH_INPUT;
        }
        length = in.readUnsignedShortLE();
        break;
    case 62:
        if (in.readableBytes() < 3) {
            return NOT_ENOUGH_INPUT;
        }
        length = in.readUnsignedMediumLE();
        break;
    case 63:
        if (in.readableBytes() < 4) {
            return NOT_ENOUGH_INPUT;
        }
        length = in.readIntLE();
        break;
    default:
        length = tag >> 2 & 0x3F;
    }
    length += 1;

    if (in.readableBytes() < length) {
        in.resetReaderIndex();
        return NOT_ENOUGH_INPUT;
    }

    out.writeBytes(in, length);
    return length;
}
 
Example 15
Source File: VarIntUtils.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
private static long crossReadLong0(ByteBuf firstPart, ByteBuf secondPart) {
    int readable = firstPart.readableBytes();

    if (readable == 0) {
        return secondPart.readLongLE();
    }

    long low, middle, high;

    switch (readable) {
        case 1:
            low = firstPart.readUnsignedByte();
            middle = (secondPart.readUnsignedIntLE() << Byte.SIZE);
            high = ((long) secondPart.readUnsignedMediumLE()) << (Byte.SIZE + Integer.SIZE);

            return high | middle | low;
        case 2:
            low = firstPart.readUnsignedShortLE();
            middle = secondPart.readUnsignedIntLE() << Short.SIZE;
            high = ((long) secondPart.readUnsignedShortLE()) << (Short.SIZE + Integer.SIZE);

            return high | middle | low;
        case 3:
            low = firstPart.readUnsignedMediumLE();
            middle = secondPart.readUnsignedIntLE() << MEDIUM_SIZE;
            high = ((long) secondPart.readUnsignedByte()) << (MEDIUM_SIZE + Integer.SIZE);

            return high | middle | low;
        case 4:
            low = firstPart.readUnsignedIntLE();
            high = secondPart.readUnsignedIntLE() << Integer.SIZE;

            return high | low;
        case 5:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedByte()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedMediumLE()) << (Integer.SIZE + Byte.SIZE);

            return high | middle | low;
        case 6:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedShortLE()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedShortLE()) << (Integer.SIZE + Short.SIZE);

            return high | middle | low;
        case 7:
            low = firstPart.readUnsignedIntLE();
            middle = ((long) firstPart.readUnsignedMediumLE()) << Integer.SIZE;
            high = ((long) secondPart.readUnsignedByte()) << (Integer.SIZE + MEDIUM_SIZE);

            return high | middle | low;
        default:
            return firstPart.readLongLE();
    }
}