Home » openjdk-7 » java » io » [javadoc | source]

    1   /* VMFile.java -- Class for methods natively accessing files
    2      Copyright (C) 2004  Free Software Foundation, Inc.
    3   
    4   This file is part of GNU Classpath.
    5   
    6   GNU Classpath is free software; you can redistribute it and/or modify
    7   it under the terms of the GNU General Public License as published by
    8   the Free Software Foundation; either version 2, or (at your option)
    9   any later version.
   10    
   11   GNU Classpath is distributed in the hope that it will be useful, but
   12   WITHOUT ANY WARRANTY; without even the implied warranty of
   13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14   General Public License for more details.
   15   
   16   You should have received a copy of the GNU General Public License
   17   along with GNU Classpath; see the file COPYING.  If not, write to the
   18   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   19   02110-1301 USA.
   20   
   21   Linking this library statically or dynamically with other modules is
   22   making a combined work based on this library.  Thus, the terms and
   23   conditions of the GNU General Public License cover the whole
   24   combination.
   25   
   26   As a special exception, the copyright holders of this library give you
   27   permission to link this library with independent modules to produce an
   28   executable, regardless of the license terms of these independent
   29   modules, and to copy and distribute the resulting executable under
   30   terms of your choice, provided that you also meet, for each linked
   31   independent module, the terms and conditions of the license of that
   32   module.  An independent module is a module which is not derived from
   33   or based on this library.  If you modify this library, you may extend
   34   this exception to your version of the library, but you are not
   35   obligated to do so.  If you do not wish to do so, delete this
   36   exception statement from your version. */
   37   
   38   
   39   package java.io;
   40   
   41   import gnu.classpath.Configuration;
   42   import gnu.java.io.PlatformHelper;
   43   
   44   
   45   /**
   46    * @author Michael Koch (konqueror@gmx.de)
   47    */
   48   final class VMFile
   49   {
   50     // FIXME: We support only case sensitive filesystems currently.
   51     static final boolean IS_CASE_SENSITIVE = true;
   52     static final boolean IS_DOS_8_3 = false;
   53   
   54     static
   55     {
   56       if (Configuration.INIT_LOAD_LIBRARY)
   57         {
   58   	System.loadLibrary("javaio");
   59         }
   60     }
   61   
   62     /*
   63      * This native method does the actual work of getting the last file
   64      * modification time.  It also does the existence check to avoid the
   65      * overhead of a call to exists()
   66      */
   67     static native long lastModified(String path);
   68   
   69     /*
   70      * This native method sets the permissions to make the file read only.
   71      */
   72     static native boolean setReadOnly(String path);
   73   
   74     /**
   75      * This method is used to create a temporary file
   76      */
   77     static native boolean create(String path) throws IOException;
   78   
   79     /*
   80      * This native function actually produces the list of file in this
   81      * directory
   82      */
   83     static native String[] list(String dirpath);
   84   
   85     /*
   86      * This native method actually performs the rename.
   87      */
   88     static native boolean renameTo(String targetpath, String destpath);
   89   
   90     /*
   91      * This native method actually determines the length of the file and
   92      * handles the existence check
   93      */
   94     static native long length(String path);
   95   
   96     /*
   97      * This native method does the actual checking of file existence.
   98      */
   99     static native boolean exists(String path);
  100   
  101     /*
  102      * This native method handles the actual deleting of the file
  103      */
  104     static native boolean delete(String path);
  105   
  106     /*
  107      * This method does the actual setting of the modification time.
  108      */
  109     static native boolean setLastModified(String path, long time);
  110   
  111     /*
  112      * This native method actually creates the directory
  113      */
  114     static native boolean mkdir(String dirpath);
  115   
  116     /*
  117      * This native method does the actual check of whether or not a file
  118      * is a plain file or not.  It also handles the existence check to
  119      * eliminate the overhead of a call to exists()
  120      */
  121     static native boolean isFile(String path);
  122   
  123     /**
  124      * This native method checks file permissions for writing
  125      */
  126     static synchronized native boolean canWrite(String path);
  127   
  128     /**
  129      * This methods checks if a directory can be written to.
  130      */
  131     static boolean canWriteDirectory(File dir)
  132     {
  133       try
  134         {
  135           String filename = IS_DOS_8_3 ? "tst" : "test-dir-write";
  136           File test = File.createTempFile(filename, null, dir);
  137           return (test != null && test.delete());
  138         }
  139       catch (IOException ioe)
  140         {
  141           return false;
  142         }
  143     }
  144   
  145     /**
  146      * This native method checks file permissions for reading
  147      */
  148     static synchronized native boolean canRead(String path);
  149   
  150     /*
  151      * This method does the actual check of whether or not a file is a
  152      * directory or not.  It also handle the existence check to eliminate
  153      * the overhead of a call to exists()
  154      */
  155     static native boolean isDirectory(String dirpath);
  156   
  157     /**
  158      * This method returns an array of filesystem roots.  Some operating systems
  159      * have volume oriented filesystem.  This method provides a mechanism for
  160      * determining which volumes exist.  GNU systems use a single hierarchical
  161      * filesystem, so will have only one "/" filesystem root.
  162      *
  163      * @return An array of <code>File</code> objects for each filesystem root
  164      * available.
  165      *
  166      * @since 1.2
  167      */
  168     static File[] listRoots()
  169     {
  170   	File[] roots = new File[1];
  171   	roots[0] = new File("/");
  172   	return roots;
  173     }
  174   
  175     /**
  176      * This method tests whether or not this file represents a "hidden" file.
  177      * On GNU systems, a file is hidden if its name begins with a "."
  178      * character.  Files with these names are traditionally not shown with
  179      * directory listing tools.
  180      *
  181      * @return <code>true</code> if the file is hidden, <code>false</code>
  182      * otherwise.
  183      *
  184      * @since 1.2
  185      */
  186     static boolean isHidden(String path)
  187     {
  188   	// FIXME: this only works on UNIX
  189   	return getName(path).startsWith(".");
  190     }
  191   
  192     /**
  193      * This method returns the name of the file.  This is everything in the
  194      * complete path of the file after the last instance of the separator
  195      * string.
  196      *
  197      * @return The file name
  198      */
  199     static String getName(String path)
  200     {
  201   	int pos = PlatformHelper.lastIndexOfSeparator(path);
  202   	if (pos == -1)
  203   	  return path;
  204   	
  205   	if (PlatformHelper.endWithSeparator(path))
  206   	  return "";
  207   	
  208   	return path.substring(pos + File.separator.length());
  209     }
  210   
  211     /**
  212      * This method returns a canonical representation of the pathname of
  213      * the given path.  The actual form of the canonical representation is
  214      * different.  On the GNU system, the canonical form differs from the
  215      * absolute form in that all relative file references to "." and ".."
  216      * are resolved and removed.
  217      * <p>
  218      * Note that this method, unlike the other methods which return path
  219      * names, can throw an IOException.  This is because native method 
  220      * might be required in order to resolve the canonical path
  221      *
  222      * @exception IOException If an error occurs
  223      */
  224     public static String toCanonicalForm(String path) throws IOException
  225     {
  226   	// FIXME: this only works on UNIX
  227   	return PlatformHelper.toCanonicalForm(path);
  228     }
  229   }

Home » openjdk-7 » java » io » [javadoc | source]