jdk/src/share/demo/nio/zipfs/src/com/sun/nio/zipfs/ZipFileAttributes.java
changeset 24414 2061862eb57c
parent 24413 1d117d2dfe92
parent 24077 0809c9a4d36e
child 24415 43aa54df554d
equal deleted inserted replaced
24413:1d117d2dfe92 24414:2061862eb57c
     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.BasicFileAttributes;
       
    45 import java.nio.file.attribute.FileTime;
       
    46 import java.util.Arrays;
       
    47 import java.util.Formatter;
       
    48 import static com.sun.nio.zipfs.ZipUtils.*;
       
    49 
       
    50 /**
       
    51  *
       
    52  * @author  Xueming Shen, Rajendra Gutupalli,Jaya Hangal
       
    53  */
       
    54 
       
    55 public class ZipFileAttributes implements BasicFileAttributes
       
    56 
       
    57 {
       
    58     private final ZipFileSystem.Entry e;
       
    59 
       
    60     ZipFileAttributes(ZipFileSystem.Entry e) {
       
    61         this.e = e;
       
    62     }
       
    63 
       
    64     ///////// basic attributes ///////////
       
    65     @Override
       
    66     public FileTime creationTime() {
       
    67         if (e.ctime != -1)
       
    68             return FileTime.fromMillis(e.ctime);
       
    69         return null;
       
    70     }
       
    71 
       
    72     @Override
       
    73     public boolean isDirectory() {
       
    74         return e.isDir();
       
    75     }
       
    76 
       
    77     @Override
       
    78     public boolean isOther() {
       
    79         return false;
       
    80     }
       
    81 
       
    82     @Override
       
    83     public boolean isRegularFile() {
       
    84         return !e.isDir();
       
    85     }
       
    86 
       
    87     @Override
       
    88     public FileTime lastAccessTime() {
       
    89         if (e.atime != -1)
       
    90             return FileTime.fromMillis(e.atime);
       
    91         return null;
       
    92     }
       
    93 
       
    94     @Override
       
    95     public FileTime lastModifiedTime() {
       
    96         return FileTime.fromMillis(e.mtime);
       
    97     }
       
    98 
       
    99     @Override
       
   100     public long size() {
       
   101         return e.size;
       
   102     }
       
   103 
       
   104     @Override
       
   105     public boolean isSymbolicLink() {
       
   106         return false;
       
   107     }
       
   108 
       
   109     @Override
       
   110     public Object fileKey() {
       
   111         return null;
       
   112     }
       
   113 
       
   114     ///////// zip entry attributes ///////////
       
   115     public long compressedSize() {
       
   116         return e.csize;
       
   117     }
       
   118 
       
   119     public long crc() {
       
   120         return e.crc;
       
   121     }
       
   122 
       
   123     public int method() {
       
   124         return e.method;
       
   125     }
       
   126 
       
   127     public byte[] extra() {
       
   128         if (e.extra != null)
       
   129             return Arrays.copyOf(e.extra, e.extra.length);
       
   130         return null;
       
   131     }
       
   132 
       
   133     public byte[] comment() {
       
   134         if (e.comment != null)
       
   135             return Arrays.copyOf(e.comment, e.comment.length);
       
   136         return null;
       
   137     }
       
   138 
       
   139     public String toString() {
       
   140         StringBuilder sb = new StringBuilder(1024);
       
   141         Formatter fm = new Formatter(sb);
       
   142         if (creationTime() != null)
       
   143             fm.format("    creationTime    : %tc%n", creationTime().toMillis());
       
   144         else
       
   145             fm.format("    creationTime    : null%n");
       
   146 
       
   147         if (lastAccessTime() != null)
       
   148             fm.format("    lastAccessTime  : %tc%n", lastAccessTime().toMillis());
       
   149         else
       
   150             fm.format("    lastAccessTime  : null%n");
       
   151         fm.format("    lastModifiedTime: %tc%n", lastModifiedTime().toMillis());
       
   152         fm.format("    isRegularFile   : %b%n", isRegularFile());
       
   153         fm.format("    isDirectory     : %b%n", isDirectory());
       
   154         fm.format("    isSymbolicLink  : %b%n", isSymbolicLink());
       
   155         fm.format("    isOther         : %b%n", isOther());
       
   156         fm.format("    fileKey         : %s%n", fileKey());
       
   157         fm.format("    size            : %d%n", size());
       
   158         fm.format("    compressedSize  : %d%n", compressedSize());
       
   159         fm.format("    crc             : %x%n", crc());
       
   160         fm.format("    method          : %d%n", method());
       
   161         fm.close();
       
   162         return sb.toString();
       
   163     }
       
   164 }