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