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

Slightly simplified FileSystem copy

上级 d7de0330
...@@ -214,14 +214,6 @@ public abstract class FileSystem { ...@@ -214,14 +214,6 @@ public abstract class FileSystem {
*/ */
public abstract boolean canWrite(String fileName); public abstract boolean canWrite(String fileName);
/**
* Copy a file from one directory to another, or to another file.
*
* @param source the name of the original file
* @param target the file name of the copy
*/
public abstract void copy(String source, String target);
/** /**
* Create all required directories that are required for this file. * Create all required directories that are required for this file.
* *
......
...@@ -309,23 +309,6 @@ public class FileSystemDisk extends FileSystem { ...@@ -309,23 +309,6 @@ public class FileSystemDisk extends FileSystem {
} }
} }
public void copy(String source, String target) {
source = translateFileName(source);
target = translateFileName(target);
OutputStream out = null;
InputStream in = null;
try {
in = IOUtils.openFileInputStream(source);
out = IOUtils.openFileOutputStream(target, false);
IOUtils.copy(in, out);
} catch (IOException e) {
throw DbException.convertIOException(e, "original: " + source + " copy: " + target);
} finally {
IOUtils.closeSilently(in);
IOUtils.closeSilently(out);
}
}
public void createDirs(String fileName) { public void createDirs(String fileName) {
fileName = translateFileName(fileName); fileName = translateFileName(fileName);
File f = new File(fileName); File f = new File(fileName);
......
...@@ -13,7 +13,6 @@ import java.util.ArrayList; ...@@ -13,7 +13,6 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.TreeMap; import java.util.TreeMap;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.IOUtils;
import org.h2.util.New; import org.h2.util.New;
/** /**
...@@ -163,16 +162,6 @@ public class FileSystemMemory extends FileSystem { ...@@ -163,16 +162,6 @@ public class FileSystemMemory extends FileSystem {
return true; return true;
} }
public void copy(String source, String target) {
try {
OutputStream out = openFileOutputStream(target, false);
InputStream in = openFileInputStream(source);
IOUtils.copyAndClose(in, out);
} catch (IOException e) {
throw DbException.convertIOException(e, "Can not copy " + source + " to " + target);
}
}
public void createDirs(String fileName) { public void createDirs(String fileName) {
// TODO directories are not really supported // TODO directories are not really supported
} }
......
...@@ -49,21 +49,6 @@ public class FileSystemSplit extends FileSystemWrapper { ...@@ -49,21 +49,6 @@ public class FileSystemSplit extends FileSystemWrapper {
return result; return result;
} }
public void copy(String source, String target) {
source = unwrap(source);
target = unwrap(target);
getInstance(source).copy(source, target);
for (int i = 1;; i++) {
String o = getFileName(source, i);
if (getInstance(o).exists(o)) {
String c = getFileName(target, i);
getInstance(o).copy(o, c);
} else {
break;
}
}
}
public void delete(String fileName) { public void delete(String fileName) {
fileName = unwrap(fileName); fileName = unwrap(fileName);
for (int i = 0;; i++) { for (int i = 0;; i++) {
......
...@@ -33,10 +33,6 @@ public abstract class FileSystemWrapper extends FileSystem { ...@@ -33,10 +33,6 @@ public abstract class FileSystemWrapper extends FileSystem {
return IOUtils.setReadOnly(unwrap(fileName)); return IOUtils.setReadOnly(unwrap(fileName));
} }
public void copy(String source, String target) {
IOUtils.copy(unwrap(source), unwrap(target));
}
public void createDirs(String fileName) { public void createDirs(String fileName) {
IOUtils.createDirs(unwrap(fileName)); IOUtils.createDirs(unwrap(fileName));
} }
......
...@@ -33,10 +33,6 @@ public class FileSystemZip extends FileSystem { ...@@ -33,10 +33,6 @@ public class FileSystemZip extends FileSystem {
return false; return false;
} }
public void copy(String original, String copy) {
throw DbException.getUnsupportedException("write");
}
public void createDirs(String fileName) { public void createDirs(String fileName) {
// ignore // ignore
} }
......
...@@ -433,7 +433,7 @@ public class IOUtils { ...@@ -433,7 +433,7 @@ public class IOUtils {
* @param s the string * @param s the string
* @return the input stream * @return the input stream
*/ */
public static InputStream getInputStream(String s) { public static InputStream getInputStreamFromString(String s) {
if (s == null) { if (s == null) {
return null; return null;
} }
...@@ -447,7 +447,7 @@ public class IOUtils { ...@@ -447,7 +447,7 @@ public class IOUtils {
* @param s the string or null * @param s the string or null
* @return the reader * @return the reader
*/ */
public static Reader getReader(String s) { public static Reader getReaderFromString(String s) {
return s == null ? null : new StringReader(s); return s == null ? null : new StringReader(s);
} }
...@@ -556,8 +556,10 @@ public class IOUtils { ...@@ -556,8 +556,10 @@ public class IOUtils {
* @param original the original file name * @param original the original file name
* @param copy the file name of the copy * @param copy the file name of the copy
*/ */
public static void copy(String original, String copy) { public static void copy(String original, String copy) throws IOException {
getFileSystem(original).copy(original, copy); InputStream in = openFileInputStream(original);
OutputStream out = openFileOutputStream(copy, false);
copyAndClose(in, out);
} }
/** /**
......
...@@ -175,17 +175,6 @@ public class FileSystemDatabase extends FileSystem { ...@@ -175,17 +175,6 @@ public class FileSystemDatabase extends FileSystem {
return true; return true;
} }
public void copy(String source, String target) {
try {
OutputStream out = openFileOutputStream(target, false);
InputStream in = openFileInputStream(source);
IOUtils.copyAndClose(in, out);
} catch (IOException e) {
rollback();
throw DbException.convertIOException(e, "Can not copy " + source + " to " + target);
}
}
public void createDirs(String fileName) { public void createDirs(String fileName) {
fileName = unwrap(fileName); fileName = unwrap(fileName);
try { try {
......
...@@ -242,7 +242,7 @@ public class TestFileSystem extends TestBase { ...@@ -242,7 +242,7 @@ public class TestFileSystem extends TestBase {
String[] list = fs.listFiles(fsBase); String[] list = fs.listFiles(fsBase);
assertEquals(1, list.length); assertEquals(1, list.length);
assertTrue(list[0].endsWith("test")); assertTrue(list[0].endsWith("test"));
fs.copy(fsBase + "/test", fsBase + "/test3"); IOUtils.copy(fsBase + "/test", fsBase + "/test3");
fs.rename(fsBase + "/test3", fsBase + "/test2"); fs.rename(fsBase + "/test3", fsBase + "/test2");
assertTrue(!fs.exists(fsBase + "/test3")); assertTrue(!fs.exists(fsBase + "/test3"));
assertTrue(fs.exists(fsBase + "/test2")); assertTrue(fs.exists(fsBase + "/test2"));
......
...@@ -15,7 +15,6 @@ import org.h2.engine.Constants; ...@@ -15,7 +15,6 @@ import org.h2.engine.Constants;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.utils.Recorder; import org.h2.test.utils.Recorder;
import org.h2.test.utils.RecordingFileSystem; import org.h2.test.utils.RecordingFileSystem;
...@@ -98,8 +97,8 @@ public class TestReopen extends TestBase implements Recorder { ...@@ -98,8 +97,8 @@ public class TestReopen extends TestBase implements Recorder {
// System.out.println(fileName + " " + IOUtils.length(fileName)); // System.out.println(fileName + " " + IOUtils.length(fileName));
return; return;
} }
FileSystem.getInstance(fileName).copy(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
try { try {
IOUtils.copy(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
verifyCount++; verifyCount++;
// avoid using the Engine class to avoid deadlocks // avoid using the Engine class to avoid deadlocks
Properties p = new Properties(); Properties p = new Properties();
...@@ -143,8 +142,8 @@ public class TestReopen extends TestBase implements Recorder { ...@@ -143,8 +142,8 @@ public class TestReopen extends TestBase implements Recorder {
// ignore // ignore
} }
testDatabase += "X"; testDatabase += "X";
FileSystem.getInstance(fileName).copy(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
try { try {
IOUtils.copy(fileName, testDatabase + Constants.SUFFIX_PAGE_FILE);
// avoid using the Engine class to avoid deadlocks // avoid using the Engine class to avoid deadlocks
Properties p = new Properties(); Properties p = new Properties();
ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + testDatabase + ";FILE_LOCK=NO", p); ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + testDatabase + ";FILE_LOCK=NO", p);
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.test.utils; package org.h2.test.utils;
import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
...@@ -64,11 +65,6 @@ public class DebugFileSystem extends FileSystemWrapper { ...@@ -64,11 +65,6 @@ public class DebugFileSystem extends FileSystemWrapper {
return super.canWrite(fileName); return super.canWrite(fileName);
} }
public void copy(String source, String target) {
trace(source, "copy", unwrap(target));
super.copy(source, target);
}
public void createDirs(String fileName) { public void createDirs(String fileName) {
trace(fileName, "createDirs"); trace(fileName, "createDirs");
super.createDirs(fileName); super.createDirs(fileName);
...@@ -155,9 +151,28 @@ public class DebugFileSystem extends FileSystemWrapper { ...@@ -155,9 +151,28 @@ public class DebugFileSystem extends FileSystemWrapper {
return super.getCanonicalPath(fileName); return super.getCanonicalPath(fileName);
} }
public InputStream openFileInputStream(String fileName) throws IOException { public InputStream openFileInputStream(final String fileName) throws IOException {
trace(fileName, "openFileInputStream"); trace(fileName, "openFileInputStream");
return super.openFileInputStream(fileName); InputStream in = super.openFileInputStream(fileName);
if (!trace) {
return in;
}
return new FilterInputStream(in) {
public int read(byte[] b) throws IOException {
trace(fileName, "in.read(b)");
return super.read(b);
}
public int read(byte[] b, int off, int len) throws IOException {
trace(fileName, "in.read(b, " + off + ", " + len + ")");
return super.read(b, off, len);
}
public long skip(long n) throws IOException {
trace(fileName, "in.skip(" + n + ")");
return super.skip(n);
}
};
} }
public FileObject openFileObject(String fileName, String mode) throws IOException { public FileObject openFileObject(String fileName, String mode) throws IOException {
......
...@@ -44,11 +44,6 @@ public class RecordingFileSystem extends FileSystemWrapper { ...@@ -44,11 +44,6 @@ public class RecordingFileSystem extends FileSystemWrapper {
RecordingFileSystem.recorder = recorder; RecordingFileSystem.recorder = recorder;
} }
public void copy(String source, String target) {
log(Recorder.COPY, unwrap(source) + ":" + unwrap(target));
super.copy(source, target);
}
public void createDirs(String fileName) { public void createDirs(String fileName) {
log(Recorder.CREATE_DIRS, unwrap(fileName)); log(Recorder.CREATE_DIRS, unwrap(fileName));
super.createDirs(fileName); super.createDirs(fileName);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论