下面列出了io.netty.util.internal.PlatformDependent#directBufferAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
final void setByteBuffer(ByteBuffer buffer, boolean tryFree) {
if (tryFree) {
ByteBuffer oldBuffer = this.buffer;
if (oldBuffer != null) {
if (doNotFree) {
doNotFree = false;
} else {
// 释放直接缓冲区
freeDirect(oldBuffer);
}
}
}
this.buffer = buffer;
memoryAddress = PlatformDependent.directBufferAddress(buffer);
tmpNioBuf = null;
capacity = buffer.remaining();
}
static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer dst) {
buf.checkIndex(index, dst.remaining());
if (dst.remaining() == 0) {
return;
}
if (dst.isDirect()) {
if (dst.isReadOnly()) {
// We need to check if dst is ready-only so we not write something in it by using Unsafe.我们需要检查dst是否已经就绪,这样我们就不会使用不安全的方法在其中写入内容。
throw new ReadOnlyBufferException();
}
// Copy to direct memory
long dstAddress = PlatformDependent.directBufferAddress(dst);
PlatformDependent.copyMemory(addr, dstAddress + dst.position(), dst.remaining());
dst.position(dst.position() + dst.remaining());
} else if (dst.hasArray()) {
// Copy to array
PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dst.position(), dst.remaining());
dst.position(dst.position() + dst.remaining());
} else {
dst.put(buf.nioBuffer());
}
}
/**
* Try to add the given {@link CompositeByteBuf}. Returns {@code true} on success,
* {@code false} otherwise.
*/
boolean add(CompositeByteBuf buf) {
ByteBuffer[] buffers = buf.nioBuffers();
if (count + buffers.length >= Native.IOV_MAX) {
// No more room!
return false;
}
for (int i = 0; i < buffers.length; i++) {
ByteBuffer nioBuffer = buffers[i];
int offset = nioBuffer.position();
int len = nioBuffer.limit() - nioBuffer.position();
if (len == 0) {
// No need to add an empty buffer so just continue
continue;
}
long addr = PlatformDependent.directBufferAddress(nioBuffer);
add(addr, offset, len);
}
return true;
}
ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer byteBuffer) {
super(allocator, byteBuffer);
// Use buffer as the super class will slice the passed in ByteBuffer which means the memoryAddress
// may be different if the position != 0.//使用buffer作为超类将分割在ByteBuffer中传递的内存地址
// 如果位置!= 0,则可能不同。
memoryAddress = PlatformDependent.directBufferAddress(buffer);
}
private int offsetCacheLine(ByteBuffer memory) {
// We can only calculate the offset if Unsafe is present as otherwise directBufferAddress(...) will
// throw an NPE.//我们只能计算偏移,如果不安全的存在,否则directBufferAddress(…)将
//扔一个NPE。
return HAS_UNSAFE ?
(int) (PlatformDependent.directBufferAddress(memory) & directMemoryCacheAlignmentMask) : 0;
}
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer src) {
buf.checkIndex(index, src.remaining());
int length = src.remaining();
if (length == 0) {
return;
}
if (src.isDirect()) {
// Copy from direct memory
long srcAddress = PlatformDependent.directBufferAddress(src);
PlatformDependent.copyMemory(srcAddress + src.position(), addr, src.remaining());
src.position(src.position() + length);
} else if (src.hasArray()) {
// Copy from array
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + src.position(), addr, length);
src.position(src.position() + length);
} else {
ByteBuf tmpBuf = buf.alloc().heapBuffer(length);
try {
byte[] tmp = tmpBuf.array();
src.get(tmp, tmpBuf.arrayOffset(), length); // moves the src position too
PlatformDependent.copyMemory(tmp, tmpBuf.arrayOffset(), addr, length);
} finally {
tmpBuf.release();
}
}
}
private void setByteBuffer(ByteBuffer buffer) {
ByteBuffer oldBuffer = this.buffer;
if (oldBuffer != null) {
if (doNotFree) {
doNotFree = false;
} else {
freeDirect(oldBuffer);
}
}
this.buffer = buffer;
memoryAddress = PlatformDependent.directBufferAddress(buffer);
tmpNioBuf = null;
capacity = buffer.remaining();
}
private MappedFile(FileChannel channel, MappedByteBuffer byteBuffer, int position, int length) throws IOException {
this.channel = channel;
this.buffer = byteBuffer;
this.position = position;
this.length = length;
this.byteBufWrapper = Unpooled.wrappedBuffer(buffer);
this.channelBufferWrapper = new ChannelBufferWrapper(this.byteBufWrapper, false);
this.address = PlatformDependent.directBufferAddress(buffer);
}
/**
* Writes a sequence of bytes to this file from the given buffer.
* <p>
* <p> Bytes are written starting at this file's current position,
*/
public void write(ByteBuffer src, int srcStart, int srcLength) throws IOException {
final int nextPosition = this.position + srcLength;
checkCapacity(nextPosition);
final long destAddress = this.address + this.position;
if (src.isDirect()) {
final long srcAddress = PlatformDependent.directBufferAddress(src) + srcStart;
PlatformDependent.copyMemory(srcAddress, destAddress, srcLength);
} else {
final byte[] srcArray = src.array();
PlatformDependent.copyMemory(srcArray, srcStart, destAddress, srcLength);
}
rawMovePositionAndLength(nextPosition);
}
private void initMemoryAddress() {
memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
}
ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
super(allocator, buffer);
memoryAddress = PlatformDependent.directBufferAddress(buffer);
}
private void initMemoryAddress() {
memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
}