71 this.jrtfs = jrtfs;
72 this.path = normalized ? path : normalize(path);
73 this.resolved = null;
74 }
75
76 final String getName() {
77 return path;
78 }
79
80 @Override
81 public final JrtPath getRoot() {
82 if (this.isAbsolute()) {
83 return jrtfs.getRootPath();
84 } else {
85 return null;
86 }
87 }
88
89 @Override
90 public final JrtPath getFileName() {
91 if (path.length() == 0)
92 return this;
93 if (path.length() == 1 && path.charAt(0) == '/')
94 return null;
95 int off = path.lastIndexOf('/');
96 if (off == -1)
97 return this;
98 return new JrtPath(jrtfs, path.substring(off + 1), true);
99 }
100
101 @Override
102 public final JrtPath getParent() {
103 initOffsets();
104 int count = offsets.length;
105 if (count == 0) { // no elements so no parent
106 return null;
107 }
108 int off = offsets[count - 1] - 1;
109 if (off <= 0) { // parent is root only (may be null)
110 return getRoot();
111 }
193 }
194 if (mlen != olen) {
195 return false;
196 }
197 int n = 0;
198 while (n < mlen) {
199 if (path.charAt(mbegin + n) != other.path.charAt(obegin + n)) {
200 return false;
201 }
202 n++;
203 }
204 return true;
205 }
206
207 @Override
208 public final JrtPath relativize(Path other) {
209 final JrtPath o = checkPath(other);
210 if (o.equals(this)) {
211 return new JrtPath(jrtfs, "", true);
212 }
213 if (path.length() == 0) {
214 return o;
215 }
216 if (jrtfs != o.jrtfs || isAbsolute() != o.isAbsolute()) {
217 throw new IllegalArgumentException(
218 "Incorrect filesystem or path: " + other);
219 }
220 final String tp = this.path;
221 final String op = o.path;
222 if (op.startsWith(tp)) { // fast path
223 int off = tp.length();
224 if (op.charAt(off - 1) == '/')
225 return new JrtPath(jrtfs, op.substring(off), true);
226 if (op.charAt(off) == '/')
227 return new JrtPath(jrtfs, op.substring(off + 1), true);
228 }
229 int mc = this.getNameCount();
230 int oc = o.getNameCount();
231 int n = Math.min(mc, oc);
232 int i = 0;
233 while (i < n) {
245 while (dotdots > 0) {
246 sb.append("..");
247 if (sb.length() < len) { // no tailing slash at the end
248 sb.append('/');
249 }
250 dotdots--;
251 }
252 if (i < oc) {
253 sb.append(o.path, o.offsets[i], o.path.length());
254 }
255 return new JrtPath(jrtfs, sb.toString(), true);
256 }
257
258 @Override
259 public JrtFileSystem getFileSystem() {
260 return jrtfs;
261 }
262
263 @Override
264 public final boolean isAbsolute() {
265 return path.length() > 0 && path.charAt(0) == '/';
266 }
267
268 @Override
269 public final JrtPath resolve(Path other) {
270 final JrtPath o = checkPath(other);
271 if (this.path.length() == 0 || o.isAbsolute()) {
272 return o;
273 }
274 if (o.path.length() == 0) {
275 return this;
276 }
277 StringBuilder sb = new StringBuilder(path.length() + o.path.length() + 1);
278 sb.append(path);
279 if (path.charAt(path.length() - 1) != '/')
280 sb.append('/');
281 sb.append(o.path);
282 return new JrtPath(jrtfs, sb.toString(), true);
283 }
284
285 @Override
286 public final Path resolveSibling(Path other) {
287 Objects.requireNonNull(other, "other");
288 Path parent = getParent();
289 return (parent == null) ? other : parent.resolve(other);
290 }
291
292 @Override
293 public final boolean startsWith(Path other) {
294 if (!(Objects.requireNonNull(other) instanceof JrtPath))
295 return false;
296 final JrtPath o = (JrtPath)other;
297 final String tp = this.path;
298 final String op = o.path;
299 if (isAbsolute() != o.isAbsolute() || !tp.startsWith(op)) {
300 return false;
301 }
302 int off = op.length();
303 if (off == 0) {
304 return tp.length() == 0;
305 }
306 // check match is on name boundary
307 return tp.length() == off || tp.charAt(off) == '/' ||
308 off == 0 || op.charAt(off - 1) == '/';
309 }
310
311 @Override
312 public final boolean endsWith(Path other) {
313 if (!(Objects.requireNonNull(other) instanceof JrtPath))
314 return false;
315 final JrtPath o = (JrtPath)other;
316 final JrtPath t = this;
317 int olast = o.path.length() - 1;
318 if (olast > 0 && o.path.charAt(olast) == '/') {
319 olast--;
320 }
321 int last = t.path.length() - 1;
322 if (last > 0 && t.path.charAt(last) == '/') {
323 last--;
324 }
|
71 this.jrtfs = jrtfs;
72 this.path = normalized ? path : normalize(path);
73 this.resolved = null;
74 }
75
76 final String getName() {
77 return path;
78 }
79
80 @Override
81 public final JrtPath getRoot() {
82 if (this.isAbsolute()) {
83 return jrtfs.getRootPath();
84 } else {
85 return null;
86 }
87 }
88
89 @Override
90 public final JrtPath getFileName() {
91 if (path.isEmpty())
92 return this;
93 if (path.length() == 1 && path.charAt(0) == '/')
94 return null;
95 int off = path.lastIndexOf('/');
96 if (off == -1)
97 return this;
98 return new JrtPath(jrtfs, path.substring(off + 1), true);
99 }
100
101 @Override
102 public final JrtPath getParent() {
103 initOffsets();
104 int count = offsets.length;
105 if (count == 0) { // no elements so no parent
106 return null;
107 }
108 int off = offsets[count - 1] - 1;
109 if (off <= 0) { // parent is root only (may be null)
110 return getRoot();
111 }
193 }
194 if (mlen != olen) {
195 return false;
196 }
197 int n = 0;
198 while (n < mlen) {
199 if (path.charAt(mbegin + n) != other.path.charAt(obegin + n)) {
200 return false;
201 }
202 n++;
203 }
204 return true;
205 }
206
207 @Override
208 public final JrtPath relativize(Path other) {
209 final JrtPath o = checkPath(other);
210 if (o.equals(this)) {
211 return new JrtPath(jrtfs, "", true);
212 }
213 if (path.isEmpty()) {
214 return o;
215 }
216 if (jrtfs != o.jrtfs || isAbsolute() != o.isAbsolute()) {
217 throw new IllegalArgumentException(
218 "Incorrect filesystem or path: " + other);
219 }
220 final String tp = this.path;
221 final String op = o.path;
222 if (op.startsWith(tp)) { // fast path
223 int off = tp.length();
224 if (op.charAt(off - 1) == '/')
225 return new JrtPath(jrtfs, op.substring(off), true);
226 if (op.charAt(off) == '/')
227 return new JrtPath(jrtfs, op.substring(off + 1), true);
228 }
229 int mc = this.getNameCount();
230 int oc = o.getNameCount();
231 int n = Math.min(mc, oc);
232 int i = 0;
233 while (i < n) {
245 while (dotdots > 0) {
246 sb.append("..");
247 if (sb.length() < len) { // no tailing slash at the end
248 sb.append('/');
249 }
250 dotdots--;
251 }
252 if (i < oc) {
253 sb.append(o.path, o.offsets[i], o.path.length());
254 }
255 return new JrtPath(jrtfs, sb.toString(), true);
256 }
257
258 @Override
259 public JrtFileSystem getFileSystem() {
260 return jrtfs;
261 }
262
263 @Override
264 public final boolean isAbsolute() {
265 return !path.isEmpty() && path.charAt(0) == '/';
266 }
267
268 @Override
269 public final JrtPath resolve(Path other) {
270 final JrtPath o = checkPath(other);
271 if (this.path.isEmpty() || o.isAbsolute()) {
272 return o;
273 }
274 if (o.path.isEmpty()) {
275 return this;
276 }
277 StringBuilder sb = new StringBuilder(path.length() + o.path.length() + 1);
278 sb.append(path);
279 if (path.charAt(path.length() - 1) != '/')
280 sb.append('/');
281 sb.append(o.path);
282 return new JrtPath(jrtfs, sb.toString(), true);
283 }
284
285 @Override
286 public final Path resolveSibling(Path other) {
287 Objects.requireNonNull(other, "other");
288 Path parent = getParent();
289 return (parent == null) ? other : parent.resolve(other);
290 }
291
292 @Override
293 public final boolean startsWith(Path other) {
294 if (!(Objects.requireNonNull(other) instanceof JrtPath))
295 return false;
296 final JrtPath o = (JrtPath)other;
297 final String tp = this.path;
298 final String op = o.path;
299 if (isAbsolute() != o.isAbsolute() || !tp.startsWith(op)) {
300 return false;
301 }
302 int off = op.length();
303 if (off == 0) {
304 return tp.isEmpty();
305 }
306 // check match is on name boundary
307 return tp.length() == off || tp.charAt(off) == '/' ||
308 off == 0 || op.charAt(off - 1) == '/';
309 }
310
311 @Override
312 public final boolean endsWith(Path other) {
313 if (!(Objects.requireNonNull(other) instanceof JrtPath))
314 return false;
315 final JrtPath o = (JrtPath)other;
316 final JrtPath t = this;
317 int olast = o.path.length() - 1;
318 if (olast > 0 && o.path.charAt(olast) == '/') {
319 olast--;
320 }
321 int last = t.path.length() - 1;
322 if (last > 0 && t.path.charAt(last) == '/') {
323 last--;
324 }
|