JrtFileAttributeView.java revision 10967:e336cbd8b15e
1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
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
26package jdk.internal.jrtfs;
27
28import java.nio.file.attribute.*;
29import java.io.IOException;
30import java.util.LinkedHashMap;
31import java.util.Map;
32
33final class JrtFileAttributeView implements BasicFileAttributeView
34{
35    private static enum AttrID {
36        size,
37        creationTime,
38        lastAccessTime,
39        lastModifiedTime,
40        isDirectory,
41        isRegularFile,
42        isSymbolicLink,
43        isOther,
44        fileKey,
45        compressedSize,
46        extension
47    };
48
49    private final JrtPath path;
50    private final boolean isJrtView;
51
52    private JrtFileAttributeView(JrtPath path, boolean isJrtView) {
53        this.path = path;
54        this.isJrtView = isJrtView;
55    }
56
57    @SuppressWarnings("unchecked") // Cast to V
58    static <V extends FileAttributeView> V get(JrtPath path, Class<V> type) {
59        if (type == null)
60            throw new NullPointerException();
61        if (type == BasicFileAttributeView.class)
62            return (V)new JrtFileAttributeView(path, false);
63        if (type == JrtFileAttributeView.class)
64            return (V)new JrtFileAttributeView(path, true);
65        return null;
66    }
67
68    static JrtFileAttributeView get(JrtPath path, String type) {
69        if (type == null)
70            throw new NullPointerException();
71        if (type.equals("basic"))
72            return new JrtFileAttributeView(path, false);
73        if (type.equals("jjrt"))
74            return new JrtFileAttributeView(path, true);
75        return null;
76    }
77
78    @Override
79    public String name() {
80        return isJrtView ? "jjrt" : "basic";
81    }
82
83    @Override
84    public JrtFileAttributes readAttributes() throws IOException
85    {
86        return path.getAttributes();
87    }
88
89    @Override
90    public void setTimes(FileTime lastModifiedTime,
91                         FileTime lastAccessTime,
92                         FileTime createTime)
93        throws IOException
94    {
95        path.setTimes(lastModifiedTime, lastAccessTime, createTime);
96    }
97
98    void setAttribute(String attribute, Object value)
99        throws IOException
100    {
101        try {
102            if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime)
103                setTimes ((FileTime)value, null, null);
104            if (AttrID.valueOf(attribute) == AttrID.lastAccessTime)
105                setTimes (null, (FileTime)value, null);
106            if (AttrID.valueOf(attribute) == AttrID.creationTime)
107                setTimes (null, null, (FileTime)value);
108            return;
109        } catch (IllegalArgumentException x) {}
110        throw new UnsupportedOperationException("'" + attribute +
111            "' is unknown or read-only attribute");
112    }
113
114    Map<String, Object> readAttributes(String attributes)
115        throws IOException
116    {
117        JrtFileAttributes jrtfas = readAttributes();
118        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
119        if ("*".equals(attributes)) {
120            for (AttrID id : AttrID.values()) {
121                try {
122                    map.put(id.name(), attribute(id, jrtfas));
123                } catch (IllegalArgumentException x) {}
124            }
125        } else {
126            String[] as = attributes.split(",");
127            for (String a : as) {
128                try {
129                    map.put(a, attribute(AttrID.valueOf(a), jrtfas));
130                } catch (IllegalArgumentException x) {}
131            }
132        }
133        return map;
134    }
135
136    Object attribute(AttrID id, JrtFileAttributes jrtfas) {
137        switch (id) {
138        case size:
139            return jrtfas.size();
140        case creationTime:
141            return jrtfas.creationTime();
142        case lastAccessTime:
143            return jrtfas.lastAccessTime();
144        case lastModifiedTime:
145            return jrtfas.lastModifiedTime();
146        case isDirectory:
147            return jrtfas.isDirectory();
148        case isRegularFile:
149            return jrtfas.isRegularFile();
150        case isSymbolicLink:
151            return jrtfas.isSymbolicLink();
152        case isOther:
153            return jrtfas.isOther();
154        case fileKey:
155            return jrtfas.fileKey();
156        case compressedSize:
157            if (isJrtView)
158                return jrtfas.compressedSize();
159            break;
160        case extension:
161            if (isJrtView) {
162                return jrtfas.extension();
163            }
164            break;
165        }
166        return null;
167    }
168}
169