1 /*
   2  * Copyright (c) 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 sun.tools.java;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.UncheckedIOException;
  31 import java.nio.file.attribute.BasicFileAttributes;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 
  35 /**
  36  * This class is used to represent a file loaded from the class path, and
  37  * is represented by nio Path.
  38  *
  39  * WARNING: The contents of this source file are not part of any
  40  * supported API.  Code that depends on them does so at its own risk:
  41  * they are subject to change or removal without notice.
  42  */
  43 final
  44 class PathClassFile extends ClassFile {
  45     private final Path path;
  46     private final BasicFileAttributes attrs;
  47 
  48     /**
  49      * Constructor for instance representing a Path
  50      */
  51     public PathClassFile(Path path) {
  52         this.path = path;
  53         try {
  54             this.attrs = Files.readAttributes(path, BasicFileAttributes.class);
  55         } catch (IOException ioExp) {
  56             throw new UncheckedIOException(ioExp);
  57         }
  58     }
  59 
  60     @Override
  61     public boolean isZipped() {
  62         return false;
  63     }
  64 
  65     @Override
  66     public InputStream getInputStream() throws IOException {
  67         return Files.newInputStream(path);
  68     }
  69 
  70     @Override
  71     public boolean exists() {
  72         return true;
  73     }
  74 
  75     @Override
  76     public boolean isDirectory() {
  77         return attrs.isDirectory();
  78     }
  79 
  80     @Override
  81     public long lastModified() {
  82         return attrs.lastModifiedTime().toMillis();
  83     }
  84 
  85     @Override
  86     public String getPath() {
  87         return path.toUri().toString();
  88     }
  89 
  90     @Override
  91     public String getName() {
  92         return path.getFileName().toString();
  93     }
  94 
  95 //JCOV
  96     @Override
  97     public String getAbsoluteName() {
  98         return path.toAbsolutePath().toUri().toString();
  99     }
 100 // end JCOV
 101 
 102     @Override
 103     public long length() {
 104         return attrs.size();
 105     }
 106 
 107     @Override
 108     public String toString() {
 109         return path.toString();
 110     }
 111 }