提交 8b48ecdb authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

FileSystem: improve exception throwing compatibility with JDK

上级 af80d630
......@@ -23,6 +23,7 @@ Change Log
</li><li>Follow JDBC specification on Procedures MetaData, use P0 as
return type of procedure.
</li><li>Issue 531: IDENTITY ignored for added column.
</li><li>FileSystem: improve exception throwing compatibility with JDK
</li></ul>
<h2>Version 1.4.178 Beta (2014-05-02)</h2>
......
......@@ -18,6 +18,7 @@ import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;
import java.util.ArrayList;
import java.util.List;
......@@ -395,10 +396,12 @@ class FileDisk extends FileBase {
private final RandomAccessFile file;
private final String name;
private final boolean readOnly;
FileDisk(String fileName, String mode) throws FileNotFoundException {
this.file = new RandomAccessFile(fileName, mode);
this.name = fileName;
this.readOnly = mode.equals("r");
}
@Override
......@@ -419,6 +422,10 @@ class FileDisk extends FileBase {
@Override
public FileChannel truncate(long newLength) throws IOException {
// compatibility with JDK FileChannel#truncate
if (readOnly) {
throw new NonWritableChannelException();
}
if (newLength < file.length()) {
file.setLength(newLength);
}
......
......@@ -12,6 +12,7 @@ import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
......@@ -264,6 +265,10 @@ class FileMem extends FileBase {
@Override
public FileChannel truncate(long newLength) throws IOException {
// compatibility with JDK FileChannel#truncate
if (readOnly) {
throw new NonWritableChannelException();
}
if (newLength < size()) {
data.touch(readOnly);
pos = Math.min(pos, newLength);
......
......@@ -82,7 +82,6 @@ class FileNio extends FileBase {
@Override
public FileChannel truncate(long newLength) throws IOException {
try {
long size = channel.size();
if (newLength < size) {
long pos = channel.position();
......@@ -101,9 +100,6 @@ class FileNio extends FileBase {
}
}
return this;
} catch (NonWritableChannelException e) {
throw new IOException("read only");
}
}
@Override
......
......@@ -16,6 +16,7 @@ import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;
import org.h2.engine.SysProperties;
......@@ -203,6 +204,10 @@ class FileNioMapped extends FileBase {
@Override
public synchronized FileChannel truncate(long newLength) throws IOException {
// compatibility with JDK FileChannel#truncate
if (mode == MapMode.READ_ONLY) {
throw new NonWritableChannelException();
}
if (newLength < size()) {
setFileLength(newLength);
}
......
......@@ -12,6 +12,7 @@ import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
......@@ -256,6 +257,10 @@ class FileNioMem extends FileBase {
@Override
public FileChannel truncate(long newLength) throws IOException {
// compatibility with JDK FileChannel#truncate
if (readOnly) {
throw new NonWritableChannelException();
}
if (newLength < size()) {
data.touch(readOnly);
pos = Math.min(pos, newLength);
......
......@@ -15,6 +15,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
......@@ -549,7 +550,7 @@ public class TestFileSystem extends TestBase {
fc.write(ByteBuffer.wrap(test, 0, 10));
}
};
new AssertThrows(IOException.class) {
new AssertThrows(NonWritableChannelException.class) {
@Override
public void test() throws Exception {
fc.truncate(10);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论