File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\fs\FilePathWrapper.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store.fs;
8
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.nio.channels.FileChannel;
13 import java.util.List;
14 import org.h2.message.DbException;
15
16 /**
17 * The base class for wrapping / delegating file systems such as
18 * the split file system.
19 */
20 public abstract class FilePathWrapper extends FilePath {
21
22 private FilePath base;
23
24 public FilePathWrapper getPath(String path) {
25 return create(path, unwrap(path));
26 }
27
28 /**
29 * Create a wrapped path instance for the given base path.
30 *
31 * @param base the base path
32 * @return the wrapped path
33 */
34 public FilePathWrapper wrap(FilePath base) {
35 return base == null ? null : create(getPrefix() + base.name, base);
36 }
37
38 public FilePath unwrap() {
39 return unwrap(name);
40 }
41
42 private FilePathWrapper create(String path, FilePath base) {
43 try {
44 FilePathWrapper p = getClass().newInstance();
45 p.name = path;
46 p.base = base;
47 return p;
48 } catch (Exception e) {
49 throw DbException.convert(e);
50 }
51 }
52
53 protected String getPrefix() {
54 return getScheme() + ":";
55 }
56
57 /**
58 * Get the base path for the given wrapped path.
59 *
60 * @param path the path including the scheme prefix
61 * @return the base file path
62 */
63 protected FilePath unwrap(String path) {
64 return FilePath.get(path.substring(getScheme().length() + 1));
65 }
66
67 protected FilePath getBase() {
68 return base;
69 }
70
71 public boolean canWrite() {
72 return base.canWrite();
73 }
74
75 public void createDirectory() {
76 base.createDirectory();
77 }
78
79 public boolean createFile() {
80 return base.createFile();
81 }
82
83 public void delete() {
84 base.delete();
85 }
86
87 public boolean exists() {
88 return base.exists();
89 }
90
91 public FilePath getParent() {
92 return wrap(base.getParent());
93 }
94
95 public boolean isAbsolute() {
96 return base.isAbsolute();
97 }
98
99 public boolean isDirectory() {
100 return base.isDirectory();
101 }
102
103 public long lastModified() {
104 return base.lastModified();
105 }
106
107 public FilePath toRealPath() {
108 return wrap(base.toRealPath());
109 }
110
111 public List<FilePath> newDirectoryStream() {
112 List<FilePath> list = base.newDirectoryStream();
113 for (int i = 0, len = list.size(); i < len; i++) {
114 list.set(i, wrap(list.get(i)));
115 }
116 return list;
117 }
118
119 public void moveTo(FilePath newName) {
120 base.moveTo(((FilePathWrapper) newName).base);
121 }
122
123 public InputStream newInputStream() throws IOException {
124 return base.newInputStream();
125 }
126
127 public OutputStream newOutputStream(boolean append) {
128 return base.newOutputStream(append);
129 }
130
131 public FileChannel open(String mode) throws IOException {
132 return base.open(mode);
133 }
134
135 public boolean setReadOnly() {
136 return base.setReadOnly();
137 }
138
139 public long size() {
140 return base.size();
141 }
142
143 public FilePath createTempFile(String suffix, boolean deleteOnExit, boolean inTempDir)
144 throws IOException {
145 return wrap(base.createTempFile(suffix, deleteOnExit, inTempDir));
146 }
147
148 }
149