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

The following are Jave code examples for showing how to use fromByteArray() 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.
+ Save this method
Example 1
Project: ja-micro   File: ProtobufHandler.java   View Source Code Vote up 6 votes
private Message readRpcBody(ServletInputStream in,
                            Class<? extends Message> requestClass) throws Exception {
    byte chunkSize[] = new byte[4];
    in.read(chunkSize);
    int size = Ints.fromByteArray(chunkSize);
    if (size == 0) {
        return ProtobufUtil.newEmptyMessage(requestClass);
    }
    if (size > ProtobufUtil.MAX_BODY_CHUNK_SIZE) {
        String message = "Invalid body chunk size: " + size;
        throw new RpcReadException(chunkSize, in, message);
    }
    byte bodyData[] = readyFully(in, size);
    Message pbRequest = ProtobufUtil.byteArrayToProtobuf(bodyData, requestClass);
    return pbRequest;
}
 
Example 2
Project: ja-micro   File: ProtobufRpcResponse.java   View Source Code Vote up 5 votes
public ProtobufRpcResponse(byte[] data) throws RpcCallException {
    //we may get a json error even though we make a protobuf call
    if (data[0] == '{' && data[data.length - 1] == '}') {
        readRpcError(new String(data));
        return;
    }
    int headerLength = Ints.fromByteArray(data);
    if (headerLength < 0 || headerLength > MAX_HEADER_SIZE) {
        StringBuilder sb = new StringBuilder();
        sb.append("Unexpected header length: ").append(headerLength).
                append(", data: ").append(ensurePrintable(data, 256));
        String message = sb.toString();
        logger.warn(message);
        throw new RpcCallException(RpcCallException.Category.InternalServerError, message);
    }
    logger.debug("headerLength = {}", headerLength);
    int offset = 4;
    byte headerData[] = Arrays.copyOfRange(data, offset, offset + headerLength);
    offset += headerLength;
    byte payloadLengthBuffer[] = Arrays.copyOfRange(data, offset, offset + 4);
    offset += 4;
    int payloadLength = Ints.fromByteArray(payloadLengthBuffer);
    payloadData = Arrays.copyOfRange(data, offset, offset + payloadLength);
    RpcEnvelope.Response responseHeader = ProtobufUtil.
            byteArrayToProtobuf(headerData, RpcEnvelope.Response.class);
    serviceMethod = responseHeader.getServiceMethod();
    sequenceNumber = responseHeader.getSequenceNumber();
    errorMessage = responseHeader.getError();
}
 
Example 3
Project: ja-micro   File: ProtobufHandler.java   View Source Code Vote up 5 votes
private RpcEnvelope.Request readRpcEnvelope(ServletInputStream in) throws Exception {
    byte chunkSize[] = new byte[4];
    in.read(chunkSize);
    int size = Ints.fromByteArray(chunkSize);
    if (size <= 0 || size > ProtobufUtil.MAX_HEADER_CHUNK_SIZE) {
        String message = "Invalid header chunk size: " + size;
        throw new RpcReadException(chunkSize, in, message);
    }
    byte headerData[] = readyFully(in, size);
    RpcEnvelope.Request rpcRequest = RpcEnvelope.Request.parseFrom(headerData);
    return rpcRequest;
}
 
Example 4
Project: BUbiNG   File: RuntimeConfiguration.java   View Source Code Vote up 5 votes
/** Converts a string specifying an IPv4 address into an integer. The string can be either a single integer (representing
 *  the address) or a dot-separated 4-tuple of bytes.
 *
 * @param s the string to be converted.
 * @return the integer representing the IP address specified by s.
 * @throws ConfigurationException
 */
private int handleIPv4(final String s) throws ConfigurationException {
	try {
		if (!RuntimeConfiguration.DOTTED_ADDRESS.matcher(s).matches()) throw new ConfigurationException("Malformed IPv4 " + s + " for blacklisting");
		// Note that since we're sure this is a dotted-notation address, we pass directly through InetAddress.
		final byte[] address = InetAddress.getByName(s).getAddress();
		if (address.length > 4) throw new UnknownHostException("Not IPv4");
		return Ints.fromByteArray(address);
	} catch (final UnknownHostException e) {
		throw new ConfigurationException("Malformed IPv4 " + s + " for blacklisting", e);
	}
}
 
Example 5
Project: hadoop-oss   File: MiniZKFCCluster.java   View Source Code Vote up 4 votes
@Override
protected HAServiceTarget dataToTarget(byte[] data) {
  int index = Ints.fromByteArray(data);
  return DummyHAService.getInstance(index);
}
 
Example 6
Project: hadoop   File: MiniZKFCCluster.java   View Source Code Vote up 4 votes
@Override
protected HAServiceTarget dataToTarget(byte[] data) {
  int index = Ints.fromByteArray(data);
  return DummyHAService.getInstance(index);
}
 
Example 7
Project: util   File: NumberUtil.java   View Source Code Vote up 4 votes
public static int toInt(byte[] bytes) {
	return Ints.fromByteArray(bytes);
}