diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 9ce8ccc5a30e5fb7cfd5c3f751c30f1ee9d43969..6281eb11f22ddffd7ad74b06d333d8e6ef99d76a 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -283,10 +283,15 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + /** + * 确定可写容量+要写容量 < 最大容量 + * @param minWritableBytes + */ final void ensureWritable0(int minWritableBytes) { final int writerIndex = writerIndex(); final int targetCapacity = writerIndex + minWritableBytes; if (targetCapacity <= capacity()) { + //检查buf释放情况,不抛出异常则正常返回 ensureAccessible(); return; } @@ -1069,8 +1074,17 @@ public abstract class AbstractByteBuf extends ByteBuf { return this; } + /** + * 将byte数组写入到buf中 + * @param src + * @param srcIndex the first index of the source + * @param length the number of bytes to transfer + * + * @return + */ @Override public ByteBuf writeBytes(byte[] src, int srcIndex, int length) { + //确定容量 ensureWritable(length); setBytes(writerIndex, src, srcIndex, length); writerIndex += length; @@ -1387,12 +1401,17 @@ public abstract class AbstractByteBuf extends ByteBuf { return ByteBufUtil.compare(this, that); } + /** + * 重写toString,如果引用计数=0,则表示将被释放 + * 展示 readIndex,writeIndex,capacity容量 + * @return + */ @Override public String toString() { if (refCnt() == 0) { return StringUtil.simpleClassName(this) + "(freed)"; } - + // 返回相关容量信息 StringBuilder buf = new StringBuilder() .append(StringUtil.simpleClassName(this)) .append("(ridx: ").append(readerIndex) @@ -1401,7 +1420,7 @@ public abstract class AbstractByteBuf extends ByteBuf { if (maxCapacity != Integer.MAX_VALUE) { buf.append('/').append(maxCapacity); } - + //返回buf的根本类型 ByteBuf unwrapped = unwrap(); if (unwrapped != null) { buf.append(", unwrapped: ").append(unwrapped); diff --git a/buffer/src/main/java/io/netty/buffer/ByteBuf.java b/buffer/src/main/java/io/netty/buffer/ByteBuf.java index 850c85a21ef8d7e203b92d98d72573db8097d0dd..ea39d6e3a7740af9d05b83ce7987b61b9a627c2d 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBuf.java @@ -17,6 +17,7 @@ package io.netty.buffer; import io.netty.util.ByteProcessor; import io.netty.util.ReferenceCounted; +import sun.misc.Unsafe; import java.io.IOException; import java.io.InputStream; @@ -2354,7 +2355,7 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable { /** * Returns the backing byte array of this buffer. - * + * 堆内buf会返回数组对象,直接内存buf将抛出异常 * @throws UnsupportedOperationException * if there no accessible backing byte array */ diff --git a/common/src/main/java/io/netty/util/ReferenceCounted.java b/common/src/main/java/io/netty/util/ReferenceCounted.java index 2e090cf5adf5865553752b8bee2194b4bff7a794..f348bec8804179c04f3361fa76be50202dc93ffc 100644 --- a/common/src/main/java/io/netty/util/ReferenceCounted.java +++ b/common/src/main/java/io/netty/util/ReferenceCounted.java @@ -31,11 +31,13 @@ package io.netty.util; */ public interface ReferenceCounted { /** + * 返回对象的引用计数, 0=已经被释放了 * Returns the reference count of this object. If {@code 0}, it means this object has been deallocated. */ int refCnt(); /** + * 引用计数+1 * Increases the reference count by {@code 1}. */ ReferenceCounted retain(); diff --git a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java index 084c42fc6f98426b8c13148506cb16b5a6dcae9d..76e2a549a3b1459c9f3449085951b55f73f2ed8c 100644 --- a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java @@ -67,6 +67,7 @@ public class ServerBootstrap extends AbstractBootstrap