View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.creadur.whisker.app.load;
20  
21  import java.io.BufferedInputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import org.apache.creadur.whisker.app.StreamableResource;
28  /**
29   * Streams, on demand, the contents of a file identified by a full file name,
30   * including path.
31   */
32  public final class StreamableFileNameResource extends StreamableResource {
33      /**
34       * The full file name, including path,
35       * of the resource to be streamed.
36       */
37      private final String fileName;
38  
39      /**
40       * Constructs an instance that streams
41       * the resource identified by name on demand.
42       * @param fileName full file name, including path,
43       * of the resource to be streamed
44       * on demand, not null
45       */
46      public StreamableFileNameResource(final String fileName) {
47          super();
48          this.fileName = fileName;
49      }
50  
51      /**
52       * Gets the file name of the resource to be streamed.
53       * @return full file name, not null
54       */
55      public String getFileName() {
56          return fileName;
57      }
58  
59      /**
60       * Opens the file as an input stream.
61       * @return not null
62       * @see StreamableResource#open()
63       * @throws IOException when resource cannot be opened
64       */
65      @Override
66      public InputStream open() throws IOException {
67          return new BufferedInputStream(new FileInputStream(new File(fileName)));
68      }
69  
70      /**
71       * Suitable for logging.
72       * @return a description
73       */
74      @Override
75      public String toString() {
76          return "StreamableFileNameResource [fileName=" + fileName + "]";
77      }
78  }