This class defines four categories of operations upon long buffers:
Absolute and relative {@link #get() get} and{@link #put(long)
put} methods that read and writesingle longs;
Relative {@link #get(long[])
bulk get}methods that transfer contiguous sequences of longs from this buffer into an array; and
Relative {@link #put(long[])
bulk put}methods that transfer contiguous sequences of longs from a long array or some other long buffer into this buffer; and
Methods for {@link #compact
compacting}, {@link #duplicate
duplicating}, and {@link #slice
slicing} a long buffer.
Long buffers can be created either by {@link #allocate
allocation}, which allocates space for the buffer's content, by {@link #wrap(long[])
wrapping} an existinglong array into a buffer, or by creating a view of an existing byte buffer.
Like a byte buffer, a long buffer is either direct or non-direct. A long buffer created via the wrap methods of this class will be non-direct. A long buffer created as a view of a byte buffer will be direct if, and only if, the byte buffer itself is direct. Whether or not a long buffer is direct may be determined by invoking the {@link #isDirect isDirect} method.
Methods in this class that do not otherwise have a value to return are specified to return the buffer upon which they are invoked. This allows method invocations to be chained.
@author Mark Reinhold
@author JSR-51 Expert Group
@version 1.62, 06/07/10
@since 1.4
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*8);
return buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.long_buffer_little : buffers.long_buffer_big;
}
private static LongBuffer doWrap(LongBuffer buffer) {
LongBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.clear();
int saved_position = buffer.position();
direct_buffer.put(buffer);
buffer.position(saved_position);
direct_buffer.flip();
return direct_buffer;
}
ja[i] = iter.getLongNext();
}
public ByteBuffer getDataAsByteBuffer() {
ByteBuffer bb = ByteBuffer.allocate((int)(8*getSize()));
LongBuffer ib = bb.asLongBuffer();
ib.put( (long[]) get1DJavaArray(long.class)); // make sure its in canonical order
return bb;
}
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);
future = Executors.newSingleThreadExecutor().submit(
new Runnable() {
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;
}
ByteArrayInputStream bis = new ByteArrayInputStream(buf.array());
return bis;
} else if (type.getName().equals(CAS.TYPE_NAME_LONG_ARRAY)) {
arrayStart = this.getHeap().heap[getArrayStartAddress(fs.getAddress())];
buf = ByteBuffer.allocate(arraySize * 8);
LongBuffer longbuf = buf.asLongBuffer();
longbuf.put(this.getLongHeap().heap, arrayStart, arraySize);
ByteArrayInputStream bis = new ByteArrayInputStream(buf.array());
return bis;
} else if (type.getName().equals(CAS.TYPE_NAME_DOUBLE_ARRAY)) {
arrayStart = this.getHeap().heap[getArrayStartAddress(fs.getAddress())];
buf = ByteBuffer.allocate(arraySize * 8);
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);
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;
}
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);
future = Executors.newSingleThreadExecutor().submit(
new Runnable() {
if (!Arrays.equals(checksum, fileChecksum)) {
throw new IllegalStateException("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;
}
/** Bulk input of a long array. */
public long[] readLongs (int length) throws KryoException {
if (capacity - position >= length * 8 && isNativeOrder()) {
long[] array = new long[length];
LongBuffer buf = niobuffer.asLongBuffer();
buf.get(array);
position += length * 8;
niobuffer.position(position);
return array;
} else
return super.readLongs(length);
Related Classes of java.nio.LongBuffer
Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.