Java Code Examples for java.nio.ByteBuffer#asLongBuffer()

The following examples show how to use java.nio.ByteBuffer#asLongBuffer() . 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: IPv6Address.java    From java-ipv6 with Apache License 2.0 6 votes vote down vote up
/**
 * Create an IPv6 address from a byte array.
 *
 * @param bytes byte array with 16 bytes (interpreted unsigned)
 * @return IPv6 address
 */
public static IPv6Address fromByteArray(final byte[] bytes)
{
    if (bytes == null)
        throw new IllegalArgumentException("can not construct from [null]");
    if (bytes.length != N_BYTES)
        throw new IllegalArgumentException("the byte array to construct from should be 16 bytes long");

    ByteBuffer buf = ByteBuffer.allocate(N_BYTES);
    for (byte b : bytes)
    {
        buf.put(b);
    }

    buf.rewind();
    LongBuffer longBuffer = buf.asLongBuffer();
    return new IPv6Address(longBuffer.get(), longBuffer.get());
}
 
Example 2
Source File: ArrayUtils.java    From pixymeta-android with Eclipse Public License 1.0 6 votes vote down vote up
public static byte[] toByteArray(long[] data, boolean bigEndian) {
	
	ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 8);
	
	if (bigEndian) {
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
	} else {
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	}
       
	LongBuffer longBuffer = byteBuffer.asLongBuffer();
       longBuffer.put(data);

       byte[] array = byteBuffer.array();

	return array;
}
 
Example 3
Source File: BaseDataBuffer.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Create a data buffer from
 * the given length
 *
 * @param buffer
 * @param length
 */
public BaseDataBuffer(ByteBuffer buffer, long length) {
    if (length < 1)
        throw new IllegalArgumentException("Length must be >= 1");
    initTypeAndSize();

    this.length = length;
    allocationMode = AllocUtil.getAllocationModeFromContext();

    if (dataType() == Type.DOUBLE) {
        pointer = new DoublePointer(buffer.asDoubleBuffer());
        setIndexer(DoubleIndexer.create((DoublePointer) pointer));
    } else if (dataType() == Type.FLOAT) {
        pointer = new FloatPointer(buffer.asFloatBuffer());
        setIndexer(FloatIndexer.create((FloatPointer) pointer));
    } else if (dataType() == Type.INT) {
        pointer = new IntPointer(buffer.asIntBuffer());
        setIndexer(IntIndexer.create((IntPointer) pointer));
    } else if (dataType() == Type.LONG) {
        pointer = new LongPointer(buffer.asLongBuffer());
        setIndexer(LongIndexer.create((LongPointer) pointer));
    }

    // log.info("Creating new buffer of size: {}; dtype: {}; D", length, dataType());
}
 
Example 4
Source File: ArrayUtils.java    From icafe with Eclipse Public License 1.0 6 votes vote down vote up
public static int[] to32BitsLongArray(byte[] data, boolean bigEndian) {
	ByteBuffer byteBuffer = ByteBuffer.wrap(data);
	
	if (bigEndian) {
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
	} else {
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	}
	
	LongBuffer longBuf = byteBuffer.asLongBuffer();
	long[] array = new long[longBuf.remaining()];
	longBuf.get(array);
	
	int[] iArray = new int[array.length];
	
	int i = 0;
	
	for(long l : array) {
		iArray[i++] = (int)l;
	}
	
	return iArray;
}
 
Example 5
Source File: FloatHistogram.java    From t-digest with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("WeakerAccess")
public void writeObject(java.io.ObjectOutputStream out) throws IOException {
    out.writeDouble(min);
    out.writeDouble(max);
    out.writeByte(bitsOfPrecision);
    out.writeByte(shift);

    ByteBuffer buf = ByteBuffer.allocate(8 * counts.length);
    LongBuffer longBuffer = buf.asLongBuffer();
    Simple64.compress(longBuffer, counts, 0, counts.length);
    buf.position(8 * longBuffer.position());
    byte[] r = new byte[buf.position()];
    out.writeShort(buf.position());
    buf.flip();
    buf.get(r);
    out.write(r);
}
 
Example 6
Source File: TypedArrayFunctions.java    From es6draft with MIT License 5 votes vote down vote up
private static final LongBuffer asLongBuffer(TypedArrayObject typedArray) {
    ByteBuffer data = byteBuffer(typedArray);
    int byteOffset = byteOffset(typedArray);
    int byteLength = byteLength(typedArray);

    data.limit(byteOffset + byteLength).position(byteOffset);
    LongBuffer view = data.asLongBuffer();
    data.clear();
    return view;
}
 
Example 7
Source File: LongJustCopy.java    From metrics with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] compress(long[] src) {
    ByteBuffer outbuf = ByteBuffer.allocate(src.length * 8);
    LongBuffer midbuf = outbuf.asLongBuffer();
    midbuf.put(src);
    return outbuf.array();
}
 
Example 8
Source File: StorageUtilities.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void controlInfoToByteArray(final byte[] data, final int offset,
                                          final byte controlByte, final long position,
                                          final int length) {

   data[offset] = controlByte;
   final ByteBuffer bBuffer = ByteBuffer.wrap(data, offset + 1, StorageConstants.STORAGE_CELL_MARKER_SIZE);
   final LongBuffer lBuffer = bBuffer.asLongBuffer();
   lBuffer.put(0, position);
   final ByteBuffer wBuffer = ByteBuffer.wrap(data, offset + StorageConstants.STORAGE_CELL_HEADER_SIZE,
           StorageConstants.STORAGE_DATA_LENGTH_SIZE);
   wBuffer.putInt(length);
}
 
Example 9
Source File: FlumeEventQueue.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
/**
 * Read the inflights file and return a
 * {@link com.google.common.collect.SetMultimap}
 * of transactionIDs to events that were inflight.
 *
 * @return - map of inflight events per txnID.
 *
 */
public SetMultimap<Long, Long> deserialize()
        throws IOException, BadCheckpointException {
  SetMultimap<Long, Long> inflights = HashMultimap.create();
  if (!fileChannel.isOpen()) {
    file = new RandomAccessFile(inflightEventsFile, "rw");
    fileChannel = file.getChannel();
  }
  if(file.length() == 0) {
    return inflights;
  }
  file.seek(0);
  byte[] checksum = new byte[16];
  file.read(checksum);
  ByteBuffer buffer = ByteBuffer.allocate(
          (int)(file.length() - file.getFilePointer()));
  fileChannel.read(buffer);
  byte[] fileChecksum = digest.digest(buffer.array());
  if (!Arrays.equals(checksum, fileChecksum)) {
    throw new BadCheckpointException("Checksum of inflights file differs"
            + " from the checksum expected.");
  }
  buffer.position(0);
  LongBuffer longBuffer = buffer.asLongBuffer();
  try {
    while (true) {
      long txnID = longBuffer.get();
      int numEvents = (int)(longBuffer.get());
      for(int i = 0; i < numEvents; i++) {
        long val = longBuffer.get();
        inflights.put(txnID, val);
      }
    }
  } catch (BufferUnderflowException ex) {
    LOG.debug("Reached end of inflights buffer. Long buffer position ="
            + String.valueOf(longBuffer.position()));
  }
  return  inflights;
}
 
Example 10
Source File: SerializationUtils.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public static long[] toLongsArray(final byte[] bytes, final int offset, final int length, final int padding) {

        final int longLength = requiredLongArraySize(length, padding);
        long[] longArray = new long[longLength];
        //log.debug("byte[{}]={}", bytes.length, bytes);
        final ByteBuffer allocate = ByteBuffer.allocate(longLength * 8 * 2);
        final LongBuffer longBuffer = allocate.asLongBuffer();
        allocate.put(bytes, offset, length);
        longBuffer.get(longArray);
        return longArray;
    }
 
Example 11
Source File: PerfCounter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 12
Source File: PerfCounter.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 13
Source File: PerfCounter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 14
Source File: StorageUtilities.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static long byteArrayToLong(final byte[] buffer, final int offset) {

      final ByteBuffer bBuffer = ByteBuffer.wrap(buffer, offset, 8);
      final LongBuffer lBuffer = bBuffer.asLongBuffer();
      return lBuffer.get(0);
   }
 
Example 15
Source File: PerfCounter.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 16
Source File: PerfCounter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 17
Source File: PerfCounter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 18
Source File: PerfCounter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 19
Source File: PerfCounter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private PerfCounter(String name, int type) {
    this.name = name;
    ByteBuffer bb = perf.createLong(name, type, U_None, 0L);
    bb.order(ByteOrder.nativeOrder());
    this.lb = bb.asLongBuffer();
}
 
Example 20
Source File: ArrayLong.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ByteBuffer getDataAsByteBuffer(ByteOrder order) {
  ByteBuffer bb = super.getDataAsByteBuffer((int) (8 * getSize()), order);
  LongBuffer ib = bb.asLongBuffer();
  ib.put((long[]) get1DJavaArray(getDataType())); // make sure its in canonical order
  return bb;
}