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.wicket.util.resource;
018:
019: import java.io.FileInputStream;
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.apache.wicket.util.file.File;
025: import org.apache.wicket.util.time.Time;
026:
027: /**
028: * A FileResourceStream is an IResource implementation for files.
029: *
030: * @see org.apache.wicket.util.resource.IResourceStream
031: * @see org.apache.wicket.util.watch.IModifiable
032: * @author Jonathan Locke
033: */
034: public class FileResourceStream extends AbstractResourceStream
035: implements IFixedLocationResourceStream {
036: private static final long serialVersionUID = 1L;
037:
038: /** Any associated file */
039: private final File file;
040:
041: /** Resource stream */
042: private transient InputStream inputStream;
043:
044: /**
045: * Constructor.
046: *
047: * @param file
048: * {@link File} containing resource
049: */
050: public FileResourceStream(final File file) {
051: this .file = file;
052: }
053:
054: /**
055: * Constructor.
056: *
057: * @param file
058: * {@link java.io.File} containing resource
059: */
060: public FileResourceStream(final java.io.File file) {
061: this .file = new File(file);
062: }
063:
064: /**
065: * Closes this resource.
066: *
067: * @throws IOException
068: */
069: public void close() throws IOException {
070: if (inputStream != null) {
071: inputStream.close();
072: inputStream = null;
073: }
074: }
075:
076: /**
077: * @see IResourceStream#getContentType()
078: */
079: public String getContentType() {
080: // Let ResourceStreamRequestTarget handle content-type automatically
081: return null;
082: }
083:
084: /**
085: * @return The file this resource resides in, if any.
086: */
087: public File getFile() {
088: return file;
089: }
090:
091: /**
092: * @return A readable input stream for this resource. The same input stream
093: * is returned until <tt>FileResourceStream.close()</tt> is
094: * invoked.
095: *
096: * @throws ResourceStreamNotFoundException
097: */
098: public InputStream getInputStream()
099: throws ResourceStreamNotFoundException {
100: if (inputStream == null) {
101: try {
102: inputStream = new FileInputStream(file);
103: } catch (FileNotFoundException e) {
104: throw new ResourceStreamNotFoundException("Resource "
105: + file + " could not be found", e);
106: }
107: }
108:
109: return inputStream;
110: }
111:
112: /**
113: * @see org.apache.wicket.util.watch.IModifiable#lastModifiedTime()
114: * @return The last time this resource was modified
115: */
116: public Time lastModifiedTime() {
117: if (file != null) {
118: return file.lastModifiedTime();
119: }
120: return null;
121: }
122:
123: /**
124: * @see java.lang.Object#toString()
125: */
126: public String toString() {
127: if (file != null) {
128: return file.toString();
129: }
130: return "";
131: }
132:
133: /**
134: * @see org.apache.wicket.util.resource.IResourceStream#length()
135: */
136: public long length() {
137: if (file != null) {
138: return file.length();
139: }
140: return 0;
141: }
142:
143: /**
144: * @see org.apache.wicket.util.resource.IFixedLocationResourceStream#locationAsString()
145: */
146: public String locationAsString() {
147: if (file != null) {
148: return file.getAbsolutePath();
149: }
150: return null;
151: }
152: }
|