1 /*
   2  * Copyright (c) 2009, 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 
  26 package jdk.nio.zipfs;
  27 
  28 import java.nio.file.attribute.BasicFileAttributes;
  29 import java.nio.file.attribute.FileTime;
  30 import java.util.Arrays;
  31 import java.util.Formatter;
  32 import static jdk.nio.zipfs.ZipUtils.*;
  33 
  34 /**
  35  *
  36  * @author  Xueming Shen, Rajendra Gutupalli,Jaya Hangal
  37  */
  38 
  39 class ZipFileAttributes implements BasicFileAttributes
  40 {
  41     private final ZipFileSystem.Entry e;
  42 
  43     ZipFileAttributes(ZipFileSystem.Entry e) {
  44         this.e = e;
  45     }
  46 
  47     ///////// basic attributes ///////////
  48     @Override
  49     public FileTime creationTime() {
  50         if (e.ctime != -1)
  51             return FileTime.fromMillis(e.ctime);
  52         return null;
  53     }
  54 
  55     @Override
  56     public boolean isDirectory() {
  57         return e.isDir();
  58     }
  59 
  60     @Override
  61     public boolean isOther() {
  62         return false;
  63     }
  64 
  65     @Override
  66     public boolean isRegularFile() {
  67         return !e.isDir();
  68     }
  69 
  70     @Override
  71     public FileTime lastAccessTime() {
  72         if (e.atime != -1)
  73             return FileTime.fromMillis(e.atime);
  74         return null;
  75     }
  76 
  77     @Override
  78     public FileTime lastModifiedTime() {
  79         return FileTime.fromMillis(e.mtime);
  80     }
  81 
  82     @Override
  83     public long size() {
  84         return e.size;
  85     }
  86 
  87     @Override
  88     public boolean isSymbolicLink() {
  89         return false;
  90     }
  91 
  92     @Override
  93     public Object fileKey() {
  94         return null;
  95     }
  96 
  97     ///////// zip entry attributes ///////////
  98     public long compressedSize() {
  99         return e.csize;
 100     }
 101 
 102     public long crc() {
 103         return e.crc;
 104     }
 105 
 106     public int method() {
 107         return e.method;
 108     }
 109 
 110     public byte[] extra() {
 111         if (e.extra != null)
 112             return Arrays.copyOf(e.extra, e.extra.length);
 113         return null;
 114     }
 115 
 116     public byte[] comment() {
 117         if (e.comment != null)
 118             return Arrays.copyOf(e.comment, e.comment.length);
 119         return null;
 120     }
 121 
 122     public String toString() {
 123         StringBuilder sb = new StringBuilder(1024);
 124         Formatter fm = new Formatter(sb);
 125         if (creationTime() != null)
 126             fm.format("    creationTime    : %tc%n", creationTime().toMillis());
 127         else
 128             fm.format("    creationTime    : null%n");
 129 
 130         if (lastAccessTime() != null)
 131             fm.format("    lastAccessTime  : %tc%n", lastAccessTime().toMillis());
 132         else
 133             fm.format("    lastAccessTime  : null%n");
 134         fm.format("    lastModifiedTime: %tc%n", lastModifiedTime().toMillis());
 135         fm.format("    isRegularFile   : %b%n", isRegularFile());
 136         fm.format("    isDirectory     : %b%n", isDirectory());
 137         fm.format("    isSymbolicLink  : %b%n", isSymbolicLink());
 138         fm.format("    isOther         : %b%n", isOther());
 139         fm.format("    fileKey         : %s%n", fileKey());
 140         fm.format("    size            : %d%n", size());
 141         fm.format("    compressedSize  : %d%n", compressedSize());
 142         fm.format("    crc             : %x%n", crc());
 143         fm.format("    method          : %d%n", method());
 144         fm.close();
 145         return sb.toString();
 146     }
 147 }