java BufferSegment
Posted Googler
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java BufferSegment相关的知识,希望对你有一定的参考价值。
package org.rx.util; import java.util.function.Consumer; import static org.rx.core.Contract.require; public class BufferSegment { private final byte[] buffer; private final int bufferSize; private volatile int offset; private volatile boolean autoReleased; public boolean isAutoReleased() { return autoReleased; } public void setAutoReleased(boolean autoReleased) { this.autoReleased = autoReleased; } public BufferSegment(int bufferSize, int bufferCount) { require(bufferSize, bufferSize >= 0); require(bufferCount, bufferCount >= 0); buffer = new byte[(this.bufferSize = bufferSize) * bufferCount]; } public BytesSegment alloc() { if (offset == buffer.length) { return new BytesSegment(new byte[bufferSize]); } synchronized (buffer) { BytesSegment segment = new BytesSegment(buffer, offset, bufferSize); offset += bufferSize; if (autoReleased) { segment.Closed = p -> release(p); } return segment; } } public void release(BytesSegment segment) { require(segment); if (buffer != segment.array) { return; } synchronized (buffer) { offset -= segment.count; } } public void accept(Consumer<BytesSegment> consumer) { require(consumer); try (BytesSegment segment = alloc()) { consumer.accept(segment); } } }
以上是关于java BufferSegment的主要内容,如果未能解决你的问题,请参考以下文章