Java Code Examples for com.google.common.primitives.Ints.BYTES

The following are Jave code examples for showing how to use BYTES 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   File: TestPacketReceiver.java   View Source Code Vote up 5 votes
private byte[] prepareFakePacket(byte[] data, byte[] sums) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(baos);
  
  int packetLen = data.length + sums.length + Ints.BYTES;
  PacketHeader header = new PacketHeader(
      packetLen, OFFSET_IN_BLOCK, SEQNO, false, data.length, false);
  header.write(dos);
  
  dos.write(sums);
  dos.write(data);
  dos.flush();
  return baos.toByteArray();
}
 
Example 2
Project: yuvi   File: BitStream.java   View Source Code Vote up 4 votes
public int getSerializedByteSize() {
  return Ints.BYTES  // Size of index.
      + Byte.BYTES +  // Size of shift
      Long.BYTES * getLastDataIndex();  // Size of long valid data
}
 
Example 3
Project: hadoop   File: PacketReceiver.java   View Source Code Vote up 4 votes
private void doRead(ReadableByteChannel ch, InputStream in)
    throws IOException {
  // Each packet looks like:
  //   PLEN    HLEN      HEADER     CHECKSUMS  DATA
  //   32-bit  16-bit   <protobuf>  <variable length>
  //
  // PLEN:      Payload length
  //            = length(PLEN) + length(CHECKSUMS) + length(DATA)
  //            This length includes its own encoded length in
  //            the sum for historical reasons.
  //
  // HLEN:      Header length
  //            = length(HEADER)
  //
  // HEADER:    the actual packet header fields, encoded in protobuf
  // CHECKSUMS: the crcs for the data chunk. May be missing if
  //            checksums were not requested
  // DATA       the actual block data
  Preconditions.checkState(curHeader == null || !curHeader.isLastPacketInBlock());

  curPacketBuf.clear();
  curPacketBuf.limit(PacketHeader.PKT_LENGTHS_LEN);
  doReadFully(ch, in, curPacketBuf);
  curPacketBuf.flip();
  int payloadLen = curPacketBuf.getInt();
  
  if (payloadLen < Ints.BYTES) {
    // The "payload length" includes its own length. Therefore it
    // should never be less than 4 bytes
    throw new IOException("Invalid payload length " +
        payloadLen);
  }
  int dataPlusChecksumLen = payloadLen - Ints.BYTES;
  int headerLen = curPacketBuf.getShort();
  if (headerLen < 0) {
    throw new IOException("Invalid header length " + headerLen);
  }
  
  if (LOG.isTraceEnabled()) {
    LOG.trace("readNextPacket: dataPlusChecksumLen = " + dataPlusChecksumLen +
        " headerLen = " + headerLen);
  }
  
  // Sanity check the buffer size so we don't allocate too much memory
  // and OOME.
  int totalLen = payloadLen + headerLen;
  if (totalLen < 0 || totalLen > MAX_PACKET_SIZE) {
    throw new IOException("Incorrect value for packet payload size: " +
                          payloadLen);
  }

  // Make sure we have space for the whole packet, and
  // read it.
  reallocPacketBuf(PacketHeader.PKT_LENGTHS_LEN +
      dataPlusChecksumLen + headerLen);
  curPacketBuf.clear();
  curPacketBuf.position(PacketHeader.PKT_LENGTHS_LEN);
  curPacketBuf.limit(PacketHeader.PKT_LENGTHS_LEN +
      dataPlusChecksumLen + headerLen);
  doReadFully(ch, in, curPacketBuf);
  curPacketBuf.flip();
  curPacketBuf.position(PacketHeader.PKT_LENGTHS_LEN);

  // Extract the header from the front of the buffer (after the length prefixes)
  byte[] headerBuf = new byte[headerLen];
  curPacketBuf.get(headerBuf);
  if (curHeader == null) {
    curHeader = new PacketHeader();
  }
  curHeader.setFieldsFromData(payloadLen, headerBuf);
  
  // Compute the sub-slices of the packet
  int checksumLen = dataPlusChecksumLen - curHeader.getDataLen();
  if (checksumLen < 0) {
    throw new IOException("Invalid packet: data length in packet header " + 
        "exceeds data length received. dataPlusChecksumLen=" +
        dataPlusChecksumLen + " header: " + curHeader); 
  }
  
  reslicePacket(headerLen, checksumLen, curHeader.getDataLen());
}