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

The following examples show how to use com.google.common.primitives.Ints#BYTES . 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: TestUnsafeSliceFactory.java    From slice with Apache License 2.0 6 votes vote down vote up
@Test
public void testRawAddress()
        throws Exception
{
    UnsafeSliceFactory factory = UnsafeSliceFactory.getInstance();

    int size = 100;
    long address = unsafe.allocateMemory(size);
    try {
        Slice slice = factory.newSlice(address, size);
        for (int i = 0; i < size; i += Ints.BYTES) {
            slice.setInt(i, i);
        }
        for (int i = 0; i < size; i += Ints.BYTES) {
            assertEquals(slice.getInt(i), i);
        }
    }
    finally {
        unsafe.freeMemory(address);
    }
}
 
Example 2
Source File: ImmutableNode.java    From bytebuffer-collections with Apache License 2.0 6 votes vote down vote up
public ImmutableNode(
    int numDims,
    int initialOffset,
    int offsetFromInitial,
    ByteBuffer data,
    BitmapFactory bitmapFactory
)
{
  this.bitmapFactory = bitmapFactory;
  this.numDims = numDims;
  this.initialOffset = initialOffset;
  this.offsetFromInitial = offsetFromInitial;
  short header = data.getShort(initialOffset + offsetFromInitial);
  this.isLeaf = (header & 0x8000) != 0;
  this.numChildren = (short) (header & 0x7FFF);
  final int sizePosition = initialOffset + offsetFromInitial + HEADER_NUM_BYTES + 2 * numDims * Floats.BYTES;
  int bitmapSize = data.getInt(sizePosition);
  this.childrenOffset = initialOffset
                        + offsetFromInitial
                        + HEADER_NUM_BYTES
                        + 2 * numDims * Floats.BYTES
                        + Ints.BYTES
                        + bitmapSize;

  this.data = data;
}
 
Example 3
Source File: MemcachedCache.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
protected byte[] encodeValue(byte[] key, byte[] valueB) {
    byte[] compressed = null;
    if (config.isEnableCompression() && (valueB.length + Ints.BYTES + key.length > compressThreshold)) {
        try {
            compressed = CompressionUtils.compress(ByteBuffer.allocate(Ints.BYTES + key.length + valueB.length)
                    .putInt(key.length).put(key).put(valueB).array());
        } catch (IOException e) {
            compressed = null;
            logger.warn("Compressing value bytes error.", e);
        }
    }
    if (compressed != null) {
        return ByteBuffer.allocate(Shorts.BYTES + compressed.length).putShort((short) 1).put(compressed).array();
    } else {
        return ByteBuffer.allocate(Shorts.BYTES + Ints.BYTES + key.length + valueB.length).putShort((short) 0)
                .putInt(key.length).put(key).put(valueB).array();
    }
}
 
Example 4
Source File: DS1.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void readPaths(InputStream in) throws IOException {
  if (version >= 14 && in.available() >= Ints.BYTES) {
    numPaths = EndianUtils.readSwappedInteger(in);
    paths = numPaths == 0 ? Path.EMPTY_PATH_ARRAY : new Path[numPaths];
    for (int i = 0; i < numPaths; i++) {
      Path path = paths[i] = new Path().read(version, objects, in);
      if (DEBUG_PATHS) Gdx.app.debug(TAG, path.toString());
    }
  } else {
    numPaths = 0;
    paths = Path.EMPTY_PATH_ARRAY;
  }
}
 
Example 5
Source File: ImmutableRTree.java    From bytebuffer-collections with Apache License 2.0 5 votes vote down vote up
public ImmutableRTree(ByteBuffer data, BitmapFactory bitmapFactory)
{
  final int initPosition = data.position();
  Preconditions.checkArgument(data.get(0) == VERSION, "Mismatching versions");
  this.numDims = data.getInt(1 + initPosition) & 0x7FFF;
  this.data = data;
  this.root = new ImmutableNode(numDims, initPosition, 1 + Ints.BYTES, data, bitmapFactory);
}
 
Example 6
Source File: ImmutableRTree.java    From bytebuffer-collections with Apache License 2.0 5 votes vote down vote up
private static int calcNumBytes(RTree tree)
{
  int total = 1 + Ints.BYTES; // VERSION and numDims

  total += calcNodeBytes(tree.getRoot());

  return total;
}
 
Example 7
Source File: Node.java    From bytebuffer-collections with Apache License 2.0 5 votes vote down vote up
public int getSizeInBytes()
{
  return ImmutableNode.HEADER_NUM_BYTES
         + 2 * getNumDims() * Floats.BYTES
         + Ints.BYTES // size of the set
         + bitmap.getSizeInBytes()
         + getChildren().size() * Ints.BYTES;
}
 
Example 8
Source File: EnumInt32FlagsUtil.java    From mongowp with Apache License 2.0 5 votes vote down vote up
private static <T extends Enum<T> & EnumBitFlags> int getInt32FlagsArray(
    @Nullable Collection<T> flags) {
  if (null == flags || flags.isEmpty()) {
    return 0;
  }

  BitSet bitSet = new BitSet(Ints.BYTES);
  for (T t : flags) {
    bitSet.set(t.getFlagBitPosition());
  }

  return convert(bitSet);
}
 
Example 9
Source File: ImmutableNode.java    From bytebuffer-collections with Apache License 2.0 5 votes vote down vote up
public ImmutableNode(
    int numDims,
    int initialOffset,
    int offsetFromInitial,
    short numChildren,
    boolean leaf,
    ByteBuffer data,
    BitmapFactory bitmapFactory
)
{
  this.bitmapFactory = bitmapFactory;
  this.numDims = numDims;
  this.initialOffset = initialOffset;
  this.offsetFromInitial = offsetFromInitial;
  this.numChildren = numChildren;
  this.isLeaf = leaf;
  final int sizePosition = initialOffset + offsetFromInitial + HEADER_NUM_BYTES + 2 * numDims * Floats.BYTES;
  int bitmapSize = data.getInt(sizePosition);
  this.childrenOffset = initialOffset
                        + offsetFromInitial
                        + HEADER_NUM_BYTES
                        + 2 * numDims * Floats.BYTES
                        + Ints.BYTES
                        + bitmapSize;

  this.data = data;
}
 
Example 10
Source File: DS1.java    From riiablo with Apache License 2.0 5 votes vote down vote up
Path read(int version, Object[] objects, InputStream in) throws IOException {
  numPoints = EndianUtils.readSwappedInteger(in);
  points    = new Point[numPoints];
  x         = EndianUtils.readSwappedInteger(in);
  y         = EndianUtils.readSwappedInteger(in);

  Object object = null;
  for (int i = 0; i < objects.length; i++) {
    Object tmp = objects[i];
    if (tmp == null) continue;
    if (tmp.x != x || tmp.y != y) continue;
    if (object != null) Gdx.app.error(TAG, "More than one object is located at path position: " + this);
    object = tmp;
  }

  if (object == null) {
    Gdx.app.error(TAG, "No object associated with path: " + this);
    int skip = (version >= 15 ? 3 : 2) * Ints.BYTES;
    for (int p = 0; p < numPoints; p++) {
      in.skip(skip);
    }

    return this;
  }

  for (int p = 0; p < numPoints; p++) points[p] = new Point().read(version, in);
  object.path = this;
  return this;
}
 
Example 11
Source File: ResultSetUtil.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private static Object convertByteArrayValue(final Object value, final Class<?> convertType) {
    byte[] bytesValue = (byte[]) value;
    switch (bytesValue.length) {
        case 1:
            return convertNumberValue(bytesValue[0], convertType);
        case Shorts.BYTES:
            return convertNumberValue(Shorts.fromByteArray(bytesValue), convertType);
        case Ints.BYTES:
            return convertNumberValue(Ints.fromByteArray(bytesValue), convertType);
        case Longs.BYTES:
            return convertNumberValue(Longs.fromByteArray(bytesValue), convertType);
        default:
            return value;
    }
}
 
Example 12
Source File: TestPacketReceiver.java    From big-c with Apache License 2.0 5 votes vote down vote up
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 13
Source File: TestPacketReceiver.java    From hadoop with Apache License 2.0 5 votes vote down vote up
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 14
Source File: MemcachedChunkingCache.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
protected static int getValueSplit(MemcachedCacheConfig config, String keyS, int valueBLen) {
    // the number 6 means the chunk number size never exceeds 6 bytes
    final int valueSize = config.getMaxObjectSize() - Shorts.BYTES - Ints.BYTES
            - keyS.getBytes(Charsets.UTF_8).length - 6;
    final int maxValueSize = config.getMaxChunkSize() * valueSize;
    Preconditions.checkArgument(valueBLen <= maxValueSize,
            "the value bytes length [%d] exceeds maximum value size [%d]", valueBLen, maxValueSize);
    return (valueBLen - 1) / valueSize + 1;
}
 
Example 15
Source File: PacketReceiver.java    From big-c with Apache License 2.0 4 votes vote down vote up
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());
}
 
Example 16
Source File: CommitMarkerCodecTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Override
public int getSize() {
  return Ints.BYTES;
}
 
Example 17
Source File: CommitMarkerCodec.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Override
public int getSize() {
  return Ints.BYTES;
}
 
Example 18
Source File: WrappedConciseBitmap.java    From bytebuffer-collections with Apache License 2.0 4 votes vote down vote up
@Override
public int getSizeInBytes()
{
  return bitmap.getWords().length * Ints.BYTES;
}
 
Example 19
Source File: PacketReceiver.java    From hadoop with Apache License 2.0 4 votes vote down vote up
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());
}
 
Example 20
Source File: BitStream.java    From yuvi with Apache License 2.0 4 votes vote down vote up
public int getSerializedByteSize() {
  return Ints.BYTES  // Size of index.
      + Byte.BYTES +  // Size of shift
      Long.BYTES * getLastDataIndex();  // Size of long valid data
}