jdk/src/share/demo/nio/zipfs/com/sun/nio/zipfs/ZipFileStore.java
changeset 7694 68672dc4d96f
parent 7691 ec07a04e74e7
parent 7646 142129d8599d
child 7695 e15276b5c5cc
equal deleted inserted replaced
7691:ec07a04e74e7 7694:68672dc4d96f
     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 package com.sun.nio.zipfs;
       
    33 
       
    34 import java.io.IOException;
       
    35 import java.nio.file.FileStore;
       
    36 import java.nio.file.FileSystems;
       
    37 import java.nio.file.Path;
       
    38 import java.nio.file.attribute.FileAttributeView;
       
    39 import java.nio.file.attribute.FileStoreAttributeView;
       
    40 import java.nio.file.attribute.FileStoreSpaceAttributeView;
       
    41 import java.nio.file.attribute.FileStoreSpaceAttributes;
       
    42 import java.nio.file.attribute.Attributes;
       
    43 import java.nio.file.attribute.BasicFileAttributeView;
       
    44 import java.util.Formatter;
       
    45 
       
    46 /*
       
    47  *
       
    48  * @author  Xueming Shen, Rajendra Gutupalli, Jaya Hangal
       
    49  */
       
    50 
       
    51 public class ZipFileStore extends FileStore {
       
    52 
       
    53     private final ZipFileSystem zfs;
       
    54 
       
    55     ZipFileStore(ZipPath zpath) {
       
    56         this.zfs = (ZipFileSystem)zpath.getFileSystem();
       
    57     }
       
    58 
       
    59     @Override
       
    60     public String name() {
       
    61         return zfs.toString() + "/";
       
    62     }
       
    63 
       
    64     @Override
       
    65     public String type() {
       
    66         return "zipfs";
       
    67     }
       
    68 
       
    69     @Override
       
    70     public boolean isReadOnly() {
       
    71         return zfs.isReadOnly();
       
    72     }
       
    73 
       
    74     @Override
       
    75     public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
       
    76         return (type == BasicFileAttributeView.class ||
       
    77                 type == ZipFileAttributeView.class);
       
    78     }
       
    79 
       
    80     @Override
       
    81     public boolean supportsFileAttributeView(String name) {
       
    82         return name.equals("basic") || name.equals("zip");
       
    83     }
       
    84 
       
    85     @Override
       
    86     @SuppressWarnings("unchecked")
       
    87     public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
       
    88         if (type == null)
       
    89             throw new NullPointerException();
       
    90         if (type == FileStoreSpaceAttributeView.class)
       
    91             return (V) new ZipFileStoreAttributeView(this);
       
    92         return null;
       
    93     }
       
    94 
       
    95     @Override
       
    96     public Object getAttribute(String attribute) throws IOException {
       
    97          if (attribute.equals("space:totalSpace"))
       
    98                return new ZipFileStoreAttributeView(this).readAttributes().totalSpace();
       
    99          if (attribute.equals("space:usableSpace"))
       
   100                return new ZipFileStoreAttributeView(this).readAttributes().usableSpace();
       
   101          if (attribute.equals("space:unallocatedSpace"))
       
   102                return new ZipFileStoreAttributeView(this).readAttributes().unallocatedSpace();
       
   103          throw new UnsupportedOperationException("does not support the given attribute");
       
   104     }
       
   105 
       
   106     private static class ZipFileStoreAttributeView implements FileStoreSpaceAttributeView {
       
   107 
       
   108         private final ZipFileStore fileStore;
       
   109 
       
   110         public ZipFileStoreAttributeView(ZipFileStore fileStore) {
       
   111             this.fileStore = fileStore;
       
   112         }
       
   113 
       
   114         @Override
       
   115         public String name() {
       
   116             return "space";
       
   117         }
       
   118 
       
   119         @Override
       
   120         public FileStoreSpaceAttributes readAttributes() throws IOException {
       
   121             final String file = fileStore.name();
       
   122             Path path = FileSystems.getDefault().getPath(file);
       
   123             final long size = Attributes.readBasicFileAttributes(path).size();
       
   124             final FileStore fstore = path.getFileStore();
       
   125             final FileStoreSpaceAttributes fstoreAttrs =
       
   126                 Attributes.readFileStoreSpaceAttributes(fstore);
       
   127             return new FileStoreSpaceAttributes() {
       
   128                 public long totalSpace() {
       
   129                     return size;
       
   130                 }
       
   131 
       
   132                 public long usableSpace() {
       
   133                     if (!fstore.isReadOnly())
       
   134                         return fstoreAttrs.usableSpace();
       
   135                     return 0;
       
   136                 }
       
   137 
       
   138                 public long unallocatedSpace() {
       
   139                     if (!fstore.isReadOnly())
       
   140                         return fstoreAttrs.unallocatedSpace();
       
   141                     return 0;
       
   142                 }
       
   143 
       
   144                 public String toString() {
       
   145                     StringBuilder sb = new StringBuilder();
       
   146                     Formatter fm = new Formatter(sb);
       
   147                     fm.format("FileStoreSpaceAttributes[%s]%n", file);
       
   148                     fm.format("      totalSpace: %d%n", totalSpace());
       
   149                     fm.format("     usableSpace: %d%n", usableSpace());
       
   150                     fm.format("    unallocSpace: %d%n", unallocatedSpace());
       
   151                     fm.close();
       
   152                     return sb.toString();
       
   153                 }
       
   154             };
       
   155         }
       
   156     }
       
   157 }