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
LongBuffer.wrap((long[])null, -1, 0);
fail("Should throw NPE"); //$NON-NLS-1$
} catch (NullPointerException e) {
}
LongBuffer buf = LongBuffer.wrap(array, 2, 16);
assertEquals(buf.position(), 2);
assertEquals(buf.limit(), 18);
assertEquals(buf.capacity(), 20);
}
buf.clear();
buf.order(ByteOrder.BIG_ENDIAN);
}
public void testAsLongBuffer() {
LongBuffer longBuffer;
byte bytes[] = new byte[8];
long value;
// test BIG_ENDIAN long buffer, read
buf.clear();
buf.order(ByteOrder.BIG_ENDIAN);
longBuffer = buf.asLongBuffer();
assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
while (longBuffer.remaining() > 0) {
buf.get(bytes);
value = longBuffer.get();
assertEquals(bytes2long(bytes, buf.order()), value);
}
// test LITTLE_ENDIAN long buffer, read
buf.clear();
buf.order(ByteOrder.LITTLE_ENDIAN);
longBuffer = buf.asLongBuffer();
assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
while (longBuffer.remaining() > 0) {
buf.get(bytes);
value = longBuffer.get();
assertEquals(bytes2long(bytes, buf.order()), value);
}
if (!buf.isReadOnly()) {
// test BIG_ENDIAN long buffer, write
buf.clear();
buf.order(ByteOrder.BIG_ENDIAN);
longBuffer = buf.asLongBuffer();
assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
while (longBuffer.remaining() > 0) {
value = (long) longBuffer.remaining();
longBuffer.put(value);
buf.get(bytes);
assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order())));
}
// test LITTLE_ENDIAN long buffer, write
buf.clear();
buf.order(ByteOrder.LITTLE_ENDIAN);
longBuffer = buf.asLongBuffer();
assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
while (longBuffer.remaining() > 0) {
value = (long) longBuffer.remaining();
longBuffer.put(value);
buf.get(bytes);
assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order())));
}
}
//expected
}
}
public void testHashCode() {
LongBuffer duplicate = buf.duplicate();
assertEquals(buf.hashCode(), duplicate.hashCode());
}
// expected
}
}
public void testPutLongBuffer() {
LongBuffer other = LongBuffer.allocate(1);
try {
buf.put(other);
fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
} catch (ReadOnlyBufferException e) {
// expected
}
public static long getLong(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
LongBuffer lBuffer = byteBuffer.asLongBuffer();
return lBuffer.get();
}
ByteArrayInputStream bis = new ByteArrayInputStream(buf.array());
return bis;
} else if (type.getName().equals(CAS.TYPE_NAME_LONG_ARRAY)) {
arrayStart = this.heap.heap[getArrayStartAddress(fs.getAddress())];
buf = ByteBuffer.allocate(arraySize * 8);
LongBuffer longbuf = buf.asLongBuffer();
longbuf.put(this.longHeap.heap, arrayStart, arraySize);
ByteArrayInputStream bis = new ByteArrayInputStream(buf.array());
return bis;
} else if (type.getName().equals(CAS.TYPE_NAME_DOUBLE_ARRAY)) {
arrayStart = this.heap.heap[getArrayStartAddress(fs.getAddress())];
buf = ByteBuffer.allocate(arraySize * 8);
_max = iin.get();
iin.get(_counts);
in.position(iin.position() * INT_SIZE);
// read our long data
LongBuffer lin = in.asLongBuffer();
_snapTotal = (_total = lin.get());
in.position(iin.position() * INT_SIZE + lin.position() * 2 * INT_SIZE);
// read our min value (which was added afterwards and must do some jockeying to maintain
// backwards compatibility)
if (in.position() == in.limit()) {
_min = 0; // legacy
iout.put(_max);
iout.put(_counts);
out.position(iout.position() * INT_SIZE);
// write our long data
LongBuffer lout = out.asLongBuffer();
lout.put(_total);
out.position(iout.position() * INT_SIZE + lout.position() * 2 * INT_SIZE);
// write our min value (added later so we can't write it above like we wish we could)
out.asIntBuffer().put(_min);
return data;
// ByteBuffer buf = ByteBuffer.wrap(b);
// buf.putLong(l);
// return b;
ByteBuffer buf = ByteBuffer.wrap(b);
LongBuffer lBuffer = buf.asLongBuffer();
lBuffer.put(0, l);
return b;
}
FloatBuffer fBuffer = buffer.asFloatBuffer();
fBuffer.position(1);
System.out.println(Memory.getPosition(fBuffer));
LongBuffer lBuffer = buffer.asLongBuffer();
lBuffer.position(1);
System.out.println(Memory.getPosition(lBuffer));
DoubleBuffer dBuffer = buffer.asDoubleBuffer();
dBuffer.position(1);
System.out.println(Memory.getPosition(dBuffer));
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.