Java Code Examples for com.google.common.primitives.Ints.fromBytes()

The following are Jave code examples for showing how to use fromBytes() of the com.google.common.primitives.Ints class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: guava-mock   File: InetAddresses.java   Source Code and License Vote up 6 votes
/**
 * Returns the string representation of an {@link InetAddress}.
 *
 * <p>For IPv4 addresses, this is identical to {@link InetAddress#getHostAddress()}, but for IPv6
 * addresses, the output follows <a href="http://tools.ietf.org/html/rfc5952">RFC 5952</a> section
 * 4. The main difference is that this method uses "::" for zero compression, while Java's version
 * uses the uncompressed form.
 *
 * <p>This method uses hexadecimal for all IPv6 addresses, including IPv4-mapped IPv6 addresses
 * such as "::c000:201". The output does not include a Scope ID.
 *
 * @param ip {@link InetAddress} to be converted to an address string
 * @return {@code String} containing the text-formatted IP address
 * @since 10.0
 */
public static String toAddrString(InetAddress ip) {
  checkNotNull(ip);
  if (ip instanceof Inet4Address) {
    // For IPv4, Java's formatting is good enough.
    return ip.getHostAddress();
  }
  checkArgument(ip instanceof Inet6Address);
  byte[] bytes = ip.getAddress();
  int[] hextets = new int[IPV6_PART_COUNT];
  for (int i = 0; i < hextets.length; i++) {
    hextets[i] = Ints.fromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]);
  }
  compressLongestRunOfZeroes(hextets);
  return hextetsToIPv6String(hextets);
}
 
Example 2
Project: googles-monorepo-demo   File: InetAddresses.java   Source Code and License Vote up 6 votes
/**
 * Returns the string representation of an {@link InetAddress}.
 *
 * <p>For IPv4 addresses, this is identical to {@link InetAddress#getHostAddress()}, but for IPv6
 * addresses, the output follows <a href="http://tools.ietf.org/html/rfc5952">RFC 5952</a> section
 * 4. The main difference is that this method uses "::" for zero compression, while Java's version
 * uses the uncompressed form.
 *
 * <p>This method uses hexadecimal for all IPv6 addresses, including IPv4-mapped IPv6 addresses
 * such as "::c000:201". The output does not include a Scope ID.
 *
 * @param ip {@link InetAddress} to be converted to an address string
 * @return {@code String} containing the text-formatted IP address
 * @since 10.0
 */
public static String toAddrString(InetAddress ip) {
  checkNotNull(ip);
  if (ip instanceof Inet4Address) {
    // For IPv4, Java's formatting is good enough.
    return ip.getHostAddress();
  }
  checkArgument(ip instanceof Inet6Address);
  byte[] bytes = ip.getAddress();
  int[] hextets = new int[IPV6_PART_COUNT];
  for (int i = 0; i < hextets.length; i++) {
    hextets[i] = Ints.fromBytes((byte) 0, (byte) 0, bytes[2 * i], bytes[2 * i + 1]);
  }
  compressLongestRunOfZeroes(hextets);
  return hextetsToIPv6String(hextets);
}
 
Example 3
Project: guava-mock   File: LittleEndianDataInputStream.java   Source Code and License Vote up 5 votes
/**
 * Reads an unsigned {@code short} as specified by {@link DataInputStream#readUnsignedShort()},
 * except using little-endian byte order.
 *
 * @return the next two bytes of the input stream, interpreted as an unsigned 16-bit integer in
 *     little-endian byte order
 * @throws IOException if an I/O error occurs
 */
@CanIgnoreReturnValue // to skip some bytes
@Override
public int readUnsignedShort() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();

  return Ints.fromBytes((byte) 0, (byte) 0, b2, b1);
}
 
Example 4
Project: guava-mock   File: LittleEndianDataInputStream.java   Source Code and License Vote up 5 votes
/**
 * Reads an integer as specified by {@link DataInputStream#readInt()}, except using little-endian
 * byte order.
 *
 * @return the next four bytes of the input stream, interpreted as an {@code int} in little-endian
 *     byte order
 * @throws IOException if an I/O error occurs
 */
@CanIgnoreReturnValue // to skip some bytes
@Override
public int readInt() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();
  byte b3 = readAndCheckByte();
  byte b4 = readAndCheckByte();

  return Ints.fromBytes(b4, b3, b2, b1);
}
 
Example 5
Project: webauthndemo   File: AuthenticatorData.java   Source Code and License Vote up 5 votes
/**
 * @param authData
 * @return Decoded AuthenticatorData object
 * @throws ResponseException
 */
public static AuthenticatorData decode(byte[] authData) throws ResponseException {
  if (authData.length < 37) {
    throw new ResponseException("Invalid input");
  }

  int index = 0;
  byte[] rpIdHash = new byte[32];
  System.arraycopy(authData, 0, rpIdHash, 0, 32);
  index += 32;
  byte flags = authData[index++];
  int signCount =
      Ints.fromBytes(authData[index++], authData[index++], authData[index++], authData[index++]);

  AttestationData attData = null;
  // Bit 6 determines whether attestation data was included
  if ((flags & 1 << 6) != 0) {
    byte[] remainder = new byte[authData.length - index];
    System.arraycopy(authData, index, remainder, 0, authData.length - index);
    try {
      attData = AttestationData.decode(remainder);
    } catch (CborException e) {
      throw new ResponseException("Error decoding");
    }
  }

  return new AuthenticatorData(rpIdHash, flags, signCount, attData);
}
 
Example 6
Project: crypto-core   File: Sha256Hash.java   Source Code and License Vote up 5 votes
/**
 * Returns the last four bytes of the wrapped hash. This should be unique
 * enough to be a suitable hash code even for blocks, where the goal is to
 * try and get the first bytes to be zeros (i.e. the value as a big integer
 * lower than the target value).
 */
@Override
public int hashCode() {
    // Use the last 4 bytes, not the first 4 which are often zeros in
    // Bitcoin.
    return Ints.fromBytes(bytes[LENGTH - 4], bytes[LENGTH - 3], bytes[LENGTH - 2], bytes[LENGTH - 1]);
}
 
Example 7
Project: googles-monorepo-demo   File: LittleEndianDataInputStream.java   Source Code and License Vote up 5 votes
/**
 * Reads an unsigned {@code short} as specified by {@link DataInputStream#readUnsignedShort()},
 * except using little-endian byte order.
 *
 * @return the next two bytes of the input stream, interpreted as an unsigned 16-bit integer in
 *     little-endian byte order
 * @throws IOException if an I/O error occurs
 */
@CanIgnoreReturnValue // to skip some bytes
@Override
public int readUnsignedShort() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();

  return Ints.fromBytes((byte) 0, (byte) 0, b2, b1);
}
 
Example 8
Project: googles-monorepo-demo   File: LittleEndianDataInputStream.java   Source Code and License Vote up 5 votes
/**
 * Reads an integer as specified by {@link DataInputStream#readInt()}, except using little-endian
 * byte order.
 *
 * @return the next four bytes of the input stream, interpreted as an {@code int} in little-endian
 *     byte order
 * @throws IOException if an I/O error occurs
 */
@CanIgnoreReturnValue // to skip some bytes
@Override
public int readInt() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();
  byte b3 = readAndCheckByte();
  byte b4 = readAndCheckByte();

  return Ints.fromBytes(b4, b3, b2, b1);
}
 
Example 9
Project: bitcoinj-thin   File: BtcECKey.java   Source Code and License Vote up 5 votes
@Override
public int hashCode() {
    // Public keys are random already so we can just use a part of them as the hashcode. Read from the start to
    // avoid picking up the type code (compressed vs uncompressed) which is tacked on the end.
    byte[] bits = getPubKey();
    return Ints.fromBytes(bits[0], bits[1], bits[2], bits[3]);
}
 
Example 10
Project: rskj   File: Sha3Hash.java   Source Code and License Vote up 4 votes
/**
 * Returns the last four bytes of the wrapped hash. This should be unique enough to be a suitable hash code even for
 * blocks, where the goal is to try and get the first bytes to be zeros (i.e. the value as a big integer lower
 * than the target value).
 */
@Override
public int hashCode() {
    // Use the last 4 bytes, not the first 4 which are often zeros in Bitcoin.
    return Ints.fromBytes(bytes[28], bytes[29], bytes[30], bytes[31]);
}