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.local;
018:
019: import org.apache.commons.vfs.FileName;
020: import org.apache.commons.vfs.FileObject;
021: import org.apache.commons.vfs.FileSystemException;
022: import org.apache.commons.vfs.FileType;
023: import org.apache.commons.vfs.RandomAccessContent;
024: import org.apache.commons.vfs.provider.AbstractFileObject;
025: import org.apache.commons.vfs.provider.UriParser;
026: import org.apache.commons.vfs.util.RandomAccessMode;
027: import org.apache.commons.vfs.util.FileObjectUtils;
028:
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.FileOutputStream;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034: import java.io.IOException;
035:
036: /**
037: * A file object implementation which uses direct file access.
038: *
039: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
040: * @author Gary D. Gregory
041: * @version $Revision: 488989 $ $Date: 2006-12-20 01:14:53 -0800 (Wed, 20 Dec 2006) $
042: */
043: public class LocalFile extends AbstractFileObject implements FileObject {
044: private final String rootFile;
045:
046: private File file;
047:
048: /**
049: * Creates a non-root file.
050: */
051: protected LocalFile(final LocalFileSystem fileSystem,
052: final String rootFile, final FileName name)
053: throws FileSystemException {
054: super (name, fileSystem);
055: this .rootFile = rootFile;
056: }
057:
058: /**
059: * Returns the local file that this file object represents.
060: */
061: protected File getLocalFile() {
062: return file;
063: }
064:
065: /**
066: * Attaches this file object to its file resource.
067: */
068: protected void doAttach() throws Exception {
069: if (file == null) {
070: // Remove the "file:///"
071: // LocalFileName localFileName = (LocalFileName) getName();
072: String fileName = rootFile + getName().getPathDecoded();
073: // fileName = UriParser.decode(fileName);
074: file = new File(fileName);
075: }
076: }
077:
078: /**
079: * Returns the file's type.
080: */
081: protected FileType doGetType() throws Exception {
082: // JDK BUG: 6192331
083: // if (!file.exists())
084: if (!file.exists() && file.length() < 1) {
085: return FileType.IMAGINARY;
086: }
087:
088: if (file.isDirectory()) {
089: return FileType.FOLDER;
090: }
091:
092: // In doubt, treat an existing file as file
093: // if (file.isFile())
094: // {
095: return FileType.FILE;
096: // }
097:
098: // throw new FileSystemException("vfs.provider.local/get-type.error", file);
099: }
100:
101: /**
102: * Returns the children of the file.
103: */
104: protected String[] doListChildren() throws Exception {
105: return UriParser.encode(file.list());
106: }
107:
108: /**
109: * Deletes this file, and all children.
110: */
111: protected void doDelete() throws Exception {
112: if (!file.delete()) {
113: throw new FileSystemException(
114: "vfs.provider.local/delete-file.error", file);
115: }
116: }
117:
118: /**
119: * rename this file
120: */
121: protected void doRename(final FileObject newfile) throws Exception {
122: LocalFile newLocalFile = (LocalFile) FileObjectUtils
123: .getAbstractFileObject(newfile);
124:
125: if (!file.renameTo(newLocalFile.getLocalFile())) {
126: throw new FileSystemException(
127: "vfs.provider.local/rename-file.error",
128: new String[] { file.toString(), newfile.toString() });
129: }
130: }
131:
132: /**
133: * Creates this folder.
134: */
135: protected void doCreateFolder() throws Exception {
136: if (!file.mkdirs()) {
137: throw new FileSystemException(
138: "vfs.provider.local/create-folder.error", file);
139: }
140: }
141:
142: /**
143: * Determines if this file can be written to.
144: */
145: protected boolean doIsWriteable() throws FileSystemException {
146: return file.canWrite();
147: }
148:
149: /**
150: * Determines if this file is hidden.
151: */
152: protected boolean doIsHidden() {
153: return file.isHidden();
154: }
155:
156: /**
157: * Determines if this file can be read.
158: */
159: protected boolean doIsReadable() throws FileSystemException {
160: return file.canRead();
161: }
162:
163: /**
164: * Gets the last modified time of this file.
165: */
166: protected long doGetLastModifiedTime() throws FileSystemException {
167: return file.lastModified();
168: }
169:
170: /**
171: * Sets the last modified time of this file.
172: */
173: protected boolean doSetLastModTime(final long modtime)
174: throws FileSystemException {
175: return file.setLastModified(modtime);
176: }
177:
178: /**
179: * Creates an input stream to read the content from.
180: */
181: protected InputStream doGetInputStream() throws Exception {
182: return new FileInputStream(file);
183: }
184:
185: /**
186: * Creates an output stream to write the file content to.
187: */
188: protected OutputStream doGetOutputStream(boolean bAppend)
189: throws Exception {
190: return new FileOutputStream(file.getPath(), bAppend);
191: }
192:
193: /**
194: * Returns the size of the file content (in bytes).
195: */
196: protected long doGetContentSize() throws Exception {
197: return file.length();
198: }
199:
200: protected RandomAccessContent doGetRandomAccessContent(
201: final RandomAccessMode mode) throws Exception {
202: return new LocalFileRandomAccessContent(file, mode);
203: }
204:
205: protected boolean doIsSameFile(FileObject destFile)
206: throws FileSystemException {
207: if (!FileObjectUtils.isInstanceOf(destFile, LocalFile.class)) {
208: return false;
209: }
210:
211: LocalFile destLocalFile = (LocalFile) FileObjectUtils
212: .getAbstractFileObject(destFile);
213: if (!exists() || !destLocalFile.exists()) {
214: return false;
215: }
216:
217: try {
218: return file.getCanonicalPath().equals(
219: destLocalFile.file.getCanonicalPath());
220: } catch (IOException e) {
221: throw new FileSystemException(e);
222: }
223:
224: }
225: }
|