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