jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileAttributeView.java
changeset 23977 cb2cf6e2958a
parent 23976 16ec7c58cdea
parent 23964 65b2502dbd2d
child 23978 8c0bdeecd7c0
equal deleted inserted replaced
23976:16ec7c58cdea 23977:cb2cf6e2958a
     1 /*
       
     2  * Copyright (c) 2009, 2011, 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  * This source code is provided to illustrate the usage of a given feature
       
    34  * or technique and has been deliberately simplified. Additional steps
       
    35  * required for a production-quality application, such as security checks,
       
    36  * input validation and proper error handling, might not be present in
       
    37  * this sample code.
       
    38  */
       
    39 
       
    40 
       
    41 
       
    42 package com.sun.nio.zipfs;
       
    43 
       
    44 import java.nio.file.attribute.*;
       
    45 import java.io.IOException;
       
    46 import java.util.LinkedHashMap;
       
    47 import java.util.Map;
       
    48 
       
    49 /*
       
    50  * @author  Xueming Shen, Rajendra Gutupalli, Jaya Hangal
       
    51  */
       
    52 
       
    53 public class ZipFileAttributeView implements BasicFileAttributeView
       
    54 {
       
    55     private static enum AttrID {
       
    56         size,
       
    57         creationTime,
       
    58         lastAccessTime,
       
    59         lastModifiedTime,
       
    60         isDirectory,
       
    61         isRegularFile,
       
    62         isSymbolicLink,
       
    63         isOther,
       
    64         fileKey,
       
    65         compressedSize,
       
    66         crc,
       
    67         method
       
    68     };
       
    69 
       
    70     private final ZipPath path;
       
    71     private final boolean isZipView;
       
    72 
       
    73     private ZipFileAttributeView(ZipPath path, boolean isZipView) {
       
    74         this.path = path;
       
    75         this.isZipView = isZipView;
       
    76     }
       
    77 
       
    78     static <V extends FileAttributeView> V get(ZipPath path, Class<V> type) {
       
    79         if (type == null)
       
    80             throw new NullPointerException();
       
    81         if (type == BasicFileAttributeView.class)
       
    82             return (V)new ZipFileAttributeView(path, false);
       
    83         if (type == ZipFileAttributeView.class)
       
    84             return (V)new ZipFileAttributeView(path, true);
       
    85         return null;
       
    86     }
       
    87 
       
    88     static ZipFileAttributeView get(ZipPath path, String type) {
       
    89         if (type == null)
       
    90             throw new NullPointerException();
       
    91         if (type.equals("basic"))
       
    92             return new ZipFileAttributeView(path, false);
       
    93         if (type.equals("zip"))
       
    94             return new ZipFileAttributeView(path, true);
       
    95         return null;
       
    96     }
       
    97 
       
    98     @Override
       
    99     public String name() {
       
   100         return isZipView ? "zip" : "basic";
       
   101     }
       
   102 
       
   103     public ZipFileAttributes readAttributes() throws IOException
       
   104     {
       
   105         return path.getAttributes();
       
   106     }
       
   107 
       
   108     @Override
       
   109     public void setTimes(FileTime lastModifiedTime,
       
   110                          FileTime lastAccessTime,
       
   111                          FileTime createTime)
       
   112         throws IOException
       
   113     {
       
   114         path.setTimes(lastModifiedTime, lastAccessTime, createTime);
       
   115     }
       
   116 
       
   117     void setAttribute(String attribute, Object value)
       
   118         throws IOException
       
   119     {
       
   120         try {
       
   121             if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime)
       
   122                 setTimes ((FileTime)value, null, null);
       
   123             if (AttrID.valueOf(attribute) == AttrID.lastAccessTime)
       
   124                 setTimes (null, (FileTime)value, null);
       
   125             if (AttrID.valueOf(attribute) == AttrID.creationTime)
       
   126                 setTimes (null, null, (FileTime)value);
       
   127             return;
       
   128         } catch (IllegalArgumentException x) {}
       
   129         throw new UnsupportedOperationException("'" + attribute +
       
   130             "' is unknown or read-only attribute");
       
   131     }
       
   132 
       
   133     Map<String, Object> readAttributes(String attributes)
       
   134         throws IOException
       
   135     {
       
   136         ZipFileAttributes zfas = readAttributes();
       
   137         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
       
   138         if ("*".equals(attributes)) {
       
   139             for (AttrID id : AttrID.values()) {
       
   140                 try {
       
   141                     map.put(id.name(), attribute(id, zfas));
       
   142                 } catch (IllegalArgumentException x) {}
       
   143             }
       
   144         } else {
       
   145             String[] as = attributes.split(",");
       
   146             for (String a : as) {
       
   147                 try {
       
   148                     map.put(a, attribute(AttrID.valueOf(a), zfas));
       
   149                 } catch (IllegalArgumentException x) {}
       
   150             }
       
   151         }
       
   152         return map;
       
   153     }
       
   154 
       
   155     Object attribute(AttrID id, ZipFileAttributes zfas) {
       
   156         switch (id) {
       
   157         case size:
       
   158             return zfas.size();
       
   159         case creationTime:
       
   160             return zfas.creationTime();
       
   161         case lastAccessTime:
       
   162             return zfas.lastAccessTime();
       
   163         case lastModifiedTime:
       
   164             return zfas.lastModifiedTime();
       
   165         case isDirectory:
       
   166             return zfas.isDirectory();
       
   167         case isRegularFile:
       
   168             return zfas.isRegularFile();
       
   169         case isSymbolicLink:
       
   170             return zfas.isSymbolicLink();
       
   171         case isOther:
       
   172             return zfas.isOther();
       
   173         case fileKey:
       
   174             return zfas.fileKey();
       
   175         case compressedSize:
       
   176             if (isZipView)
       
   177                 return zfas.compressedSize();
       
   178             break;
       
   179         case crc:
       
   180             if (isZipView)
       
   181                 return zfas.crc();
       
   182             break;
       
   183         case method:
       
   184             if (isZipView)
       
   185                 return zfas.method();
       
   186             break;
       
   187         }
       
   188         return null;
       
   189     }
       
   190 }