⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pooledbytebufferallocator.java

📁 apache 的一个socket框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            // Expire unused buffers every seconds            while (!timeToStop) {                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    //ignore                }                // Check if expiration is disabled.                long timeout = getTimeoutMillis();                if (timeout <= 0L) {                    continue;                }                // Expire old buffers                long expirationTime = System.currentTimeMillis() - timeout;                for (int i = directBufferStacks.length - 1; i >= 0; i--) {                    ExpiringStack stack = directBufferStacks[i];                    synchronized (stack) {                        stack.expireBefore(expirationTime);                    }                }                for (int i = heapBufferStacks.length - 1; i >= 0; i--) {                    ExpiringStack stack = heapBufferStacks[i];                    synchronized (stack) {                        stack.expireBefore(expirationTime);                    }                }            }        }    }    private class PooledByteBuffer extends BaseByteBuffer {        private UnexpandableByteBuffer buf;        private int refCount = 1;        protected PooledByteBuffer() {        }        public synchronized void init(UnexpandableByteBuffer buf, boolean clear) {            this.buf = buf;            if (clear) {                buf.buf().clear();            }            buf.buf().order(ByteOrder.BIG_ENDIAN);            setAutoExpand(false);            refCount = 1;        }        public synchronized void acquire() {            if (refCount <= 0) {                throw new IllegalStateException("Already released buffer.");            }            refCount++;        }        public void release() {            synchronized (this) {                if (refCount <= 0) {                    refCount = 0;                    throw new IllegalStateException(                            "Already released buffer.  You released the buffer too many times.");                }                refCount--;                if (refCount > 0) {                    return;                }            }            // No need to return buffers to the pool if it is disposed already.            if (disposed) {                return;            }            buf.release();        }        public java.nio.ByteBuffer buf() {            return buf.buf();        }        public boolean isPooled() {            return buf.isPooled();        }        public void setPooled(boolean pooled) {            buf.setPooled(pooled);        }        public ByteBuffer duplicate() {            PooledByteBuffer newBuf = allocateContainer();            newBuf.init(new UnexpandableByteBuffer(buf().duplicate(), buf),                    false);            return newBuf;        }        public ByteBuffer slice() {            PooledByteBuffer newBuf = allocateContainer();            newBuf.init(new UnexpandableByteBuffer(buf().slice(), buf), false);            return newBuf;        }        public ByteBuffer asReadOnlyBuffer() {            PooledByteBuffer newBuf = allocateContainer();            newBuf.init(new UnexpandableByteBuffer(buf().asReadOnlyBuffer(),                    buf), false);            return newBuf;        }        public byte[] array() {            return buf().array();        }        public int arrayOffset() {            return buf().arrayOffset();        }        protected void capacity0(int requestedCapacity) {            if (buf.isDerived()) {                throw new IllegalStateException(                        "Derived buffers cannot be expanded.");            }            int newCapacity = MINIMUM_CAPACITY;            while (newCapacity < requestedCapacity) {                newCapacity <<= 1;            }            UnexpandableByteBuffer oldBuf = this.buf;            boolean direct = isDirect();            UnexpandableByteBuffer newBuf;            try {                newBuf = allocate0(newCapacity, direct);            } catch (OutOfMemoryError e) {                if (direct) {                    newBuf = allocate0(newCapacity, false);                } else {                    throw e;                }            }            newBuf.buf().clear();            oldBuf.buf().clear();            newBuf.buf().put(oldBuf.buf());            this.buf = newBuf;            oldBuf.release();        }    }    private class UnexpandableByteBuffer {        private final java.nio.ByteBuffer buf;        private final UnexpandableByteBuffer parentBuf;        private int refCount;        private boolean pooled;        protected UnexpandableByteBuffer(java.nio.ByteBuffer buf) {            this.buf = buf;            this.parentBuf = null;        }        protected UnexpandableByteBuffer(java.nio.ByteBuffer buf,                UnexpandableByteBuffer parentBuf) {            parentBuf.acquire();            this.buf = buf;            this.parentBuf = parentBuf;        }        public void init() {            refCount = 1;            pooled = true;        }        public synchronized void acquire() {            if (isDerived()) {                parentBuf.acquire();                return;            }            if (refCount <= 0) {                throw new IllegalStateException("Already released buffer.");            }            refCount++;        }        public void release() {            if (isDerived()) {                parentBuf.release();                return;            }            synchronized (this) {                if (refCount <= 0) {                    refCount = 0;                    throw new IllegalStateException(                            "Already released buffer.  You released the buffer too many times.");                }                refCount--;                if (refCount > 0) {                    return;                }            }            // No need to return buffers to the pool if it is disposed already.            if (disposed) {                return;            }            if (pooled) {                if (parentBuf != null) {                    release0(parentBuf);                } else {                    release0(this);                }            }        }        public java.nio.ByteBuffer buf() {            return buf;        }        public boolean isPooled() {            return pooled;        }        public void setPooled(boolean pooled) {            this.pooled = pooled;        }        public boolean isDerived() {            return parentBuf != null;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -