001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.tar;
018:
019: //TODO: Revert to [compress]
020: //import org.apache.commons.compress.tar.TarEntry;
021: import org.apache.commons.vfs.FileName;
022: import org.apache.commons.vfs.FileObject;
023: import org.apache.commons.vfs.FileSystemException;
024: import org.apache.commons.vfs.FileType;
025: import org.apache.commons.vfs.provider.AbstractFileObject;
026:
027: import java.io.InputStream;
028: import java.util.HashSet;
029:
030: /**
031: * A file in a Tar file system.
032: */
033: public class TarFileObject extends AbstractFileObject implements
034: FileObject {
035: private final HashSet children = new HashSet();
036: private final TarFileSystem fs;
037: protected TarEntry entry;
038: private FileType type;
039:
040: protected TarFileObject(FileName name, TarEntry entry,
041: TarFileSystem fs, boolean tarExists)
042: throws FileSystemException {
043: super (name, fs);
044: this .fs = fs;
045: setTarEntry(entry);
046: if (!tarExists) {
047: type = FileType.IMAGINARY;
048: }
049: }
050:
051: /**
052: * Sets the details for this file object.
053: */
054: protected void setTarEntry(final TarEntry entry) {
055: if (this .entry != null) {
056: return;
057: }
058:
059: if ((entry == null) || (entry.isDirectory())) {
060: type = FileType.FOLDER;
061: } else {
062: type = FileType.FILE;
063: }
064:
065: this .entry = entry;
066: }
067:
068: /**
069: * Attaches a child
070: */
071: protected void attachChild(FileName childName) {
072: children.add(childName.getBaseName());
073: }
074:
075: /**
076: * Determines if this file can be written to.
077: *
078: * @return <code>true</code> if this file is writeable, <code>false</code> if not.
079: */
080: public boolean isWriteable() throws FileSystemException {
081: return false;
082: }
083:
084: /**
085: * Returns the file's type.
086: */
087: protected FileType doGetType() {
088: return type;
089: }
090:
091: /**
092: * Lists the children of the file.
093: */
094: protected String[] doListChildren() {
095: return (String[]) children.toArray(new String[children.size()]);
096: }
097:
098: /**
099: * Returns the size of the file content (in bytes). Is only called if
100: * {@link #doGetType} returns {@link FileType#FILE}.
101: */
102: protected long doGetContentSize() {
103: if (entry == null) {
104: return 0;
105: }
106:
107: return entry.getSize();
108: }
109:
110: /**
111: * Returns the last modified time of this file.
112: */
113: protected long doGetLastModifiedTime() throws Exception {
114: if (entry == null) {
115: return 0;
116: }
117:
118: return entry.getModTime().getTime();
119: }
120:
121: /**
122: * Creates an input stream to read the file content from. Is only called
123: * if {@link #doGetType} returns {@link FileType#FILE}. The input stream
124: * returned by this method is guaranteed to be closed before this
125: * method is called again.
126: */
127: protected InputStream doGetInputStream() throws Exception {
128: return fs.getInputStream(entry);
129: }
130: }
|