1 /* 2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 33 package com.sun.nio.zipfs; 34 35 import java.nio.file.attribute.*; 36 import java.io.IOException; 37 import java.util.LinkedHashMap; 38 import java.util.Map; 39 40 /* 41 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal 42 */ 43 44 public class ZipFileAttributeView implements BasicFileAttributeView 45 { 46 private static enum AttrID { 47 size, 48 creationTime, 49 lastAccessTime, 50 lastModifiedTime, 51 isDirectory, 52 isRegularFile, 53 isSymbolicLink, 54 isOther, 55 fileKey, 56 compressedSize, 57 crc, 58 method 59 }; 60 61 private final ZipPath path; 62 private final boolean isZipView; 63 64 private ZipFileAttributeView(ZipPath path, boolean isZipView) { 65 this.path = path; 66 this.isZipView = isZipView; 67 } 68 69 static <V extends FileAttributeView> V get(ZipPath path, Class<V> type) { 70 if (type == null) 71 throw new NullPointerException(); 72 if (type == BasicFileAttributeView.class) 73 return (V)new ZipFileAttributeView(path, false); 74 if (type == ZipFileAttributeView.class) 75 return (V)new ZipFileAttributeView(path, true); 76 return null; 77 } 78 79 static ZipFileAttributeView get(ZipPath path, String type) { 80 if (type == null) 81 throw new NullPointerException(); 82 if (type.equals("basic")) 83 return new ZipFileAttributeView(path, false); 84 if (type.equals("zip")) 85 return new ZipFileAttributeView(path, true); 86 return null; 87 } 88 89 @Override 90 public String name() { 91 return isZipView ? "zip" : "basic"; 92 } 93 94 public ZipFileAttributes readAttributes() throws IOException 95 { 96 return path.getAttributes(); 97 } 98 99 @Override 100 public void setTimes(FileTime lastModifiedTime, 101 FileTime lastAccessTime, 102 FileTime createTime) 103 throws IOException 104 { 105 path.setTimes(lastModifiedTime, lastAccessTime, createTime); 106 } 107 108 void setAttribute(String attribute, Object value) 109 throws IOException 110 { 111 try { 112 if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime) 113 setTimes ((FileTime)value, null, null); 114 if (AttrID.valueOf(attribute) == AttrID.lastAccessTime) 115 setTimes (null, (FileTime)value, null); 116 if (AttrID.valueOf(attribute) == AttrID.creationTime) 117 setTimes (null, null, (FileTime)value); 118 return; 119 } catch (IllegalArgumentException x) {} 120 throw new UnsupportedOperationException("'" + attribute + 121 "' is unknown or read-only attribute"); 122 } 123 124 Map<String, Object> readAttributes(String attributes) 125 throws IOException 126 { 127 ZipFileAttributes zfas = readAttributes(); 128 LinkedHashMap<String, Object> map = new LinkedHashMap<>(); 129 if ("*".equals(attributes)) { 130 for (AttrID id : AttrID.values()) { 131 try { 132 map.put(id.name(), attribute(id, zfas)); 133 } catch (IllegalArgumentException x) {} 134 } 135 } else { 136 String[] as = attributes.split(","); 137 for (String a : as) { 138 try { 139 map.put(a, attribute(AttrID.valueOf(a), zfas)); 140 } catch (IllegalArgumentException x) {} 141 } 142 } 143 return map; 144 } 145 146 Object attribute(AttrID id, ZipFileAttributes zfas) { 147 switch (id) { 148 case size: 149 return zfas.size(); 150 case creationTime: 151 return zfas.creationTime(); 152 case lastAccessTime: 153 return zfas.lastAccessTime(); 154 case lastModifiedTime: 155 return zfas.lastModifiedTime(); 156 case isDirectory: 157 return zfas.isDirectory(); 158 case isRegularFile: 159 return zfas.isRegularFile(); 160 case isSymbolicLink: 161 return zfas.isSymbolicLink(); 162 case isOther: 163 return zfas.isOther(); 164 case fileKey: 165 return zfas.fileKey(); 166 case compressedSize: 167 if (isZipView) 168 return zfas.compressedSize(); 169 break; 170 case crc: 171 if (isZipView) 172 return zfas.crc(); 173 break; 174 case method: 175 if (isZipView) 176 return zfas.method(); 177 break; 178 } 179 return null; 180 } 181 }