forked from KNEMC/KLALB
Changes
This commit is contained in:
@@ -15,10 +15,8 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
private final ByteBuffer readBuffer;
|
||||
private final ByteBuffer writeBuffer;
|
||||
|
||||
private final ReentrantLock readLock = new ReentrantLock();
|
||||
private final ReentrantLock writeLock = new ReentrantLock();
|
||||
|
||||
private final AtomicBoolean closed = new AtomicBoolean(false);
|
||||
// private final AtomicBoolean closed = new AtomicBoolean(false);
|
||||
private final AtomicBoolean inputShutdown = new AtomicBoolean(false);
|
||||
private final AtomicBoolean outputShutdown = new AtomicBoolean(false);
|
||||
|
||||
@@ -43,12 +41,11 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
|
||||
@Override
|
||||
public int read(ByteBuffer dst) throws IOException {
|
||||
if (closed.get() || inputShutdown.get()) {
|
||||
if ( inputShutdown.get()) {
|
||||
throw new ClosedChannelException();
|
||||
}
|
||||
|
||||
readLock.lock();
|
||||
try {
|
||||
|
||||
// 检查底层通道是否仍然打开
|
||||
if (inputChannel != null && !inputChannel.isOpen()) {
|
||||
inputShutdown.set(true);
|
||||
@@ -102,10 +99,7 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
}
|
||||
|
||||
return totalRead;
|
||||
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,9 +120,7 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
|
||||
@Override
|
||||
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
|
||||
if (closed.get() || inputShutdown.get()) {
|
||||
throw new ClosedChannelException();
|
||||
}
|
||||
|
||||
|
||||
long totalRead = 0;
|
||||
for (int i = offset; i < offset + length; i++) {
|
||||
@@ -150,12 +142,9 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
|
||||
@Override
|
||||
public int write(ByteBuffer src) throws IOException {
|
||||
if (closed.get() || outputShutdown.get()) {
|
||||
throw new ClosedChannelException();
|
||||
}
|
||||
|
||||
|
||||
writeLock.lock();
|
||||
try {
|
||||
|
||||
// 检查底层通道是否仍然打开
|
||||
if (outputChannel != null && !outputChannel.isOpen()) {
|
||||
outputShutdown.set(true);
|
||||
@@ -188,9 +177,7 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
}
|
||||
return totalWritten;
|
||||
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -200,9 +187,7 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
|
||||
@Override
|
||||
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
|
||||
if (closed.get() || outputShutdown.get()) {
|
||||
throw new ClosedChannelException();
|
||||
}
|
||||
|
||||
|
||||
long totalWritten = 0;
|
||||
for (int i = offset; i < offset + length; i++) {
|
||||
@@ -222,23 +207,18 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
* 安全的刷新方法
|
||||
*/
|
||||
public void flush() throws IOException {
|
||||
if (closed.get()) {
|
||||
throw new ClosedChannelException();
|
||||
}
|
||||
|
||||
|
||||
if (outputChannel == null || outputShutdown.get()) {
|
||||
return;
|
||||
}
|
||||
|
||||
writeLock.lock();
|
||||
try {
|
||||
flushInternal();
|
||||
} catch (IOException e) {
|
||||
outputShutdown.set(true);
|
||||
throw e;
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -274,16 +254,13 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (!closed.compareAndSet(false, true)) {
|
||||
return; // 已经关闭
|
||||
}
|
||||
|
||||
|
||||
IOException exception = null;
|
||||
|
||||
// 先刷新输出缓冲区
|
||||
if (outputChannel != null && !outputShutdown.get()) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
|
||||
if (writeBuffer.position() > 0) {
|
||||
try {
|
||||
flushInternal();
|
||||
@@ -291,9 +268,7 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
exception = e;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 关闭底层通道
|
||||
@@ -322,20 +297,14 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
}
|
||||
|
||||
// 清理缓冲区状态
|
||||
readLock.lock();
|
||||
try {
|
||||
|
||||
readBuffer.clear();
|
||||
readBuffer.limit(0); // 标记为已清空
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
writeLock.lock();
|
||||
try {
|
||||
|
||||
|
||||
writeBuffer.clear();
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
// 设置关闭状态
|
||||
inputShutdown.set(true);
|
||||
@@ -351,13 +320,10 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
*/
|
||||
public void shutdownInput() throws IOException {
|
||||
inputShutdown.set(true);
|
||||
readLock.lock();
|
||||
try {
|
||||
|
||||
readBuffer.clear();
|
||||
readBuffer.limit(0);
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
if (inputChannel instanceof SocketChannel) {
|
||||
((SocketChannel) inputChannel).shutdownInput();
|
||||
@@ -366,14 +332,11 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
|
||||
public void shutdownOutput() throws IOException {
|
||||
outputShutdown.set(true);
|
||||
writeLock.lock();
|
||||
try {
|
||||
|
||||
if (writeBuffer.position() > 0) {
|
||||
flushInternal();
|
||||
}
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
if (outputChannel instanceof SocketChannel) {
|
||||
((SocketChannel) outputChannel).shutdownOutput();
|
||||
@@ -382,7 +345,7 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return !closed.get() &&
|
||||
return
|
||||
(inputChannel == null || inputChannel.isOpen()) &&
|
||||
(outputChannel == null || outputChannel.isOpen());
|
||||
}
|
||||
@@ -391,51 +354,39 @@ public class BufferedChannel implements ReadableByteChannel, WritableByteChannel
|
||||
* 检查是否还有可读数据(包括缓冲区中的)
|
||||
*/
|
||||
public boolean hasRemaining() throws IOException {
|
||||
if (closed.get() || inputShutdown.get()) {
|
||||
if ( inputShutdown.get()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
readLock.lock();
|
||||
try {
|
||||
|
||||
return readBuffer.hasRemaining() ||
|
||||
(inputChannel != null && inputChannel.isOpen());
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓冲区状态信息(用于调试)
|
||||
*/
|
||||
public String getBufferState() {
|
||||
readLock.lock();
|
||||
writeLock.lock();
|
||||
try {
|
||||
|
||||
return String.format(
|
||||
"ReadBuffer[pos=%d, lim=%d, cap=%d], WriteBuffer[pos=%d, lim=%d, cap=%d], " +
|
||||
"closed=%b, inputShutdown=%b, outputShutdown=%b",
|
||||
readBuffer.position(), readBuffer.limit(), readBuffer.capacity(),
|
||||
writeBuffer.position(), writeBuffer.limit(), writeBuffer.capacity(),
|
||||
closed.get(), inputShutdown.get(), outputShutdown.get()
|
||||
writeBuffer.position(), writeBuffer.limit(), writeBuffer.capacity(), inputShutdown.get(), outputShutdown.get()
|
||||
);
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 其他方法保持不变...
|
||||
public int available() throws IOException {
|
||||
if (closed.get() || inputShutdown.get()) {
|
||||
if ( inputShutdown.get()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
readLock.lock();
|
||||
try {
|
||||
|
||||
return readBuffer.remaining();
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public long skip(long n) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user