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

The following are Jave code examples for showing how to use toByteArray() 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: hadoop-oss   File: TestActiveStandbyElectorRealZK.java   View Source Code Vote up 5 votes
@Override
public void setUp() throws Exception {
  super.setUp();
  
  zkServer = getServer(serverFactory);

  for (int i = 0; i < NUM_ELECTORS; i++) {
    cbs[i] =  Mockito.mock(ActiveStandbyElectorCallback.class);
    appDatas[i] = Ints.toByteArray(i);
    electors[i] = new ActiveStandbyElector(hostPort, 5000, PARENT_DIR,
        Ids.OPEN_ACL_UNSAFE, Collections.<ZKAuthInfo> emptyList(), cbs[i],
        CommonConfigurationKeys.HA_FC_ELECTOR_ZK_OP_RETRIES_DEFAULT);
  }
}
 
Example 2
Project: ja-micro   File: RpcClientIntegrationTest.java   View Source Code Vote up 5 votes
public void setResponseException(RpcCallException responseException) {
    this.responseException = responseException;
    if (featureFlag.equals("true")) {
        when(httpResponse.getStatus()).thenReturn(responseException.getCategory().getHttpStatus());
    }
    String response = "{\"error\":" + responseException.toJson() + ",\"result\":{}}";
    when(httpResponse.getContentAsString()).thenReturn(response);
    RpcEnvelope.Response pbResponse = RpcEnvelope.Response.newBuilder().
            setError(responseException.toJson().toString()).build();
    byte[] responseArray = pbResponse.toByteArray();
    byte[] headerLength = Ints.toByteArray(responseArray.length);
    byte[] bodyLength = Ints.toByteArray(0);
    byte[] overallPayload = concatAll(headerLength, responseArray, bodyLength);
    when(httpResponse.getContent()).thenReturn(overallPayload);
}
 
Example 3
Project: ja-micro   File: ProtobufRpcResponseTest.java   View Source Code Vote up 5 votes
@Test
public void testNegativeHeaderLength() throws Exception {
    byte[] data = Ints.toByteArray(-1);

    Throwable thrown = catchThrowable( () ->  new ProtobufRpcResponse(data));

    assertThat(thrown).isExactlyInstanceOf(RpcCallException.class);
    assertThat(thrown).hasFieldOrPropertyWithValue("category", RpcCallException.Category.InternalServerError);
}
 
Example 4
Project: ios-device-control   File: BinaryPlistSocket.java   View Source Code Vote up 5 votes
@Override
public void sendMessage(NSDictionary message) throws IOException {
  byte[] messageBytes = BinaryPropertyListWriter.writeToArray(message);
  byte[] lengthBytes = Ints.toByteArray(messageBytes.length);
  socketOut.write(lengthBytes);
  socketOut.write(messageBytes);
}
 
Example 5
Project: ios-device-control   File: BinaryPlistSocketTest.java   View Source Code Vote up 5 votes
@Test
public void testReceiveMessage() throws IOException {
  NSDictionary inputMessage = new NSDictionary();
  inputMessage.put("goodbye", "world");
  byte[] messageBytes = BinaryPropertyListWriter.writeToArray(inputMessage);
  byte[] lengthBytes = Ints.toByteArray(messageBytes.length);
  byte[] inputBytes = new byte[messageBytes.length + 4];
  System.arraycopy(lengthBytes, 0, inputBytes, 0, 4);
  System.arraycopy(messageBytes, 0, inputBytes, 4, messageBytes.length);

  FakeSocket fakeSocket = new FakeSocket(inputBytes);
  try (InspectorSocket socket = new BinaryPlistSocket(fakeSocket)) {
    assertThat(socket.receiveMessage().get()).isEqualTo(inputMessage);
  }
}
 
Example 6
Project: ios-device-control   File: BinaryPlistSocketTest.java   View Source Code Vote up 5 votes
@Test
public void testReceiveEOF() throws IOException {
  NSDictionary inputMessage = new NSDictionary();
  inputMessage.put("goodbye", "world");
  byte[] messageBytes = BinaryPropertyListWriter.writeToArray(inputMessage);
  byte[] lengthBytes = Ints.toByteArray(messageBytes.length + 1);
  byte[] inputBytes = new byte[messageBytes.length + 4];
  System.arraycopy(lengthBytes, 0, inputBytes, 0, 4);
  System.arraycopy(messageBytes, 0, inputBytes, 4, messageBytes.length);

  FakeSocket fakeSocket = new FakeSocket(inputBytes);
  try (InspectorSocket socket = new BinaryPlistSocket(fakeSocket)) {
    assertThat(socket.receiveMessage().isPresent()).isFalse();
  }
}
 
Example 7
Project: hadoop   File: TestActiveStandbyElectorRealZK.java   View Source Code Vote up 5 votes
@Override
public void setUp() throws Exception {
  super.setUp();
  
  zkServer = getServer(serverFactory);

  for (int i = 0; i < NUM_ELECTORS; i++) {
    cbs[i] =  Mockito.mock(ActiveStandbyElectorCallback.class);
    appDatas[i] = Ints.toByteArray(i);
    electors[i] = new ActiveStandbyElector(hostPort, 5000, PARENT_DIR,
        Ids.OPEN_ACL_UNSAFE, Collections.<ZKAuthInfo> emptyList(), cbs[i],
        CommonConfigurationKeys.HA_FC_ELECTOR_ZK_OP_RETRIES_DEFAULT);
  }
}
 
Example 8
Project: hadoop-oss   File: MiniZKFCCluster.java   View Source Code Vote up 4 votes
@Override
protected byte[] targetToData(HAServiceTarget target) {
  return Ints.toByteArray(((DummyHAService)target).index);
}
 
Example 9
Project: open-kilda   File: TopologyBuilder.java   View Source Code Vote up 4 votes
public static String intToSwitchId(int i){
    byte[] ib = Ints.toByteArray(i);
    return String.format("DE:AD:BE:EF:%02x:%02x:%02x:%02x",ib[0],ib[1],ib[2],ib[3]);
}
 
Example 10
Project: hadoop   File: MiniZKFCCluster.java   View Source Code Vote up 4 votes
@Override
protected byte[] targetToData(HAServiceTarget target) {
  return Ints.toByteArray(((DummyHAService)target).index);
}
 
Example 11
Project: util   File: NumberUtil.java   View Source Code Vote up 4 votes
public static byte[] toBytes(int value) {
	return Ints.toByteArray(value);
}
 
Example 12
Project: athena   File: Validation.java   View Source Code Vote up 3 votes
/**
 * Throws exception if length is not correct.
 *
 * @param errorCode Error code
 * @param subErrCode Sub Error Code
 * @param length erroneous length
 * @throws BgpParseException for erroneous length
 */
public static void validateLen(byte errorCode, byte subErrCode, int length) throws BgpParseException {
    byte[] errLen = Ints.toByteArray(length);
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    buffer.writeBytes(errLen);
    throw new BgpParseException(errorCode, subErrCode, buffer);
}
 
Example 13
Project: athena   File: Validation.java   View Source Code Vote up 3 votes
/**
 * Throws exception if type is not correct.
 *
 * @param errorCode Error code
 * @param subErrCode Sub Error Code
 * @param type erroneous type
 * @throws BgpParseException for erroneous type
 */
public static void validateType(byte errorCode, byte subErrCode, int type) throws BgpParseException {
    byte[] errType = Ints.toByteArray(type);
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    buffer.writeBytes(errType);
    throw new BgpParseException(errorCode, subErrCode, buffer);
}