sun.nio.ch.DirectBuffer Java Examples

The following examples show how to use sun.nio.ch.DirectBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SctpChannelImpl.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: TestDirectByteBufferPool.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testAllocateSign() {
    int size = 256;
    int pageSize = size * 4;
    int allocTimes = 9;
    DirectByteBufferPool pool = new DirectByteBufferPool(pageSize, (short) 256, (short) 2,0);
    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);
}
 
Example #3
Source File: DirectByteBufferPool.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
public void recycle(ByteBuffer theBuf) {
    if (!(theBuf instanceof DirectBuffer)) {
        theBuf.clear();
        return;
    }

    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 (ByteBufferPage allPage : allPages) {
        if ((recycled = allPage.recycleBuffer((ByteBuffer) parentBuf, startChunk, chunkCount))) {
            break;
        }
    }
    if (!recycled) {
        LOGGER.info("warning ,not recycled buffer " + theBuf);
    }
}
 
Example #4
Source File: SctpChannelImpl.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #5
Source File: Adler32.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: P11Mac.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #7
Source File: SctpMultiChannelImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #8
Source File: SctpMultiChannelImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #9
Source File: Adler32.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #10
Source File: P11Mac.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    } catch (PKCS11Exception e) {
        throw new ProviderException("update() failed", e);
    }
}
 
Example #11
Source File: CRC32.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #12
Source File: SctpChannelImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #13
Source File: Adler32.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #14
Source File: SctpMultiChannelImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #15
Source File: CRC32.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #16
Source File: P11Mac.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #17
Source File: SctpChannelImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #18
Source File: P11Mac.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #19
Source File: UnsafeAccess.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * 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 = (long) 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);
}
 
Example #20
Source File: ByteBufferPage.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 回收buffer
 * @param parent      当前要释放的buf的parent 
 * @param recycleBuf  当前要释放的recycleBuf
 * @param startChunk
 * @param chunkCount
 * @param relatedThreadId  用于返回当前要释放的recycleBuf关联的线程id
 * @return
 */
public boolean recycleBuffer(ByteBuffer parent, ByteBuffer recycleBuf, int startChunk, int chunkCount,
        StringBuilder relatedThreadId) {

    if (parent == this.buf) {
    	//获取锁 必须获取成功
        while (!this.allocLockStatus.compareAndSet(false, true)) {
            Thread.yield();
        }
        //清空已用状态
        try {
            markChunksUnused(startChunk,chunkCount);
            Long threadId = relationBufferThreadId.remove(((DirectBuffer) recycleBuf).address());
            if (threadId != null) {
                relatedThreadId.append(threadId);
            }
        } finally {
        	//释放锁
            allocLockStatus.set(false);
        }
        return true;
    }
    return false;
}
 
Example #21
Source File: CRC32.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #22
Source File: CRC32.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #23
Source File: CopyMemory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static long getMemory(int n) {
    ByteBuffer b = ByteBuffer.allocateDirect(n);
    if (b instanceof DirectBuffer == false) {
        throw new RuntimeException("Not a direct buffer");
    }
    buffers.add(b); // make sure the buffer does not get GCed
    return ((DirectBuffer)b).address();
}
 
Example #24
Source File: P11Digest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void engineUpdate(ByteBuffer byteBuffer) {
    int len = byteBuffer.remaining();
    if (len <= 0) {
        return;
    }

    if (byteBuffer instanceof DirectBuffer == false) {
        super.engineUpdate(byteBuffer);
        return;
    }

    fetchSession();
    long addr = ((DirectBuffer)byteBuffer).address();
    int ofs = byteBuffer.position();
    try {
        if (state == S_BUFFERED) {
            token.p11.C_DigestInit(session.id(), mechanism);
            state = S_INIT;
        }
        if (bufOfs != 0) {
            token.p11.C_DigestUpdate(session.id(), 0, buffer, 0, bufOfs);
            bufOfs = 0;
        }
        token.p11.C_DigestUpdate(session.id(), addr + ofs, null, 0, len);
        byteBuffer.position(ofs + len);
    } catch (PKCS11Exception e) {
        engineReset();
        throw new ProviderException("update() failed", e);
    }
}
 
Example #25
Source File: TransientStorePool.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void destroy() {
    for (ByteBuffer byteBuffer : availableBuffers) {
        final long address = ((DirectBuffer) byteBuffer).address();
        Pointer pointer = new Pointer(address);
        LibC.INSTANCE.munlock(pointer, new NativeLong(fileSize));
    }
}
 
Example #26
Source File: SctpMultiChannelImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private int receiveIntoNativeBuffer(int fd,
                                    ResultContainer resultContainer,
                                    ByteBuffer bb,
                                    int rem,
                                    int pos)
        throws IOException {
    int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem);
    if (n > 0)
        bb.position(pos + n);
    return n;
}
 
Example #27
Source File: PreloadBufferPool.java    From journalkeeper with Apache License 2.0 5 votes vote down vote up
private void releaseIfDirect(ByteBuffer byteBuffer) {
    if (byteBuffer instanceof DirectBuffer) {
        try {
            Method getCleanerMethod;
            getCleanerMethod = byteBuffer.getClass().getMethod("cleaner");
            getCleanerMethod.setAccessible(true);
            Cleaner cleaner = (Cleaner) getCleanerMethod.invoke(byteBuffer, new Object[0]);
            cleaner.clean();
        } catch (Exception e) {
            logger.warn("Exception: ", e);
        }
    }
}
 
Example #28
Source File: CopyMemory.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static long getMemory(int n) {
    ByteBuffer b = ByteBuffer.allocateDirect(n);
    if (b instanceof DirectBuffer == false) {
        throw new RuntimeException("Not a direct buffer");
    }
    buffers.add(b); // make sure the buffer does not get GCed
    return ((DirectBuffer)b).address();
}
 
Example #29
Source File: UnsafeAccess.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Put a byte value out to the specified BB position in big-endian format.
 * @param buf the byte buffer
 * @param offset position in the buffer
 * @param b byte to write out
 * @return incremented offset
 */
public static int putByte(ByteBuffer buf, int offset, byte b) {
  if (buf.isDirect()) {
    theUnsafe.putByte(((DirectBuffer) buf).address() + offset, b);
  } else {
    theUnsafe.putByte(buf.array(),
        BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, b);
  }
  return offset + 1;
}
 
Example #30
Source File: SctpChannelImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private int send(int fd, ByteBuffer src, MessageInfo messageInfo)
        throws IOException {
    int streamNumber = messageInfo.streamNumber();
    SocketAddress target = messageInfo.address();
    boolean unordered = messageInfo.isUnordered();
    int ppid = messageInfo.payloadProtocolID();

    if (src instanceof DirectBuffer)
        return sendFromNativeBuffer(fd, src, target, streamNumber,
                unordered, ppid);

    /* Substitute a native buffer */
    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim && streamNumber >= 0);

    int rem = (pos <= lim ? lim - pos : 0);
    ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
    try {
        bb.put(src);
        bb.flip();
        /* Do not update src until we see how many bytes were written */
        src.position(pos);

        int n = sendFromNativeBuffer(fd, bb, target, streamNumber,
                unordered, ppid);
        if (n > 0) {
            /* now update src */
            src.position(pos + n);
        }
        return n;
    } finally {
        Util.releaseTemporaryDirectBuffer(bb);
    }
}