提交 ac65d7c1 authored 作者: Thomas Mueller's avatar Thomas Mueller

FileSystem improvements.

上级 d497e7d0
......@@ -213,7 +213,7 @@ public abstract class FilePath {
* truncated first
* @return the output stream
*/
public abstract OutputStream newOutputStream(boolean append);
public abstract OutputStream newOutputStream(boolean append) throws IOException;
/**
* Open a random access file object.
......
......@@ -247,7 +247,7 @@ public class FilePathDisk extends FilePath {
throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name);
}
public OutputStream newOutputStream(boolean append) {
public OutputStream newOutputStream(boolean append) throws IOException {
try {
File file = new File(name);
File parent = file.getParentFile();
......@@ -259,11 +259,7 @@ public class FilePathDisk extends FilePath {
return out;
} catch (IOException e) {
freeMemoryAndFinalize();
try {
return new FileOutputStream(name);
} catch (IOException e2) {
throw DbException.convertIOException(e, name);
}
}
}
......@@ -398,7 +394,6 @@ class FileDisk extends FileBase {
public FileChannel truncate(long newLength) throws IOException {
if (newLength < file.length()) {
// some implementations actually only support truncate
file.setLength(newLength);
}
return this;
......
......@@ -136,14 +136,10 @@ public class FilePathMem extends FilePath {
// TODO directories are not really supported
}
public OutputStream newOutputStream(boolean append) {
try {
public OutputStream newOutputStream(boolean append) throws IOException {
FileMemData obj = getMemoryFile();
FileMem m = new FileMem(obj, false);
return new FileChannelOutputStream(m, append);
} catch (IOException e) {
throw DbException.convertIOException(e, name);
}
}
public InputStream newInputStream() {
......
......@@ -59,7 +59,7 @@ public class FilePathRec extends FilePathWrapper {
return new FileRec(this, super.open(mode), name);
}
public OutputStream newOutputStream(boolean append) {
public OutputStream newOutputStream(boolean append) throws IOException {
log(Recorder.OPEN_OUTPUT_STREAM, name);
return super.newOutputStream(append);
}
......
......@@ -166,12 +166,8 @@ public class FilePathSplit extends FilePathWrapper {
throw new IOException(message);
}
public OutputStream newOutputStream(boolean append) {
try {
public OutputStream newOutputStream(boolean append) throws IOException {
return new FileChannelOutputStream(open("rw"), append);
} catch (IOException e) {
throw DbException.convertIOException(e, name);
}
}
public void moveTo(FilePath path) {
......
......@@ -124,7 +124,7 @@ public abstract class FilePathWrapper extends FilePath {
return base.newInputStream();
}
public OutputStream newOutputStream(boolean append) {
public OutputStream newOutputStream(boolean append) throws IOException {
return base.newOutputStream(append);
}
......
......@@ -168,8 +168,8 @@ public class FilePathZip extends FilePath {
return new FileZip(file, entry);
}
public OutputStream newOutputStream(boolean append) {
throw DbException.getUnsupportedException("write");
public OutputStream newOutputStream(boolean append) throws IOException {
throw new IOException("write");
}
public void moveTo(FilePath newName) {
......
......@@ -219,7 +219,7 @@ public class FileUtils {
* truncated first
* @return the output stream
*/
public static OutputStream newOutputStream(String fileName, boolean append) {
public static OutputStream newOutputStream(String fileName, boolean append) throws IOException {
return FilePath.get(fileName).newOutputStream(append);
}
......
......@@ -6,7 +6,6 @@
*/
package org.h2.test.unit;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
......@@ -25,6 +24,7 @@ import java.util.List;
import java.util.Random;
import org.h2.message.DbException;
import org.h2.store.fs.FilePath;
import org.h2.store.fs.FilePathCrypt;
import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase;
import org.h2.test.utils.AssertThrows;
......@@ -58,8 +58,8 @@ public class TestFileSystem extends TestBase {
testMemFsDir();
testClasspath();
FilePathDebug.register().setTrace(true);
// testFileSystem("crypt:007:" + getBaseDir() + "/fs");
FilePathCrypt.register();
testFileSystem("crypt:0007:" + getBaseDir() + "/fs");
testSimpleExpandTruncateSize();
testSplitDatabaseInZip();
testDatabaseInMemFileSys();
......@@ -72,7 +72,6 @@ public class TestFileSystem extends TestBase {
testFileSystem("memLZF:");
testUserHome();
try {
// testFileSystem("crypt:007:" + getBaseDir() + "/fs");
testFileSystem("nio:" + getBaseDir() + "/fs");
testFileSystem("nioMapped:" + getBaseDir() + "/fs");
if (!config.splitFileSystem) {
......@@ -230,7 +229,7 @@ public class TestFileSystem extends TestBase {
}
private void testReadOnly(final String f) throws IOException {
new AssertThrows(DbException.class) { public void test() {
new AssertThrows(IOException.class) { public void test() throws IOException {
FileUtils.newOutputStream(f, false);
}};
new AssertThrows(DbException.class) { public void test() {
......@@ -500,6 +499,7 @@ public class TestFileSystem extends TestBase {
}
case 2: {
trace("truncate " + pos);
buff.append("truncate " + pos + "\n");
f.truncate(pos);
if (pos < ra.length()) {
// truncate is supposed to have no effect if the
......@@ -507,7 +507,6 @@ public class TestFileSystem extends TestBase {
ra.setLength(pos);
}
assertEquals(ra.getFilePointer(), f.position());
buff.append("truncate " + pos + "\n");
break;
}
case 3: {
......@@ -516,13 +515,9 @@ public class TestFileSystem extends TestBase {
byte[] b1 = new byte[len];
byte[] b2 = new byte[len];
trace("readFully " + len);
buff.append("readFully " + len + "\n");
ra.readFully(b1, 0, len);
try {
FileUtils.readFully(f, ByteBuffer.wrap(b2, 0, len));
} catch (EOFException e) {
e.printStackTrace();
}
buff.append("readFully " + len + "\n");
assertEquals(b1, b2);
break;
}
......
......@@ -132,7 +132,7 @@ public class FilePathDebug extends FilePathWrapper {
public InputStream newInputStream() throws IOException {
trace(name, "newInputStream");
InputStream in = super.newInputStream();
if (!trace) {
if (!isTrace()) {
return in;
}
final String fileName = name;
......@@ -159,7 +159,7 @@ public class FilePathDebug extends FilePathWrapper {
return new FileDebug(this, super.open(mode), name);
}
public OutputStream newOutputStream(boolean append) {
public OutputStream newOutputStream(boolean append) throws IOException {
trace(name, "newOutputStream", append);
return super.newOutputStream(append);
}
......@@ -183,7 +183,7 @@ public class FilePathDebug extends FilePathWrapper {
* @param params parameters if any
*/
void trace(String fileName, String method, Object... params) {
if (trace) {
if (isTrace()) {
StringBuilder buff = new StringBuilder(" ");
buff.append(unwrap(fileName)).append(' ').append(method);
for (Object s : params) {
......@@ -202,11 +202,11 @@ public class FilePathDebug extends FilePathWrapper {
}
public boolean isTrace() {
return trace;
return INSTANCE.trace;
}
public void setTrace(boolean trace) {
this.trace = trace;
INSTANCE.trace = trace;
}
public String getScheme() {
......@@ -227,7 +227,7 @@ class FileDebug extends FileBase {
FileDebug(FilePathDebug debug, FileChannel channel, String name) {
this.debug = debug;
this.channel = channel;
this.name = debug.getScheme() + ":" + name;
this.name = name;
}
public void implCloseChannel() throws IOException {
......@@ -296,4 +296,8 @@ class FileDebug extends FileBase {
return channel.tryLock(position, size, shared);
}
public String toString() {
return name;
}
}
......@@ -122,7 +122,7 @@ public class FilePathUnstable extends FilePathWrapper {
return new FileUnstable(this, super.open(mode));
}
public OutputStream newOutputStream(boolean append) {
public OutputStream newOutputStream(boolean append) throws IOException {
return super.newOutputStream(append);
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论