Debian Codegraph @

src/main/org/h2/store/fs/FilePathWrapper.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.store.fs;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.nio.channels.FileChannel;
12 import java.util.List;
13
14 /**
15 * The base class for wrapping / delegating file systems such as
16 * the split file system.
17 */
18 public abstract class FilePathWrapper extends FilePath {
19
20 private FilePath base;
21
22 @Override
23 public FilePathWrapper getPath(String path) {
24 return create(path, unwrap(path));
25 }
26
27 /**
28 * Create a wrapped path instance for the given base path.
29 *
30 * @param base the base path
31 * @return the wrapped path
32 */
33 public FilePathWrapper wrap(FilePath base) {
34 return base == null ? null : create(getPrefix() + base.name, base);
35 }
36
37 @Override
38 public FilePath unwrap() {
39 return unwrap(name);
40 }
41
42 private FilePathWrapper create(String path, FilePath base) {
43 try {
44 FilePathWrapper p = getClass().getDeclaredConstructor().newInstance();
45 p.name = path;
46 p.base = base;
47 return p;
48 } catch (Exception e) {
49 throw new IllegalArgumentException("Path: " + path, 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 @Override
72 public boolean canWrite() {
73 return base.canWrite();
74 }
75
76 @Override
77 public void createDirectory() {
78 base.createDirectory();
79 }
80
81 @Override
82 public boolean createFile() {
83 return base.createFile();
84 }
85
86 @Override
87 public void delete() {
88 base.delete();
89 }
90
91 @Override
92 public boolean exists() {
93 return base.exists();
94 }
95
96 @Override
97 public FilePath getParent() {
98 return wrap(base.getParent());
99 }
100
101 @Override
102 public boolean isAbsolute() {
103 return base.isAbsolute();
104 }
105
106 @Override
107 public boolean isDirectory() {
108 return base.isDirectory();
109 }
110
111 @Override
112 public boolean isRegularFile() {
113 return base.isRegularFile();
114 }
115
116 @Override
117 public long lastModified() {
118 return base.lastModified();
119 }
120
121 @Override
122 public FilePath toRealPath() {
123 return wrap(base.toRealPath());
124 }
125
126 @Override
127 public List<FilePath> newDirectoryStream() {
128 List<FilePath> list = base.newDirectoryStream();
129 for (int i = 0, len = list.size(); i < len; i++) {
130 list.set(i, wrap(list.get(i)));
131 }
132 return list;
133 }
134
135 @Override
136 public void moveTo(FilePath newName, boolean atomicReplace) {
137 base.moveTo(((FilePathWrapper) newName).base, atomicReplace);
138 }
139
140 @Override
141 public InputStream newInputStream() throws IOException {
142 return base.newInputStream();
143 }
144
145 @Override
146 public OutputStream newOutputStream(boolean append) throws IOException {
147 return base.newOutputStream(append);
148 }
149
150 @Override
151 public FileChannel open(String mode) throws IOException {
152 return base.open(mode);
153 }
154
155 @Override
156 public boolean setReadOnly() {
157 return base.setReadOnly();
158 }
159
160 @Override
161 public long size() {
162 return base.size();
163 }
164
165 @Override
166 public FilePath createTempFile(String suffix, boolean inTempDir) throws IOException {
167 return wrap(base.createTempFile(suffix, inTempDir));
168 }
169
170 }