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

The following examples show how to use java.nio.ByteBuffer#asLongBuffer() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: java-ipv6   File: IPv6Address.java    License: 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
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 Project: nd4j   File: BaseDataBuffer.java    License: 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 Project: icafe   File: ArrayUtils.java    License: 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 Project: t-digest   File: FloatHistogram.java    License: 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 Project: mt-flume   File: FlumeEventQueue.java    License: 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 7
Source Project: es6draft   File: TypedArrayFunctions.java    License: 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 8
Source Project: metrics   File: LongJustCopy.java    License: 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 9
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 10
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 11
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;
}
 
Example 12
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
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
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 15
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
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 Project: Bytecoder   File: PerfCounter.java    License: 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 18
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 19
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 Project: hottub   File: PerfCounter.java    License: 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();
}