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      /**
38       * The value returned by {@link #doGetContentSize()} when not overridden by a subclass.
39       *
40       * @since 2.5.0
41       */
42      public static final int SIZE_UNDEFINED = -1;
43      private final FileObject container;
44      private final String[] children;
45  
46      /**
47       * Constructs a new instance.
48       *
49       * @param fileName the file name.
50       * @param container the container.
51       * @param fileSystem the file system.
52       */
53      protected CompressedFileFileObject(final AbstractFileName fileName, final FileObject container, final FS fileSystem) {
54          super(fileName, fileSystem);
55          this.container = container;
56  
57          // TODO, add getBaseName(String) to FileName
58          String baseName = container.getName().getBaseName();
59          final int pos = baseName.lastIndexOf('.');
60          if (pos > 0) {
61              baseName = baseName.substring(0, pos);
62          }
63          children = new String[] {baseName};
64      }
65  
66      @Override
67      public void createFile() throws FileSystemException {
68          container.createFile();
69          injectType(FileType.FILE);
70      }
71  
72      /**
73       * Returns the size of the file content (in bytes). Is only called if {@link #doGetType} returns
74       * {@link FileType#FILE}.
75       */
76      @Override
77      protected long doGetContentSize() {
78          return SIZE_UNDEFINED;
79      }
80  
81      /**
82       * Returns the last modified time of this file.
83       */
84      @Override
85      protected long doGetLastModifiedTime() throws Exception {
86          return container.getContent().getLastModifiedTime();
87      }
88  
89      /**
90       * Returns the file's type.
91       */
92      @Override
93      protected FileType doGetType() throws FileSystemException {
94          if (getName().getPath().endsWith("/")) {
95              return FileType.FOLDER;
96          }
97          return FileType.FILE;
98      }
99  
100     /**
101      * Lists the children of the file.
102      */
103     @Override
104     protected String[] doListChildren() {
105         return children;
106     }
107 
108     /**
109      * Gets the container.
110      *
111      * @return the container.
112      */
113     protected FileObject getContainer() {
114         return container;
115     }
116 
117     /**
118      * Determines if this file can be written to.
119      *
120      * @return {@code true} if this file is writable, {@code false} if not.
121      * @throws FileSystemException if an error occurs.
122      */
123     @Override
124     public boolean isWriteable() throws FileSystemException {
125         return getFileSystem().hasCapability(Capability.WRITE_CONTENT);
126     }
127 }