提交 21dff6f9 authored 作者: Noel Grandin's avatar Noel Grandin

throw ClosedChannelException instead of generating an NPE

上级 90f3688c
...@@ -9,6 +9,7 @@ import java.io.IOException; ...@@ -9,6 +9,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException; import java.nio.channels.NonWritableChannelException;
...@@ -288,6 +289,9 @@ class FileMem extends FileBase { ...@@ -288,6 +289,9 @@ class FileMem extends FileBase {
if (readOnly) { if (readOnly) {
throw new NonWritableChannelException(); throw new NonWritableChannelException();
} }
if (data == null) {
throw new ClosedChannelException();
}
if (newLength < size()) { if (newLength < size()) {
data.touch(readOnly); data.touch(readOnly);
pos = Math.min(pos, newLength); pos = Math.min(pos, newLength);
...@@ -304,6 +308,9 @@ class FileMem extends FileBase { ...@@ -304,6 +308,9 @@ class FileMem extends FileBase {
@Override @Override
public int write(ByteBuffer src, long position) throws IOException { public int write(ByteBuffer src, long position) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
int len = src.remaining(); int len = src.remaining();
if (len == 0) { if (len == 0) {
return 0; return 0;
...@@ -317,6 +324,9 @@ class FileMem extends FileBase { ...@@ -317,6 +324,9 @@ class FileMem extends FileBase {
@Override @Override
public int write(ByteBuffer src) throws IOException { public int write(ByteBuffer src) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
int len = src.remaining(); int len = src.remaining();
if (len == 0) { if (len == 0) {
return 0; return 0;
...@@ -330,6 +340,9 @@ class FileMem extends FileBase { ...@@ -330,6 +340,9 @@ class FileMem extends FileBase {
@Override @Override
public int read(ByteBuffer dst, long position) throws IOException { public int read(ByteBuffer dst, long position) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
int len = dst.remaining(); int len = dst.remaining();
if (len == 0) { if (len == 0) {
return 0; return 0;
...@@ -346,6 +359,9 @@ class FileMem extends FileBase { ...@@ -346,6 +359,9 @@ class FileMem extends FileBase {
@Override @Override
public int read(ByteBuffer dst) throws IOException { public int read(ByteBuffer dst) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
int len = dst.remaining(); int len = dst.remaining();
if (len == 0) { if (len == 0) {
return 0; return 0;
...@@ -380,6 +396,9 @@ class FileMem extends FileBase { ...@@ -380,6 +396,9 @@ class FileMem extends FileBase {
@Override @Override
public synchronized FileLock tryLock(long position, long size, public synchronized FileLock tryLock(long position, long size,
boolean shared) throws IOException { boolean shared) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
if (shared) { if (shared) {
if (!data.lockShared()) { if (!data.lockShared()) {
return null; return null;
...@@ -406,7 +425,7 @@ class FileMem extends FileBase { ...@@ -406,7 +425,7 @@ class FileMem extends FileBase {
@Override @Override
public String toString() { public String toString() {
return data.getName(); return data == null ? "<closed>" : data.getName();
} }
} }
......
...@@ -9,6 +9,7 @@ import java.io.IOException; ...@@ -9,6 +9,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException; import java.nio.channels.NonWritableChannelException;
...@@ -297,6 +298,9 @@ class FileNioMem extends FileBase { ...@@ -297,6 +298,9 @@ class FileNioMem extends FileBase {
if (readOnly) { if (readOnly) {
throw new NonWritableChannelException(); throw new NonWritableChannelException();
} }
if (data == null) {
throw new ClosedChannelException();
}
if (newLength < size()) { if (newLength < size()) {
data.touch(readOnly); data.touch(readOnly);
pos = Math.min(pos, newLength); pos = Math.min(pos, newLength);
...@@ -313,6 +317,9 @@ class FileNioMem extends FileBase { ...@@ -313,6 +317,9 @@ class FileNioMem extends FileBase {
@Override @Override
public int write(ByteBuffer src) throws IOException { public int write(ByteBuffer src) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
int len = src.remaining(); int len = src.remaining();
if (len == 0) { if (len == 0) {
return 0; return 0;
...@@ -326,6 +333,9 @@ class FileNioMem extends FileBase { ...@@ -326,6 +333,9 @@ class FileNioMem extends FileBase {
@Override @Override
public int read(ByteBuffer dst) throws IOException { public int read(ByteBuffer dst) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
int len = dst.remaining(); int len = dst.remaining();
if (len == 0) { if (len == 0) {
return 0; return 0;
...@@ -342,6 +352,9 @@ class FileNioMem extends FileBase { ...@@ -342,6 +352,9 @@ class FileNioMem extends FileBase {
@Override @Override
public int read(ByteBuffer dst, long position) throws IOException { public int read(ByteBuffer dst, long position) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
int len = dst.remaining(); int len = dst.remaining();
if (len == 0) { if (len == 0) {
return 0; return 0;
...@@ -375,6 +388,9 @@ class FileNioMem extends FileBase { ...@@ -375,6 +388,9 @@ class FileNioMem extends FileBase {
@Override @Override
public synchronized FileLock tryLock(long position, long size, public synchronized FileLock tryLock(long position, long size,
boolean shared) throws IOException { boolean shared) throws IOException {
if (data == null) {
throw new ClosedChannelException();
}
if (shared) { if (shared) {
if (!data.lockShared()) { if (!data.lockShared()) {
return null; return null;
...@@ -401,7 +417,7 @@ class FileNioMem extends FileBase { ...@@ -401,7 +417,7 @@ class FileNioMem extends FileBase {
@Override @Override
public String toString() { public String toString() {
return data.getName(); return data == null ? "<closed>" : data.getName();
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论