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

The following are Jave code examples for showing how to use asLongBuffer() of the java.nio.ByteBuffer 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: BIMplatform   File: BinUtils.java   View Source Code Vote up 5 votes
public static byte[] longToByteArray(long inLong) {
	byte[] bArray = new byte[8];
	ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
	LongBuffer lBuffer = bBuffer.asLongBuffer();
	lBuffer.put(inLong);
	return bArray;
}
 
Example 2
Project: BIMplatform   File: BinUtils.java   View Source Code Vote up 5 votes
public static byte[] longToByteArrayLittleEndian(long inLong) {
	byte[] bArray = new byte[8];
	ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
	bBuffer.order(ByteOrder.LITTLE_ENDIAN);
	LongBuffer lBuffer = bBuffer.asLongBuffer();
	lBuffer.put(inLong);
	return bArray;
}
 
Example 3
Project: debug   File: RT.java   View Source Code Vote up 5 votes
public static LongBuffer asLongBuffer(ByteBuffer buf) {
    LongBuffer buffer = buf.asLongBuffer();
    Buffer viewedBuffer = bufferViews.get(buf);
    if (viewedBuffer != null) {
        bufferViews.put(buffer, viewedBuffer);
    } else {
        bufferViews.put(buffer, buf);
    }
    return buffer;
}
 
Example 4
Project: flume-release-1.7.0   File: FlumeEventQueue.java   View Source Code Vote up 5 votes
/**
 * 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 5
Project: monarch   File: ByteSourceJUnitTest.java   View Source Code Vote up 5 votes
@Test
public void testGetLong() {
  ByteBuffer bb = ByteBuffer.allocate(40);
  LongBuffer lb = bb.asLongBuffer();
  lb.put(0x1110223344556677L);
  lb.put(0x2220334455667788L);
  lb.put(0x3330445566778899L);
  lb.put(0x4440556677889900L);
  lb.put(0x55506677889900AAL);
  byte[] bytes = bb.array();
  ByteSource bs = createByteSource(bytes);
  long l = bs.getLong();
  assertEquals(0x1110223344556677L, l);
  assertEquals(8, bs.position());
  l = bs.getLong();
  assertEquals(0x2220334455667788L, l);
  assertEquals(16, bs.position());
  bs.position(4 * 8);
  l = bs.getLong();
  assertEquals(0x55506677889900AAL, l);
  assertEquals(40, bs.position());
  try {
    bs.getLong();
    fail("expected BufferUnderflowException");
  } catch (BufferUnderflowException expected) {
  }
}
 
Example 6
Project: monarch   File: ByteSourceJUnitTest.java   View Source Code Vote up 5 votes
@Test
public void testGetLongInt() {
  ByteBuffer bb = ByteBuffer.allocate(40);
  LongBuffer lb = bb.asLongBuffer();
  lb.put(0x1110223344556677L);
  lb.put(0x2220334455667788L);
  lb.put(0x3330445566778899L);
  lb.put(0x4440556677889900L);
  lb.put(0x55506677889900AAL);
  byte[] bytes = bb.array();
  ByteSource bs = createByteSource(bytes);
  bs.position(3);
  long l = bs.getLong(0);
  assertEquals(0x1110223344556677L, l);
  assertEquals(3, bs.position());
  l = bs.getLong(8);
  assertEquals(0x2220334455667788L, l);
  assertEquals(3, bs.position());
  l = bs.getLong(4 * 8);
  assertEquals(0x55506677889900AAL, l);
  assertEquals(3, bs.position());
  try {
    bs.getLong((4 * 8) + 1);
    fail("expected IndexOutOfBoundsException");
  } catch (IndexOutOfBoundsException expected) {
  }
}
 
Example 7
Project: openjdk-jdk10   File: ByteBufferTest.java   View Source Code Vote up 5 votes
Buffer asView(ByteBuffer b, PrimitiveType t) {
    switch (t) {
        case BYTE: return b;
        case CHAR: return b.asCharBuffer();
        case SHORT: return b.asShortBuffer();
        case INT: return b.asIntBuffer();
        case LONG: return b.asLongBuffer();
        case FLOAT: return b.asFloatBuffer();
        case DOUBLE: return b.asDoubleBuffer();
    }
    throw new InternalError("Should not reach here");
}
 
Example 8
Project: incubator-netbeans   File: CacheWriter.java   View Source Code Vote up 4 votes
private ByteBuffer getMetadata (BufferedImage img, String id, long start, long end) throws IOException {
    byte[] bytes = new byte[METAENTRY_LENGTH];
    Arrays.fill(bytes, (byte)'-'); //XXX
    
    ByteBuffer result = ByteBuffer.wrap(bytes);
    result.position(0);
    
    int width = img.getWidth();
    int height = img.getHeight();
    
    id = convertPathSeparators (id);
    
    //First write the id length, width and height as ints
    
    IntBuffer ibuf = result.asIntBuffer();
    ibuf.put(id.length()).put(width).put(height);
    
    result.position(result.position() + (ibuf.position() * 4));
    
    //Then write the start and end positions in the cache file as longs
    
    LongBuffer lbuf = result.asLongBuffer();
    lbuf.put(start).put(end);
    
    result.position(result.position() + (lbuf.position() * 8));
    
    //We are intentionally stripping the high eight bits - unless we start
    //having modules with katakana pathnames, 16 bits clean filenames will
    //not be needed
    
    char[] chars = id.toCharArray();
    if (chars.length + result.position() > METAENTRY_LENGTH) {
        throw new IOException ("ID " + id + " too long.  Limit is " + (METAENTRY_LENGTH - 8));
    }
    
    //Now write the id text
    for (int i=0; i < chars.length; i++) {
        result.put((byte) chars[i]);
    }
    
    result.position(METAENTRY_LENGTH);
    result.flip();
    return result;
}
 
Example 9
Project: flume-release-1.7.0   File: FlumeEventQueue.java   View Source Code Vote up 4 votes
/**
 * Serialize the set of in flights into a byte longBuffer.
 *
 * @return Returns the checksum of the buffer that is being
 * asynchronously written to disk.
 */
public void serializeAndWrite() throws Exception {
  Collection<Long> values = inflightEvents.values();
  if (!fileChannel.isOpen()) {
    file = new RandomAccessFile(inflightEventsFile, "rw");
    fileChannel = file.getChannel();
  }
  if (values.isEmpty()) {
    file.setLength(0L);
  }
  //What is written out?
  //Checksum - 16 bytes
  //and then each key-value pair from the map:
  //transactionid numberofeventsforthistxn listofeventpointers

  try {
    int expectedFileSize = (((inflightEvents.keySet().size() * 2) //for transactionIDs and
                                                                  //events per txn ID
        + values.size()) * 8) //Event pointers
        + 16; //Checksum
    //There is no real need of filling the channel with 0s, since we
    //will write the exact number of bytes as expected file size.
    file.setLength(expectedFileSize);
    Preconditions.checkState(file.length() == expectedFileSize,
        "Expected File size of inflight events file does not match the "
            + "current file size. Checkpoint is incomplete.");
    file.seek(0);
    final ByteBuffer buffer = ByteBuffer.allocate(expectedFileSize);
    LongBuffer longBuffer = buffer.asLongBuffer();
    for (Long txnID : inflightEvents.keySet()) {
      Set<Long> pointers = inflightEvents.get(txnID);
      longBuffer.put(txnID);
      longBuffer.put((long) pointers.size());
      LOG.debug("Number of events inserted into "
          + "inflights file: " + String.valueOf(pointers.size())
          + " file: " + inflightEventsFile.getCanonicalPath());
      long[] written = ArrayUtils.toPrimitive(
          pointers.toArray(new Long[0]));
      longBuffer.put(written);
    }
    byte[] checksum = digest.digest(buffer.array());
    file.write(checksum);
    buffer.position(0);
    fileChannel.write(buffer);
    fileChannel.force(true);
    syncRequired = false;
  } catch (IOException ex) {
    LOG.error("Error while writing checkpoint to disk.", ex);
    throw ex;
  }
}
 
Example 10
Project: OpenJSharp   File: PerfCounter.java   View Source Code Vote up 4 votes
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 11
Project: jdk8u-jdk   File: PerfCounter.java   View Source Code Vote up 4 votes
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
Project: openjdk-jdk10   File: PerfCounter.java   View Source Code Vote up 4 votes
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();
}