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.vfs2.provider.compressed;
18  
19  import org.apache.commons.vfs2.Capability;
20  import org.apache.commons.vfs2.FileObject;
21  import org.apache.commons.vfs2.FileSystemException;
22  import org.apache.commons.vfs2.FileType;
23  import org.apache.commons.vfs2.provider.AbstractFileName;
24  import org.apache.commons.vfs2.provider.AbstractFileObject;
25  
26  /**
27   * A compressed file.
28   *
29   * <p>
30   * Such a file only has one child (the compressed filename with stripped last extension)
31   * </p>
32   *
33   * @param <FS>
34   *            A CompressedFileFileSystem
35   */
36  public abstract class CompressedFileFileObject<FS extends CompressedFileFileSystem> extends AbstractFileObject<FS>
37  {
38      private final FileObject container;
39      private final String[] children;
40  
41      protected CompressedFileFileObject(final AbstractFileName name, final FileObject container, final FS fs)
42      {
43          super(name, fs);
44          this.container = container;
45  
46          // todo, add getBaseName(String) to FileName
47          String basename = container.getName().getBaseName();
48          final int pos = basename.lastIndexOf('.');
49          if (pos > 0)
50          {
51              basename = basename.substring(0, pos);
52          }
53          children = new String[]{basename};
54      }
55  
56      /**
57       * Determines if this file can be written to.
58       *
59       * @return {@code true} if this file is writeable, {@code false} if not.
60       * @throws FileSystemException if an error occurs.
61       */
62      @Override
63      public boolean isWriteable() throws FileSystemException
64      {
65          return getFileSystem().hasCapability(Capability.WRITE_CONTENT);
66      }
67  
68      /**
69       * Returns the file's type.
70       */
71      @Override
72      protected FileType doGetType() throws FileSystemException
73      {
74          if (getName().getPath().endsWith("/"))
75          {
76              return FileType.FOLDER;
77          }
78          else
79          {
80              return FileType.FILE;
81          }
82      }
83  
84      /**
85       * Lists the children of the file.
86       */
87      @Override
88      protected String[] doListChildren()
89      {
90          return children;
91      }
92  
93      /**
94       * Returns the size of the file content (in bytes).  Is only called if
95       * {@link #doGetType} returns {@link FileType#FILE}.
96       */
97      @Override
98      protected long doGetContentSize()
99      {
100         return -1;
101     }
102 
103     /**
104      * Returns the last modified time of this file.
105      */
106     @Override
107     protected long doGetLastModifiedTime() throws Exception
108     {
109         return container.getContent().getLastModifiedTime();
110     }
111 
112     protected FileObject getContainer()
113     {
114         return container;
115     }
116 
117     @Override
118     public void createFile() throws FileSystemException
119     {
120         container.createFile();
121         injectType(FileType.FILE);
122     }
123 }