本文整理汇总了Java中java.nio.ByteBuffer.asLongBuffer方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.asLongBuffer方法的具体用法?Java ByteBuffer.asLongBuffer怎么用?Java ByteBuffer.asLongBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.asLongBuffer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: longToByteArray
import java.nio.ByteBuffer; //导入方法依赖的package包/类
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;
}
示例2: longToByteArrayLittleEndian
import java.nio.ByteBuffer; //导入方法依赖的package包/类
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;
}
示例3: asLongBuffer
import java.nio.ByteBuffer; //导入方法依赖的package包/类
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;
}
示例4: deserialize
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* 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;
}
示例5: testGetLong
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@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) {
}
}
示例6: testGetLongInt
import java.nio.ByteBuffer; //导入方法依赖的package包/类
@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) {
}
}
示例7: asView
import java.nio.ByteBuffer; //导入方法依赖的package包/类
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");
}
示例8: getMetadata
import java.nio.ByteBuffer; //导入方法依赖的package包/类
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;
}
示例9: serializeAndWrite
import java.nio.ByteBuffer; //导入方法依赖的package包/类
/**
* 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;
}
}
示例10: PerfCounter
import java.nio.ByteBuffer; //导入方法依赖的package包/类
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();
}