author | alanb |
Mon, 18 Jan 2010 15:21:34 +0000 | |
changeset 4679 | 5feae448a41f |
parent 3065 | 452aaa2899fc |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
1 |
/* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
2 |
* Copyright 2007-2009 Sun Microsystems, Inc. All Rights Reserved. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
4 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Sun designates this |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
10 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
15 |
* accompanied this code). |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
16 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
20 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
23 |
* have any questions. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
24 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
25 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
26 |
package sun.nio.fs; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
27 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
28 |
import java.nio.file.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
29 |
import static java.nio.file.StandardOpenOption.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
30 |
import java.nio.file.attribute.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
31 |
import java.nio.channels.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
33 |
import java.io.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
34 |
import java.util.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
35 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
36 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
37 |
* Base implementation class for a {@code Path}. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
38 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
39 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
40 |
abstract class AbstractPath extends Path { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
41 |
protected AbstractPath() { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
42 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
43 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
44 |
public final Path createFile(FileAttribute<?>... attrs) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
45 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
46 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
47 |
EnumSet<StandardOpenOption> options = EnumSet.of(CREATE_NEW, WRITE); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
48 |
SeekableByteChannel sbc = newByteChannel(options, attrs); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
49 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
50 |
sbc.close(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
51 |
} catch (IOException x) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
52 |
// ignore |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
53 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
54 |
return this; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
55 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
56 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
57 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
58 |
* Deletes a file. The {@code failIfNotExists} parameters determines if an |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
59 |
* {@code IOException} is thrown when the file does not exist. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
60 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
61 |
abstract void implDelete(boolean failIfNotExists) throws IOException; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
62 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
63 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
64 |
public final void delete() throws IOException { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
65 |
implDelete(true); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
66 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
67 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
68 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
69 |
public final void deleteIfExists() throws IOException { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
70 |
implDelete(false); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
71 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
72 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
73 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
74 |
public final InputStream newInputStream(OpenOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
75 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
76 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
77 |
if (options.length > 0) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
78 |
for (OpenOption opt: options) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
79 |
if (opt != READ) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
80 |
throw new UnsupportedOperationException("'" + opt + "' not allowed"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
81 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
82 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
83 |
return Channels.newInputStream(newByteChannel()); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
84 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
85 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
86 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
87 |
public final OutputStream newOutputStream(OpenOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
88 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
89 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
90 |
int len = options.length; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
91 |
Set<OpenOption> opts = new HashSet<OpenOption>(len + 3); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
92 |
if (len == 0) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
93 |
opts.add(CREATE); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
94 |
opts.add(TRUNCATE_EXISTING); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
95 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
96 |
for (OpenOption opt: options) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
97 |
if (opt == READ) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
98 |
throw new IllegalArgumentException("READ not allowed"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
99 |
opts.add(opt); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
100 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
101 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
102 |
opts.add(WRITE); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
103 |
return Channels.newOutputStream(newByteChannel(opts)); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
104 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
105 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
106 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
107 |
public final SeekableByteChannel newByteChannel(OpenOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
108 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
109 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
110 |
Set<OpenOption> set = new HashSet<OpenOption>(options.length); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
111 |
Collections.addAll(set, options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
112 |
return newByteChannel(set); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
113 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
114 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
115 |
private static final DirectoryStream.Filter<Path> acceptAllFilter = |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
116 |
new DirectoryStream.Filter<Path>() { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
117 |
@Override public boolean accept(Path entry) { return true; } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
118 |
}; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
119 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
120 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
121 |
public final DirectoryStream<Path> newDirectoryStream() throws IOException { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
122 |
return newDirectoryStream(acceptAllFilter); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
123 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
124 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
125 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
126 |
public final DirectoryStream<Path> newDirectoryStream(String glob) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
127 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
128 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
129 |
// avoid creating a matcher if all entries are required. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
130 |
if (glob.equals("*")) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
131 |
return newDirectoryStream(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
132 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
133 |
// create a matcher and return a filter that uses it. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
134 |
final PathMatcher matcher = getFileSystem().getPathMatcher("glob:" + glob); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
135 |
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
136 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
137 |
public boolean accept(Path entry) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
138 |
return matcher.matches(entry.getName()); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
139 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
140 |
}; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
141 |
return newDirectoryStream(filter); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
142 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
143 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
144 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
145 |
public final boolean exists() { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
146 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
147 |
checkAccess(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
148 |
return true; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
149 |
} catch (IOException x) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
150 |
// unable to determine if file exists |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
151 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
152 |
return false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
153 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
154 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
155 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
156 |
public final boolean notExists() { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
157 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
158 |
checkAccess(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
159 |
return false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
160 |
} catch (NoSuchFileException x) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
161 |
// file confirmed not to exist |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
162 |
return true; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
163 |
} catch (IOException x) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
164 |
return false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
165 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
166 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
167 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
168 |
private static final WatchEvent.Modifier[] NO_MODIFIERS = new WatchEvent.Modifier[0]; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
169 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
170 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
171 |
public final WatchKey register(WatchService watcher, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
172 |
WatchEvent.Kind<?>... events) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
173 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
174 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
175 |
return register(watcher, events, NO_MODIFIERS); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
176 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
177 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
178 |
abstract void implCopyTo(Path target, CopyOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
179 |
throws IOException; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
180 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
181 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
182 |
public final Path copyTo(Path target, CopyOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
183 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
184 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
185 |
if ((getFileSystem().provider() == target.getFileSystem().provider())) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
186 |
implCopyTo(target, options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
187 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
188 |
copyToForeignTarget(target, options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
189 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
190 |
return target; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
191 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
192 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
193 |
abstract void implMoveTo(Path target, CopyOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
194 |
throws IOException; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
195 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
196 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
197 |
public final Path moveTo(Path target, CopyOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
198 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
199 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
200 |
if ((getFileSystem().provider() == target.getFileSystem().provider())) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
201 |
implMoveTo(target, options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
202 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
203 |
// different providers so copy + delete |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
204 |
copyToForeignTarget(target, convertMoveToCopyOptions(options)); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
205 |
delete(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
206 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
207 |
return target; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
208 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
209 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
210 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
211 |
* Converts the given array of options for moving a file to options suitable |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
212 |
* for copying the file when a move is implemented as copy + delete. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
213 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
214 |
private static CopyOption[] convertMoveToCopyOptions(CopyOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
215 |
throws AtomicMoveNotSupportedException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
216 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
217 |
int len = options.length; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
218 |
CopyOption[] newOptions = new CopyOption[len+2]; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
219 |
for (int i=0; i<len; i++) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
220 |
CopyOption option = options[i]; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
221 |
if (option == StandardCopyOption.ATOMIC_MOVE) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
222 |
throw new AtomicMoveNotSupportedException(null, null, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
223 |
"Atomic move between providers is not supported"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
224 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
225 |
newOptions[i] = option; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
226 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
227 |
newOptions[len] = LinkOption.NOFOLLOW_LINKS; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
228 |
newOptions[len+1] = StandardCopyOption.COPY_ATTRIBUTES; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
229 |
return newOptions; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
230 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
231 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
232 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
233 |
* Parses the arguments for a file copy operation. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
234 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
235 |
private static class CopyOptions { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
236 |
boolean replaceExisting = false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
237 |
boolean copyAttributes = false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
238 |
boolean followLinks = true; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
239 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
240 |
private CopyOptions() { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
241 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
242 |
static CopyOptions parse(CopyOption... options) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
243 |
CopyOptions result = new CopyOptions(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
244 |
for (CopyOption option: options) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
245 |
if (option == StandardCopyOption.REPLACE_EXISTING) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
246 |
result.replaceExisting = true; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
247 |
continue; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
248 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
249 |
if (option == LinkOption.NOFOLLOW_LINKS) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
250 |
result.followLinks = false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
251 |
continue; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
252 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
253 |
if (option == StandardCopyOption.COPY_ATTRIBUTES) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
254 |
result.copyAttributes = true; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
255 |
continue; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
256 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
257 |
if (option == null) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
258 |
throw new NullPointerException(); |
4679
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
259 |
throw new UnsupportedOperationException("'" + option + |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
260 |
"' is not a recognized copy option"); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
261 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
262 |
return result; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
263 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
264 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
265 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
266 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
267 |
* Simple cross-provider copy where the target is a Path. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
268 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
269 |
private void copyToForeignTarget(Path target, CopyOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
270 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
271 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
272 |
CopyOptions opts = CopyOptions.parse(options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
273 |
LinkOption[] linkOptions = (opts.followLinks) ? new LinkOption[0] : |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
274 |
new LinkOption[] { LinkOption.NOFOLLOW_LINKS }; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
275 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
276 |
// attributes of source file |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
277 |
BasicFileAttributes attrs = Attributes |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
278 |
.readBasicFileAttributes(this, linkOptions); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
279 |
if (attrs.isSymbolicLink()) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
280 |
throw new IOException("Copying of symbolic links not supported"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
281 |
|
4679
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
282 |
// check if target exists |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
283 |
boolean exists; |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
284 |
if (opts.replaceExisting) { |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
285 |
try { |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
286 |
target.deleteIfExists(); |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
287 |
exists = false; |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
288 |
} catch (DirectoryNotEmptyException x) { |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
289 |
// let exception translate to FileAlreadyExistsException (6895012) |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
290 |
exists = true; |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
291 |
} |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
292 |
} else { |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
293 |
exists = target.exists(); |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
294 |
} |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
295 |
if (exists) |
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
296 |
throw new FileAlreadyExistsException(target.toString()); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
297 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
298 |
// create directory or file |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
299 |
if (attrs.isDirectory()) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
300 |
target.createDirectory(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
301 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
302 |
copyRegularFileToForeignTarget(target); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
303 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
304 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
305 |
// copy basic attributes to target |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
306 |
if (opts.copyAttributes) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
307 |
BasicFileAttributeView view = target |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
308 |
.getFileAttributeView(BasicFileAttributeView.class, linkOptions); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
309 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
310 |
view.setTimes(attrs.lastModifiedTime(), |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
311 |
attrs.lastAccessTime(), |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
312 |
attrs.creationTime()); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
313 |
} catch (IOException x) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
314 |
// rollback |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
315 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
316 |
target.delete(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
317 |
} catch (IOException ignore) { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
318 |
throw x; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
319 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
320 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
321 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
322 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
323 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
324 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
325 |
* Simple copy of regular file to a target file that exists. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
326 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
327 |
private void copyRegularFileToForeignTarget(Path target) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
328 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
329 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
330 |
ReadableByteChannel rbc = newByteChannel(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
331 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
332 |
// open target file for writing |
4679
5feae448a41f
6917021: (file) copyTo/moveTo can overrwrite existing file when target associated with custom provider
alanb
parents:
3065
diff
changeset
|
333 |
SeekableByteChannel sbc = target.newByteChannel(CREATE_NEW, WRITE); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
334 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
335 |
// simple copy loop |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
336 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
337 |
ByteBuffer buf = ByteBuffer.wrap(new byte[8192]); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
338 |
int n = 0; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
339 |
for (;;) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
340 |
n = rbc.read(buf); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
341 |
if (n < 0) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
342 |
break; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
343 |
assert n > 0; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
344 |
buf.flip(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
345 |
while (buf.hasRemaining()) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
346 |
sbc.write(buf); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
347 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
348 |
buf.rewind(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
349 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
350 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
351 |
} finally { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
352 |
sbc.close(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
353 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
354 |
} finally { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
355 |
rbc.close(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
356 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
357 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
358 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
359 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
360 |
* Splits the given attribute name into the name of an attribute view and |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
361 |
* the attribute. If the attribute view is not identified then it assumed |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
362 |
* to be "basic". |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
363 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
364 |
private static String[] split(String attribute) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
365 |
String[] s = new String[2]; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
366 |
int pos = attribute.indexOf(':'); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
367 |
if (pos == -1) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
368 |
s[0] = "basic"; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
369 |
s[1] = attribute; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
370 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
371 |
s[0] = attribute.substring(0, pos++); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
372 |
s[1] = (pos == attribute.length()) ? "" : attribute.substring(pos); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
373 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
374 |
return s; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
375 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
376 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
377 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
378 |
* Gets a DynamicFileAttributeView by name. Returns {@code null} if the |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
379 |
* view is not available. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
380 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
381 |
abstract DynamicFileAttributeView getFileAttributeView(String name, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
382 |
LinkOption... options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
383 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
384 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
385 |
public final void setAttribute(String attribute, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
386 |
Object value, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
387 |
LinkOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
388 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
389 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
390 |
String[] s = split(attribute); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
391 |
DynamicFileAttributeView view = getFileAttributeView(s[0], options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
392 |
if (view == null) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
393 |
throw new UnsupportedOperationException("View '" + s[0] + "' not available"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
394 |
view.setAttribute(s[1], value); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
395 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
396 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
397 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
398 |
public final Object getAttribute(String attribute, LinkOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
399 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
400 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
401 |
String[] s = split(attribute); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
402 |
DynamicFileAttributeView view = getFileAttributeView(s[0], options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
403 |
return (view == null) ? null : view.getAttribute(s[1]); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
404 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
405 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
406 |
@Override |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
407 |
public final Map<String,?> readAttributes(String attributes, LinkOption... options) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
408 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
409 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
410 |
String[] s = split(attributes); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
411 |
DynamicFileAttributeView view = getFileAttributeView(s[0], options); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
412 |
if (view == null) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
413 |
return Collections.emptyMap(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
414 |
return view.readAttributes(s[1].split(",")); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
415 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
416 |
} |