KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > InputStreamResource


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.io;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21
22 /**
23  * {@link Resource} implementation for a given InputStream. Should only
24  * be used if no specific Resource implementation is applicable.
25  * In particular, prefer {@link ByteArrayResource} or any of the
26  * file-based Resource implementations where possible.
27  *
28  * <p>In contrast to other Resource implementations, this is a descriptor
29  * for an <i>already opened</i> resource - therefore returning "true" from
30  * <code>isOpen()</code>. Do not use it if you need to keep the resource
31  * descriptor somewhere, or if you need to read a stream multiple times.
32  *
33  * @author Juergen Hoeller
34  * @since 28.12.2003
35  * @see ByteArrayResource
36  * @see ClassPathResource
37  * @see FileSystemResource
38  * @see UrlResource
39  */

40 public class InputStreamResource extends AbstractResource {
41
42     private final InputStream JavaDoc inputStream;
43
44     private final String JavaDoc description;
45
46     private boolean read = false;
47
48
49     /**
50      * Create a new InputStreamResource.
51      * @param inputStream the InputStream to use
52      */

53     public InputStreamResource(InputStream JavaDoc inputStream) {
54         this(inputStream, "resource loaded through InputStream");
55     }
56
57     /**
58      * Create a new InputStreamResource.
59      * @param inputStream the InputStream to use
60      * @param description where the InputStream comes from
61      */

62     public InputStreamResource(InputStream JavaDoc inputStream, String JavaDoc description) {
63         if (inputStream == null) {
64             throw new IllegalArgumentException JavaDoc("InputStream must not be null");
65         }
66         this.inputStream = inputStream;
67         this.description = (description != null ? description : "");
68     }
69
70
71     /**
72      * This implementation always returns <code>true</code>.
73      */

74     public boolean exists() {
75         return true;
76     }
77
78     /**
79      * This implementation always returns <code>true</code>.
80      */

81     public boolean isOpen() {
82         return true;
83     }
84
85     /**
86      * This implementation throws IllegalStateException if attempting to
87      * read the underlying stream multiple times.
88      */

89     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, IllegalStateException JavaDoc {
90         if (this.read) {
91             throw new IllegalStateException JavaDoc("InputStream has already been read - " +
92                     "do not use InputStreamResource if a stream needs to be read multiple times");
93         }
94         this.read = true;
95         return this.inputStream;
96     }
97
98     /**
99      * This implementation returns the passed-in description, if any.
100      */

101     public String JavaDoc getDescription() {
102         return this.description;
103     }
104
105
106     /**
107      * This implementation compares the underlying InputStream.
108      */

109     public boolean equals(Object JavaDoc obj) {
110         return (obj == this ||
111             (obj instanceof InputStreamResource && ((InputStreamResource) obj).inputStream.equals(this.inputStream)));
112     }
113
114     /**
115      * This implementation returns the hash code of the underlying InputStream.
116      */

117     public int hashCode() {
118         return this.inputStream.hashCode();
119     }
120
121 }
122
Popular Tags