01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.io.InputStreamReader;
21: import java.io.Reader;
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 encoding;
41:
42: /**
43: * Create a new EncodedResource for the given Resource,
44: * not specifying a specific encoding.
45: * @param resource the Resource to hold
46: */
47: public EncodedResource(Resource resource) {
48: this (resource, null);
49: }
50:
51: /**
52: * Create a new EncodedResource for the given Resource,
53: * using the specified encoding.
54: * @param resource the Resource to hold
55: * @param encoding the encoding to use for reading from the resource
56: */
57: public EncodedResource(Resource resource, String encoding) {
58: Assert.notNull(resource, "Resource must not be null");
59: this .resource = resource;
60: this .encoding = encoding;
61: }
62:
63: /**
64: * Return the Resource held.
65: */
66: public Resource getResource() {
67: return resource;
68: }
69:
70: /**
71: * Return the encoding to use for reading from the resource,
72: * or <code>null</code> if none specified.
73: */
74: public String getEncoding() {
75: return encoding;
76: }
77:
78: /**
79: * Open a <code>java.io.Reader</code> for the specified resource,
80: * using the specified encoding (if any).
81: * @throws IOException if opening the Reader failed
82: */
83: public Reader getReader() throws IOException {
84: if (this .encoding != null) {
85: return new InputStreamReader(
86: this .resource.getInputStream(), this .encoding);
87: } else {
88: return new InputStreamReader(this.resource.getInputStream());
89: }
90: }
91:
92: }
|