12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.IOException;
29 import java.nio.file.attribute.BasicFileAttributeView;
30 import java.nio.file.attribute.FileAttributeView;
31 import java.nio.file.attribute.FileTime;
32 import java.util.LinkedHashMap;
33 import java.util.Map;
34
35 /**
36 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
37 */
38 class ZipFileAttributeView implements BasicFileAttributeView {
39 private static enum AttrID {
40 size,
41 creationTime,
42 lastAccessTime,
43 lastModifiedTime,
44 isDirectory,
45 isRegularFile,
46 isSymbolicLink,
47 isOther,
48 fileKey,
49 compressedSize,
50 crc,
51 method
52 };
53
54 private final ZipPath path;
55 private final boolean isZipView;
56
57 private ZipFileAttributeView(ZipPath path, boolean isZipView) {
58 this.path = path;
59 this.isZipView = isZipView;
60 }
61
62 @SuppressWarnings("unchecked") // Cast to V
63 static <V extends FileAttributeView> V get(ZipPath path, Class<V> type) {
64 if (type == null)
65 throw new NullPointerException();
66 if (type == BasicFileAttributeView.class)
67 return (V)new ZipFileAttributeView(path, false);
68 if (type == ZipFileAttributeView.class)
69 return (V)new ZipFileAttributeView(path, true);
70 return null;
71 }
72
73 static ZipFileAttributeView get(ZipPath path, String type) {
74 if (type == null)
75 throw new NullPointerException();
76 if (type.equals("basic"))
77 return new ZipFileAttributeView(path, false);
78 if (type.equals("zip"))
79 return new ZipFileAttributeView(path, true);
80 return null;
81 }
82
83 @Override
84 public String name() {
85 return isZipView ? "zip" : "basic";
86 }
87
88 public ZipFileAttributes readAttributes() throws IOException {
89 return path.getAttributes();
90 }
91
92 @Override
93 public void setTimes(FileTime lastModifiedTime,
94 FileTime lastAccessTime,
95 FileTime createTime)
96 throws IOException
97 {
98 path.setTimes(lastModifiedTime, lastAccessTime, createTime);
99 }
100
101 void setAttribute(String attribute, Object value)
102 throws IOException
103 {
104 try {
105 if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime)
106 setTimes((FileTime)value, null, null);
107 if (AttrID.valueOf(attribute) == AttrID.lastAccessTime)
108 setTimes(null, (FileTime)value, null);
109 if (AttrID.valueOf(attribute) == AttrID.creationTime)
110 setTimes(null, null, (FileTime)value);
111 return;
112 } catch (IllegalArgumentException x) {}
113 throw new UnsupportedOperationException("'" + attribute +
114 "' is unknown or read-only attribute");
115 }
116
117 Map<String, Object> readAttributes(String attributes)
118 throws IOException
119 {
120 ZipFileAttributes zfas = readAttributes();
121 LinkedHashMap<String, Object> map = new LinkedHashMap<>();
122 if ("*".equals(attributes)) {
123 for (AttrID id : AttrID.values()) {
124 try {
125 map.put(id.name(), attribute(id, zfas));
126 } catch (IllegalArgumentException x) {}
127 }
128 } else {
129 String[] as = attributes.split(",");
130 for (String a : as) {
131 try {
132 map.put(a, attribute(AttrID.valueOf(a), zfas));
133 } catch (IllegalArgumentException x) {}
134 }
135 }
136 return map;
137 }
138
139 Object attribute(AttrID id, ZipFileAttributes zfas) {
140 switch (id) {
141 case size:
142 return zfas.size();
143 case creationTime:
144 return zfas.creationTime();
145 case lastAccessTime:
146 return zfas.lastAccessTime();
147 case lastModifiedTime:
148 return zfas.lastModifiedTime();
149 case isDirectory:
150 return zfas.isDirectory();
151 case isRegularFile:
152 return zfas.isRegularFile();
153 case isSymbolicLink:
154 return zfas.isSymbolicLink();
155 case isOther:
156 return zfas.isOther();
157 case fileKey:
158 return zfas.fileKey();
159 case compressedSize:
160 if (isZipView)
161 return zfas.compressedSize();
162 break;
163 case crc:
164 if (isZipView)
165 return zfas.crc();
166 break;
167 case method:
168 if (isZipView)
169 return zfas.method();
170 break;
171 }
172 return null;
173 }
174 }
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nio.zipfs;
27
28 import java.io.IOException;
29 import java.nio.file.attribute.BasicFileAttributeView;
30 import java.nio.file.attribute.FileAttributeView;
31 import java.nio.file.attribute.FileTime;
32 import java.nio.file.attribute.GroupPrincipal;
33 import java.nio.file.attribute.PosixFileAttributeView;
34 import java.nio.file.attribute.PosixFilePermission;
35 import java.nio.file.attribute.UserPrincipal;
36 import java.util.LinkedHashMap;
37 import java.util.Map;
38 import java.util.Set;
39
40 /**
41 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
42 */
43 class ZipFileAttributeView implements PosixFileAttributeView {
44 private static enum AttrID {
45 size,
46 creationTime,
47 lastAccessTime,
48 lastModifiedTime,
49 isDirectory,
50 isRegularFile,
51 isSymbolicLink,
52 isOther,
53 fileKey,
54 compressedSize,
55 crc,
56 method,
57 permissions
58 };
59
60 private static enum ViewType {
61 zip,
62 posix,
63 basic
64 }
65
66 private final ZipPath path;
67 private final ViewType type;
68
69 private ZipFileAttributeView(ZipPath path, ViewType type) {
70 this.path = path;
71 this.type = type;
72 }
73
74 @SuppressWarnings("unchecked") // Cast to V
75 static <V extends FileAttributeView> V get(ZipPath path, Class<V> type) {
76 if (type == null)
77 throw new NullPointerException();
78 if (type == BasicFileAttributeView.class)
79 return (V)new ZipFileAttributeView(path, ViewType.basic);
80 if (type == PosixFileAttributeView.class)
81 return (V)new ZipFileAttributeView(path, ViewType.posix);
82 if (type == ZipFileAttributeView.class)
83 return (V)new ZipFileAttributeView(path, ViewType.zip);
84 return null;
85 }
86
87 static ZipFileAttributeView get(ZipPath path, String type) {
88 if (type == null)
89 throw new NullPointerException();
90 if (type.equals("basic"))
91 return new ZipFileAttributeView(path, ViewType.basic);
92 if (type.equals("posix"))
93 return new ZipFileAttributeView(path, ViewType.posix);
94 if (type.equals("zip"))
95 return new ZipFileAttributeView(path, ViewType.zip);
96 return null;
97 }
98
99 @Override
100 public String name() {
101 switch (type) {
102 case zip:
103 return "zip";
104 case posix:
105 return "posix";
106 case basic:
107 default:
108 return "basic";
109 }
110 }
111
112 public ZipFileAttributes readAttributes() throws IOException {
113 return path.getAttributes();
114 }
115
116 @Override
117 public void setTimes(FileTime lastModifiedTime,
118 FileTime lastAccessTime,
119 FileTime createTime)
120 throws IOException
121 {
122 path.setTimes(lastModifiedTime, lastAccessTime, createTime);
123 }
124
125 @Override
126 public UserPrincipal getOwner() throws IOException {
127 throw new UnsupportedOperationException("ZipFileSystem does not support getOwner.");
128 }
129
130 @Override
131 public void setOwner(UserPrincipal owner) throws IOException {
132 throw new UnsupportedOperationException("ZipFileSystem does not support setOwner.");
133 }
134
135 @Override
136 public void setPermissions(Set<PosixFilePermission> perms) throws IOException {
137 path.setPermissions(perms);
138 }
139
140 @Override
141 public void setGroup(GroupPrincipal group) throws IOException {
142 throw new UnsupportedOperationException("ZipFileSystem does not support setGroup.");
143 }
144
145 @SuppressWarnings("unchecked")
146 void setAttribute(String attribute, Object value)
147 throws IOException
148 {
149 try {
150 if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime)
151 setTimes((FileTime)value, null, null);
152 if (AttrID.valueOf(attribute) == AttrID.lastAccessTime)
153 setTimes(null, (FileTime)value, null);
154 if (AttrID.valueOf(attribute) == AttrID.creationTime)
155 setTimes(null, null, (FileTime)value);
156 if (AttrID.valueOf(attribute) == AttrID.permissions)
157 setPermissions((Set<PosixFilePermission>)value);
158 return;
159 } catch (IllegalArgumentException x) {
160 throw new UnsupportedOperationException("'" + attribute +
161 "' is unknown or read-only attribute");
162 }
163 }
164
165 Map<String, Object> readAttributes(String attributes)
166 throws IOException
167 {
168 ZipFileAttributes zfas = readAttributes();
169 LinkedHashMap<String, Object> map = new LinkedHashMap<>();
170 if ("*".equals(attributes)) {
171 for (AttrID id : AttrID.values()) {
172 try {
173 map.put(id.name(), attribute(id, zfas));
174 } catch (IllegalArgumentException x) {}
175 }
176 } else {
177 String[] as = attributes.split(",");
178 for (String a : as) {
179 try {
180 map.put(a, attribute(AttrID.valueOf(a), zfas));
181 } catch (IllegalArgumentException x) {}
182 }
183 }
184 return map;
185 }
186
187 private Object attribute(AttrID id, ZipFileAttributes zfas) {
188 switch (id) {
189 case size:
190 return zfas.size();
191 case creationTime:
192 return zfas.creationTime();
193 case lastAccessTime:
194 return zfas.lastAccessTime();
195 case lastModifiedTime:
196 return zfas.lastModifiedTime();
197 case isDirectory:
198 return zfas.isDirectory();
199 case isRegularFile:
200 return zfas.isRegularFile();
201 case isSymbolicLink:
202 return zfas.isSymbolicLink();
203 case isOther:
204 return zfas.isOther();
205 case fileKey:
206 return zfas.fileKey();
207 case permissions:
208 if (type == ViewType.posix || type == ViewType.zip) {
209 try {
210 return zfas.permissions();
211 } catch (UnsupportedOperationException e) {
212 return null;
213 }
214 }
215 break;
216 case compressedSize:
217 if (type == ViewType.zip) {
218 return zfas.compressedSize();
219 }
220 break;
221 case crc:
222 if (type == ViewType.zip) {
223 return zfas.crc();
224 }
225 break;
226 case method:
227 if (type == ViewType.zip) {
228 return zfas.method();
229 }
230 break;
231 }
232 return null;
233 }
234 }
|