View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.vfs.provider.local;
    18
    19  import org.apache.commons.vfs.FileName;
    20  import org.apache.commons.vfs.FileObject;
    21  import org.apache.commons.vfs.FileSystemException;
    22  import org.apache.commons.vfs.FileType;
    23  import org.apache.commons.vfs.RandomAccessContent;
    24  import org.apache.commons.vfs.provider.AbstractFileObject;
    25  import org.apache.commons.vfs.provider.UriParser;
    26  import org.apache.commons.vfs.util.RandomAccessMode;
    27  import org.apache.commons.vfs.util.FileObjectUtils;
    28
    29  import java.io.File;
    30  import java.io.FileInputStream;
    31  import java.io.FileOutputStream;
    32  import java.io.InputStream;
    33  import java.io.OutputStream;
    34  import java.io.IOException;
    35
    36  /***
    37   * A file object implementation which uses direct file access.
    38   *
    39   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    40   * @author Gary D. Gregory
    41   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    42   */
    43  public class LocalFile
    44      extends AbstractFileObject
    45      implements FileObject
    46  {
    47      private final String rootFile;
    48
    49      private File file;
    50
    51      /***
    52       * Creates a non-root file.
    53       */
    54      protected LocalFile(final LocalFileSystem fileSystem,
    55                          final String rootFile,
    56                          final FileName name) throws FileSystemException
    57      {
    58          super(name, fileSystem);
    59          this.rootFile = rootFile;
    60      }
    61
    62      /***
    63       * Returns the local file that this file object represents.
    64       */
    65      protected File getLocalFile()
    66      {
    67          return file;
    68      }
    69
    70      /***
    71       * Attaches this file object to its file resource.
    72       */
    73      protected void doAttach()
    74          throws Exception
    75      {
    76          if (file == null)
    77          {
    78              // Remove the "file:///"
    79              // LocalFileName localFileName = (LocalFileName) getName();
    80              String fileName = rootFile + getName().getPathDecoded();
    81              // fileName = UriParser.decode(fileName);
    82              file = new File(fileName);
    83          }
    84      }
    85
    86      /***
    87       * Returns the file's type.
    88       */
    89      protected FileType doGetType()
    90          throws Exception
    91      {
    92      	// JDK BUG: 6192331
    93          // if (!file.exists())
    94          if (!file.exists() && file.length() < 1)
    95          {
    96              return FileType.IMAGINARY;
    97          }
    98
    99          if (file.isDirectory())
    100         {
    101             return FileType.FOLDER;
    102         }
    103
    104         // In doubt, treat an existing file as file
    105         // if (file.isFile())
    106         // {
    107             return FileType.FILE;
    108         // }
    109
    110         // throw new FileSystemException("vfs.provider.local/get-type.error", file);
    111     }
    112
    113     /***
    114      * Returns the children of the file.
    115      */
    116     protected String[] doListChildren()
    117         throws Exception
    118     {
    119         return UriParser.encode(file.list());
    120     }
    121
    122     /***
    123      * Deletes this file, and all children.
    124      */
    125     protected void doDelete()
    126         throws Exception
    127     {
    128         if (!file.delete())
    129         {
    130             throw new FileSystemException("vfs.provider.local/delete-file.error", file);
    131         }
    132     }
    133
    134     /***
    135      * rename this file
    136      */
    137     protected void doRename(final FileObject newfile) throws Exception
    138     {
    139         LocalFile newLocalFile = (LocalFile) FileObjectUtils.getAbstractFileObject(newfile);
    140
    141         if (!file.renameTo(newLocalFile.getLocalFile()))
    142         {
    143             throw new FileSystemException("vfs.provider.local/rename-file.error",
    144                 new String[]{file.toString(), newfile.toString()});
    145         }
    146     }
    147
    148     /***
    149      * Creates this folder.
    150      */
    151     protected void doCreateFolder()
    152         throws Exception
    153     {
    154         if (!file.mkdirs())
    155         {
    156             throw new FileSystemException("vfs.provider.local/create-folder.error", file);
    157         }
    158     }
    159
    160     /***
    161      * Determines if this file can be written to.
    162      */
    163     protected boolean doIsWriteable() throws FileSystemException
    164     {
    165         return file.canWrite();
    166     }
    167
    168     /***
    169      * Determines if this file is hidden.
    170      */
    171     protected boolean doIsHidden()
    172     {
    173         return file.isHidden();
    174     }
    175
    176     /***
    177      * Determines if this file can be read.
    178      */
    179     protected boolean doIsReadable() throws FileSystemException
    180     {
    181         return file.canRead();
    182     }
    183
    184     /***
    185      * Gets the last modified time of this file.
    186      */
    187     protected long doGetLastModifiedTime() throws FileSystemException
    188     {
    189         return file.lastModified();
    190     }
    191
    192     /***
    193      * Sets the last modified time of this file.
    194      */
    195     protected void doSetLastModifiedTime(final long modtime)
    196         throws FileSystemException
    197     {
    198         file.setLastModified(modtime);
    199     }
    200
    201     /***
    202      * Creates an input stream to read the content from.
    203      */
    204     protected InputStream doGetInputStream()
    205         throws Exception
    206     {
    207         return new FileInputStream(file);
    208     }
    209
    210     /***
    211      * Creates an output stream to write the file content to.
    212      */
    213     protected OutputStream doGetOutputStream(boolean bAppend)
    214         throws Exception
    215     {
    216         return new FileOutputStream(file.getPath(), bAppend);
    217     }
    218
    219     /***
    220      * Returns the size of the file content (in bytes).
    221      */
    222     protected long doGetContentSize()
    223         throws Exception
    224     {
    225         return file.length();
    226     }
    227
    228     protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception
    229     {
    230         return new LocalFileRandomAccessContent(file, mode);
    231     }
    232
    233     protected boolean doIsSameFile(FileObject destFile) throws FileSystemException
    234     {
    235         if (!FileObjectUtils.isInstanceOf(destFile, LocalFile.class))
    236         {
    237             return false;
    238         }
    239
    240         LocalFile destLocalFile = (LocalFile) FileObjectUtils.getAbstractFileObject(destFile);
    241         if (!exists() || !destLocalFile.exists())
    242         {
    243             return false;
    244         }
    245
    246         try
    247         {
    248             return file.getCanonicalPath().equals(destLocalFile.file.getCanonicalPath());
    249         }
    250         catch (IOException e)
    251         {
    252             throw new FileSystemException(e);
    253         }
    254
    255     }
    256 }