Java Code Examples for com.google.common.primitives.UnsignedBytes#toString()

The following examples show how to use com.google.common.primitives.UnsignedBytes#toString() . 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: ParsingTools.java    From mongowp with Apache License 2.0 6 votes vote down vote up
protected static BinarySubtype getBinarySubtype(byte readByte) {
  switch (readByte) {
    case 0x00:
      return BinarySubtype.GENERIC;
    case 0x01:
      return BinarySubtype.FUNCTION;
    case 0x02:
      return BinarySubtype.OLD_BINARY;
    case 0x03:
      return BinarySubtype.OLD_UUID;
    case 0x04:
      return BinarySubtype.UUID;
    case 0x05:
      return BinarySubtype.MD5;
    default: {
      if (UnsignedBytes.compare(readByte, FIRST_USER_DEFINED) >= 0) {
        return BinarySubtype.USER_DEFINED;
      } else {
        throw new AssertionError(
            "Unrecognized binary type 0x" + UnsignedBytes.toString(readByte, 16));
      }
    }
  }
}
 
Example 2
Source File: MongoBsonTranslator.java    From mongowp with Apache License 2.0 6 votes vote down vote up
protected static BinarySubtype getBinarySubtype(byte readByte) {
  switch (readByte) {
    case 0x00:
      return BinarySubtype.GENERIC;
    case 0x01:
      return BinarySubtype.FUNCTION;
    case 0x02:
      return BinarySubtype.OLD_BINARY;
    case 0x03:
      return BinarySubtype.OLD_UUID;
    case 0x04:
      return BinarySubtype.UUID;
    case 0x05:
      return BinarySubtype.MD5;
    default:
    {
      if (UnsignedBytes.compare(readByte, FIRST_USER_DEFINED) >= 0) {
        return BinarySubtype.USER_DEFINED;
      } else {
        throw new AssertionError(
                "Unrecognized binary type 0x" + UnsignedBytes.toString(readByte, 16));
      }
    }
  }
}
 
Example 3
Source File: AsfRsspSessionAuthentication.java    From ipmi4j with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static Payload fromWire(@Nonnull ByteBuffer buffer) {
    if (!buffer.hasRemaining())
        return EndOfList.INSTANCE;

    byte type = buffer.get();
    AbstractWireable.assertWireByte(buffer, (byte) 0, "reserved byte");
    short length = buffer.getShort();
    byte[] data = new byte[length - 4];
    buffer.get(data);

    switch (buffer.get()) {
        case 0:
            return EndOfList.INSTANCE;
        case 1:
            return Code.fromByte(AuthenticationAlgorithm.class, data[0]);
        case 2:
            return Code.fromByte(IntegrityAlgorithm.class, data[0]);
        default:
            throw new IllegalArgumentException("Unknown algorithm type 0x" + UnsignedBytes.toString(type, 16));
    }
}
 
Example 4
Source File: BytesTest.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
@Test
void toHexStringAllHexNumbersLower() {
  for (byte b = 0; b <= 0xF; b++) {
    String expected = "0" + UnsignedBytes.toString(b, 16);
    assertThat(Bytes.toHexString(bytes(b)), equalTo(expected));
  }
}
 
Example 5
Source File: BytesTest.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
@Test
void toHexStringAllHexNumbersUpper() {
  for (int i = 1; i <= 0xF; i++) {
    byte b = (byte) (i << 4);
    String expected = UnsignedBytes.toString(b, 16);
    assertThat(Bytes.toHexString(bytes(b)), equalTo(expected));
  }
}
 
Example 6
Source File: DefaultNettyBsonLowLevelReader.java    From mongowp with Apache License 2.0 5 votes vote down vote up
@Override
BsonBoolean readBoolean(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
  byte readByte = byteBuf.readByte();
  if (readByte == 0x00) {
    return FalseBsonBoolean.getInstance();
  }
  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");
}
 
Example 7
Source File: AbstractIpmiCommand.java    From ipmi4j with Apache License 2.0 5 votes vote down vote up
public void fromWireChecksum(@Nonnull ByteBuffer buffer, @Nonnegative int start, @Nonnull String description) {
    byte expect = toChecksum(buffer, start);
    byte actual = buffer.get();
    if (expect != actual)
        throw new IllegalArgumentException("Checksum failure: " + description
                + ": expected=" + UnsignedBytes.toString(expect, 16)
                + " actual=" + UnsignedBytes.toString(actual, 16) + "; command=" + getClass().getSimpleName() + "; data=" + this);
}
 
Example 8
Source File: AbstractWireable.java    From ipmi4j with Apache License 2.0 5 votes vote down vote up
/** Reads a byte from the wire, and asserts it equal to the given expected value. */
public static void assertWireByte(@Nonnull ByteBuffer buffer, byte expectValue, @Nonnull String description) {
    byte actualValue = buffer.get();
    if (actualValue != expectValue)
        throw new IllegalArgumentException("In " + description + ": "
                + "Expected 0x" + UnsignedBytes.toString(expectValue, 16)
                + " but got 0x" + UnsignedBytes.toString(actualValue, 16));
}
 
Example 9
Source File: AbstractWireable.java    From ipmi4j with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String toHexString(@CheckForNull byte... data) {
    if (data == null)
        return "<null>";
    StringBuilder buf = new StringBuilder();
    buf.append("(").append(data.length).append(" bytes) ");
    for (byte b : data) {
        String s = UnsignedBytes.toString(b, 16);
        if (s.length() < 2)
            buf.append('0');
        buf.append(s).append(' ');
    }
    buf.setLength(buf.length() - 1);
    return buf.toString();
}
 
Example 10
Source File: Code.java    From ipmi4j with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static <T extends Enum<T> & Code.Wrapper> T fromByte(@Nonnull Class<T> type, byte code) {
    for (T value : type.getEnumConstants())
        if (value.getCode() == code)
            return value;
    throw new IllegalArgumentException("Unknown " + type.getSimpleName() + " code 0x" + UnsignedBytes.toString(code, 16));
}
 
Example 11
Source File: ParsingTools.java    From mongowp with Apache License 2.0 4 votes vote down vote up
/**
 * Translate a byte to the {@link BsonType} it represents, as specified on the
 * <a href="http://bsonspec.org/spec.html">BSON Spec</a>
 *
 * @param typeByte
 * @return
 * @throws NettyBsonReaderException
 */
@Nonnull
protected static BsonType getBsonType(byte typeByte) throws NettyBsonReaderException {
  switch (typeByte) {
    case 0x01:
      return DOUBLE;
    case 0x02:
      return STRING;
    case 0x03:
      return DOCUMENT;
    case 0x04:
      return ARRAY;
    case 0x05:
      return BINARY;
    case 0x06:
      return UNDEFINED;
    case 0x07:
      return OBJECT_ID;
    case 0x08:
      return BOOLEAN;
    case 0x09:
      return DATETIME;
    case 0x0A:
      return NULL;
    case 0x0B:
      return REGEX;
    case 0x0C:
      return DB_POINTER;
    case 0x0D:
      return JAVA_SCRIPT;
    case 0x0E:
      return DEPRECATED;
    case 0x0F:
      return JAVA_SCRIPT_WITH_SCOPE;
    case 0x10:
      return INT32;
    case 0x11:
      return TIMESTAMP;
    case 0x12:
      return INT64;
    case 0x13:
      return DECIMAL128;
    case UnsignedBytes.MAX_VALUE:
      return MIN;
    case 0x7F:
      return MAX;
    default:
      throw new NettyBsonReaderException("It is not defined the type associated with the byte "
          + UnsignedBytes.toString(typeByte, 16));
  }
}
 
Example 12
Source File: SDRDeviceSubtype.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return "Unknown(0x" + UnsignedBytes.toString(getCode(), 16) + ")";
}
 
Example 13
Source File: Code.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static <T extends Enum<T> & Code.DescriptiveWrapper> String toString(@Nonnull T value) {
    return value.name() + "(0x" + UnsignedBytes.toString(value.getCode(), 16) + "): " + value.getDescription();
}