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.tar;
    18
    19  //TODO: Revert to [compress]
    20  //import org.apache.commons.compress.tar.TarEntry;
    21  import org.apache.commons.vfs.FileName;
    22  import org.apache.commons.vfs.FileObject;
    23  import org.apache.commons.vfs.FileSystemException;
    24  import org.apache.commons.vfs.FileType;
    25  import org.apache.commons.vfs.provider.AbstractFileObject;
    26
    27  import java.io.InputStream;
    28  import java.util.HashSet;
    29
    30  /***
    31   * A file in a Tar file system.
    32   */
    33  public class TarFileObject
    34      extends AbstractFileObject
    35      implements FileObject
    36  {
    37      private final HashSet children = new HashSet();
    38      private final TarFileSystem fs;
    39      protected TarEntry entry;
    40      private FileType type;
    41
    42      protected TarFileObject(FileName name,
    43                              TarEntry entry,
    44                              TarFileSystem fs,
    45                              boolean tarExists) throws FileSystemException
    46      {
    47          super(name, fs);
    48          this.fs = fs;
    49          setTarEntry(entry);
    50          if (!tarExists)
    51          {
    52              type = FileType.IMAGINARY;
    53          }
    54      }
    55
    56      /***
    57       * Sets the details for this file object.
    58       */
    59      protected void setTarEntry(final TarEntry entry)
    60      {
    61          if (this.entry != null)
    62          {
    63              return;
    64          }
    65
    66          if ((entry == null) || (entry.isDirectory()))
    67          {
    68              type = FileType.FOLDER;
    69          }
    70          else
    71          {
    72              type = FileType.FILE;
    73          }
    74
    75          this.entry = entry;
    76      }
    77
    78      /***
    79       * Attaches a child
    80       */
    81      protected void attachChild(FileName childName)
    82      {
    83          children.add(childName.getBaseName());
    84      }
    85
    86      /***
    87       * Determines if this file can be written to.
    88       *
    89       * @return <code>true</code> if this file is writeable, <code>false</code> if not.
    90       */
    91      public boolean isWriteable() throws FileSystemException
    92      {
    93          return false;
    94      }
    95
    96      /***
    97       * Returns the file's type.
    98       */
    99      protected FileType doGetType()
    100     {
    101         return type;
    102     }
    103
    104     /***
    105      * Lists the children of the file.
    106      */
    107     protected String[] doListChildren()
    108     {
    109         return (String[]) children.toArray(new String[children.size()]);
    110     }
    111
    112     /***
    113      * Returns the size of the file content (in bytes).  Is only called if
    114      * {@link #doGetType} returns {@link FileType#FILE}.
    115      */
    116     protected long doGetContentSize()
    117     {
    118         if (entry == null)
    119         {
    120             return 0;
    121         }
    122
    123         return entry.getSize();
    124     }
    125
    126     /***
    127      * Returns the last modified time of this file.
    128      */
    129     protected long doGetLastModifiedTime() throws Exception
    130     {
    131         if (entry == null)
    132         {
    133             return 0;
    134         }
    135
    136         return entry.getModTime().getTime();
    137     }
    138
    139     /***
    140      * Creates an input stream to read the file content from.  Is only called
    141      * if  {@link #doGetType} returns {@link FileType#FILE}.  The input stream
    142      * returned by this method is guaranteed to be closed before this
    143      * method is called again.
    144      */
    145     protected InputStream doGetInputStream() throws Exception
    146     {
    147         return fs.getInputStream(entry);
    148     }
    149 }