本文整理匯總了Java中sun.nio.ch.DirectBuffer類的典型用法代碼示例。如果您正苦於以下問題:Java DirectBuffer類的具體用法?Java DirectBuffer怎麽用?Java DirectBuffer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DirectBuffer類屬於sun.nio.ch包,在下文中一共展示了DirectBuffer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setBytes
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
public void setBytes(long memoryOffset, ByteBuffer buffer)
{
if (buffer == null)
throw new NullPointerException();
else if (buffer.remaining() == 0)
return;
checkBounds(memoryOffset, memoryOffset + buffer.remaining());
if (buffer.hasArray())
{
setBytes(memoryOffset, buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
}
else if (buffer instanceof DirectBuffer)
{
unsafe.copyMemory(((DirectBuffer) buffer).address() + buffer.position(), peer + memoryOffset, buffer.remaining());
}
else
throw new IllegalStateException();
}
示例2: testAllocateSign
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
@Test
public void testAllocateSign() {
int size = 256;
int pageSize = size * 4;
int allocTimes = 9;
DirectByteBufferPool pool = new DirectByteBufferPool(pageSize, (short) 256, (short) 2);
long start = System.currentTimeMillis();
ByteBuffer byteBuffer = null;
List<ByteBuffer> buffs = new ArrayList<ByteBuffer>();
int i = 0;
for (; i < allocTimes; i++) {
byteBuffer = pool.allocate(size);
if (byteBuffer == null || !(byteBuffer instanceof DirectBuffer)) {
break;
}
buffs.add(byteBuffer);
}
for (ByteBuffer buff : buffs) {
pool.recycle(buff);
}
Assert.assertEquals("Should out of memory when i = " + 8, i, 8);
}
示例3: freeByteBuffer
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
public synchronized void freeByteBuffer(ByteBuffer byteBuffer) {
long address = ((DirectBuffer) byteBuffer).address();
int relativeAddress = (int) (address - bufAddress);
int length = byteBuffer.capacity();
int depth = maxOrder - 1 - log2(length / pageSize);
int count = 0;
int i;
for (i = 0; i < depthMap.length; i++) {
if (depthMap[i] == depth) {
if (count == relativeAddress) {
break;
}
count += length;
}
if (depthMap[i] > depth) {
break;
}
}
free(i);
}
示例4: recycle
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
public void recycle(ByteBuffer theBuf) {
if(!(theBuf instanceof DirectBuffer)){
theBuf.clear();
return;
}
final long size = theBuf.capacity();
boolean recycled = false;
DirectBuffer thisNavBuf = (DirectBuffer) theBuf;
int chunkCount = theBuf.capacity() / chunkSize;
DirectBuffer parentBuf = (DirectBuffer) thisNavBuf.attachment();
int startChunk = (int) ((thisNavBuf.address() - parentBuf.address()) / this.chunkSize);
for (int i = 0; i < allPages.length; i++) {
if ((recycled = allPages[i].recycleBuffer((ByteBuffer) parentBuf, startChunk, chunkCount) == true)) {
break;
}
}
final long threadId = Thread.currentThread().getId();
if (memoryUsage.containsKey(threadId)){
memoryUsage.put(threadId,memoryUsage.get(threadId)-size);
}
if (recycled == false) {
LOGGER.warn("warning ,not recycled buffer " + theBuf);
}
}
示例5: testAllocateNullWhenOutOfMemory
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
@Test
public void testAllocateNullWhenOutOfMemory() {
int size = 256;
int pageSize = size * 4;
int allocTimes = 9;
DirectByteBufferPool pool = new DirectByteBufferPool(pageSize, (short) 256, (short) 2);
long start = System.currentTimeMillis();
ByteBuffer byteBuffer = null;
List<ByteBuffer> buffs = new ArrayList<ByteBuffer>();
int i = 0;
for (; i < allocTimes; i++) {
byteBuffer = pool.allocate(size);
if (byteBuffer == null || !(byteBuffer instanceof DirectBuffer)) {
break;
}
buffs.add(byteBuffer);
}
for (ByteBuffer buff : buffs) {
pool.recycle(buff);
}
Assert.assertEquals("Should out of memory when i = " + 8, i, 8);
}
示例6: update
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
/**
* Updates the checksum with the bytes from the specified buffer.
*
* The checksum is updated using
* buffer.{@link java.nio.Buffer#remaining() remaining()}
* bytes starting at
* buffer.{@link java.nio.Buffer#position() position()}
* Upon return, the buffer's position will be updated to its
* limit; its limit will not have been changed.
*
* @param buffer the ByteBuffer to update the checksum with
* @since 1.8
*/
public void update(ByteBuffer buffer) {
int pos = buffer.position();
int limit = buffer.limit();
assert (pos <= limit);
int rem = limit - pos;
if (rem <= 0)
return;
if (buffer instanceof DirectBuffer) {
adler = updateByteBuffer(adler, ((DirectBuffer)buffer).address(), pos, rem);
} else if (buffer.hasArray()) {
adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem);
} else {
byte[] b = new byte[rem];
buffer.get(b);
adler = updateBytes(adler, b, 0, b.length);
}
buffer.position(limit);
}
示例7: engineUpdate
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
protected void engineUpdate(ByteBuffer byteBuffer) {
try {
ensureInitialized();
int len = byteBuffer.remaining();
if (len <= 0) {
return;
}
if (byteBuffer instanceof DirectBuffer == false) {
super.engineUpdate(byteBuffer);
return;
}
long addr = ((DirectBuffer)byteBuffer).address();
int ofs = byteBuffer.position();
token.p11.C_SignUpdate(session.id(), addr + ofs, null, 0, len);
byteBuffer.position(ofs + len);
state = S_UPDATE;
} catch (PKCS11Exception e) {
throw new ProviderException("update() failed", e);
}
}
示例8: receive
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
private int receive(int fd,
ByteBuffer dst,
ResultContainer resultContainer)
throws IOException {
int pos = dst.position();
int lim = dst.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (dst instanceof DirectBuffer && rem > 0)
return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos);
/* Substitute a native buffer. */
int newSize = Math.max(rem, 1);
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
try {
int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0);
bb.flip();
if (n > 0 && rem > 0)
dst.put(bb);
return n;
} finally {
Util.releaseTemporaryDirectBuffer(bb);
}
}
示例9: sendFromNativeBuffer
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
private int sendFromNativeBuffer(int fd,
ByteBuffer bb,
SocketAddress target,
int assocId,
int streamNumber,
boolean unordered,
int ppid)
throws IOException {
InetAddress addr = null; // no preferred address
int port = 0;
if (target != null) {
InetSocketAddress isa = Net.checkAddress(target);
addr = isa.getAddress();
port = isa.getPort();
}
int pos = bb.position();
int lim = bb.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr,
port, assocId, streamNumber, unordered, ppid);
if (written > 0)
bb.position(pos + written);
return written;
}
示例10: receive
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
private int receive(int fd,
ByteBuffer dst,
ResultContainer resultContainer,
boolean peek)
throws IOException {
int pos = dst.position();
int lim = dst.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (dst instanceof DirectBuffer && rem > 0)
return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos, peek);
/* Substitute a native buffer */
int newSize = Math.max(rem, 1);
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
try {
int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0, peek);
bb.flip();
if (n > 0 && rem > 0)
dst.put(bb);
return n;
} finally {
Util.releaseTemporaryDirectBuffer(bb);
}
}
示例11: sendFromNativeBuffer
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
private int sendFromNativeBuffer(int fd,
ByteBuffer bb,
SocketAddress target,
int streamNumber,
boolean unordered,
int ppid)
throws IOException {
InetAddress addr = null; // no preferred address
int port = 0;
if (target != null) {
InetSocketAddress isa = Net.checkAddress(target);
addr = isa.getAddress();
port = isa.getPort();
}
int pos = bb.position();
int lim = bb.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr,
port, -1 /*121*/, streamNumber, unordered, ppid);
if (written > 0)
bb.position(pos + written);
return written;
}
示例12: copy
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
/**
* Copies specified number of bytes from given offset of {@code src} buffer into the {@code dest}
* buffer.
*
* @param src
* @param srcOffset
* @param dest
* @param destOffset
* @param length
*/
public static void copy(ByteBuffer src, int srcOffset, ByteBuffer dest, int destOffset,
int length) {
long srcAddress, destAddress;
Object srcBase = null, destBase = null;
if (src.isDirect()) {
srcAddress = srcOffset + ((DirectBuffer) src).address();
} else {
srcAddress = srcOffset + src.arrayOffset() + BYTE_ARRAY_BASE_OFFSET;
srcBase = src.array();
}
if (dest.isDirect()) {
destAddress = destOffset + ((DirectBuffer) dest).address();
} else {
destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET + dest.arrayOffset();
destBase = dest.array();
}
unsafeCopy(srcBase, srcAddress, destBase, destAddress, length);
}
示例13: update
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
/**
* Updates the checksum with the bytes from the specified buffer.
*
* The checksum is updated using
* buffer.{@link java.nio.Buffer#remaining() remaining()}
* bytes starting at
* buffer.{@link java.nio.Buffer#position() position()}
* Upon return, the buffer's position will
* be updated to its limit; its limit will not have been changed.
*
* @param buffer the ByteBuffer to update the checksum with
* @since 1.8
*/
public void update(ByteBuffer buffer) {
int pos = buffer.position();
int limit = buffer.limit();
assert (pos <= limit);
int rem = limit - pos;
if (rem <= 0)
return;
if (buffer instanceof DirectBuffer) {
crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
} else if (buffer.hasArray()) {
crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
} else {
byte[] b = new byte[rem];
buffer.get(b);
crc = updateBytes(crc, b, 0, b.length);
}
buffer.position(limit);
}
示例14: engineUpdate
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
@Override
protected synchronized void engineUpdate(ByteBuffer in) {
if (in == null || in.remaining() == 0) return;
if (in instanceof DirectBuffer == false) {
// cannot do better than default impl
super.engineUpdate(in);
return;
}
long inAddr = ((DirectBuffer)in).address();
int inOfs = in.position();
int inLen = in.remaining();
int rv = update((inAddr + inOfs), inLen);
if (rv < 0) {
throw new UcryptoException(-rv);
}
in.position(inOfs + inLen);
}
示例15: update
import sun.nio.ch.DirectBuffer; //導入依賴的package包/類
/**
* Updates the checksum with the bytes from the specified buffer.
*
* The checksum is updated with the remaining bytes in the buffer, starting
* at the buffer's position. Upon return, the buffer's position will be
* updated to its limit; its limit will not have been changed.
*
* @since 1.8
*/
@Override
public void update(ByteBuffer buffer) {
int pos = buffer.position();
int limit = buffer.limit();
assert (pos <= limit);
int rem = limit - pos;
if (rem <= 0)
return;
if (buffer instanceof DirectBuffer) {
adler = updateByteBuffer(adler, ((DirectBuffer)buffer).address(), pos, rem);
} else if (buffer.hasArray()) {
adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem);
} else {
byte[] b = new byte[Math.min(buffer.remaining(), 4096)];
while (buffer.hasRemaining()) {
int length = Math.min(buffer.remaining(), b.length);
buffer.get(b, 0, length);
update(b, 0, length);
}
}
buffer.position(limit);
}