KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > support > EncodedResource


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.support;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 import org.springframework.core.io.Resource;
24 import org.springframework.util.Assert;
25
26 /**
27  * Holder that combines a Resource with an encoding.
28  *
29  * <p>Used as argument for operations that support to read content with
30  * a specific encoding (usually through a <code>java.io.Reader</code>.
31  *
32  * @author Juergen Hoeller
33  * @since 1.2.6
34  * @see java.io.Reader
35  */

36 public class EncodedResource {
37
38     private final Resource resource;
39
40     private final String JavaDoc encoding;
41
42
43     /**
44      * Create a new EncodedResource for the given Resource,
45      * not specifying a specific encoding.
46      * @param resource the Resource to hold
47      */

48     public EncodedResource(Resource resource) {
49         this(resource, null);
50     }
51
52     /**
53      * Create a new EncodedResource for the given Resource,
54      * using the specified encoding.
55      * @param resource the Resource to hold
56      * @param encoding the encoding to use for reading from the resource
57      */

58     public EncodedResource(Resource resource, String JavaDoc encoding) {
59         Assert.notNull(resource, "Resource must not be null");
60         this.resource = resource;
61         this.encoding = encoding;
62     }
63
64
65     /**
66      * Return the Resource held.
67      */

68     public Resource getResource() {
69         return resource;
70     }
71
72     /**
73      * Return the encoding to use for reading from the resource,
74      * or <code>null</code> if none specified.
75      */

76     public String JavaDoc getEncoding() {
77         return encoding;
78     }
79
80     /**
81      * Open a <code>java.io.Reader</code> for the specified resource,
82      * using the specified encoding (if any).
83      * @throws IOException if opening the Reader failed
84      */

85     public Reader JavaDoc getReader() throws IOException JavaDoc {
86         if (this.encoding != null) {
87             return new InputStreamReader JavaDoc(this.resource.getInputStream(), this.encoding);
88         }
89         else {
90             return new InputStreamReader JavaDoc(this.resource.getInputStream());
91         }
92     }
93
94 }
95
Popular Tags