1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
27
27
import org .springframework .util .ObjectUtils ;
28
28
29
29
/**
30
- * Holder that combines a {@link org.springframework.core.io. Resource}
31
- * with a specific encoding to be used for reading from the resource.
30
+ * Holder that combines a {@link Resource} descriptor with a specific encoding
31
+ * or {@code Charset} to be used for reading from the resource.
32
32
*
33
- * <p>Used as argument for operations that support to read content with
34
- * a specific encoding (usually through a {@code java.io.Reader}.
33
+ * <p>Used as an argument for operations that support reading content with
34
+ * a specific encoding, typically via a {@code java.io.Reader}.
35
35
*
36
36
* @author Juergen Hoeller
37
+ * @author Sam Brannen
37
38
* @since 1.2.6
38
39
* @see java.io.Reader
40
+ * @see java.nio.charset.Charset
39
41
*/
40
42
public class EncodedResource {
41
43
42
44
private final Resource resource ;
43
45
44
- private String encoding ;
46
+ private final String encoding ;
45
47
46
- private Charset charset ;
48
+ private final Charset charset ;
47
49
48
50
49
51
/**
50
- * Create a new EncodedResource for the given Resource,
51
- * not specifying a specific encoding.
52
- * @param resource the Resource to hold
52
+ * Create a new {@code EncodedResource} for the given {@code Resource} ,
53
+ * not specifying an explicit encoding or {@code Charset} .
54
+ * @param resource the {@code Resource} to hold; never {@code null}
53
55
*/
54
56
public EncodedResource (Resource resource ) {
55
- Assert .notNull (resource , "Resource must not be null" );
56
- this .resource = resource ;
57
+ this (resource , null , null );
57
58
}
58
59
59
60
/**
60
- * Create a new EncodedResource for the given Resource,
61
- * using the specified encoding.
62
- * @param resource the Resource to hold
61
+ * Create a new {@code EncodedResource} for the given {@code Resource} ,
62
+ * using the specified {@code encoding} .
63
+ * @param resource the {@code Resource} to hold; never {@code null}
63
64
* @param encoding the encoding to use for reading from the resource
64
65
*/
65
66
public EncodedResource (Resource resource , String encoding ) {
66
- Assert .notNull (resource , "Resource must not be null" );
67
- this .resource = resource ;
68
- this .encoding = encoding ;
67
+ this (resource , encoding , null );
69
68
}
70
69
71
70
/**
72
- * Create a new EncodedResource for the given Resource,
73
- * using the specified encoding .
74
- * @param resource the Resource to hold
75
- * @param charset the charset to use for reading from the resource
71
+ * Create a new {@code EncodedResource} for the given {@code Resource} ,
72
+ * using the specified {@code Charset} .
73
+ * @param resource the {@code Resource} to hold; never {@code null}
74
+ * @param charset the {@code Charset} to use for reading from the resource
76
75
*/
77
76
public EncodedResource (Resource resource , Charset charset ) {
77
+ this (resource , null , charset );
78
+ }
79
+
80
+ private EncodedResource (Resource resource , String encoding , Charset charset ) {
81
+ super ();
78
82
Assert .notNull (resource , "Resource must not be null" );
79
83
this .resource = resource ;
84
+ this .encoding = encoding ;
80
85
this .charset = charset ;
81
86
}
82
87
83
-
84
88
/**
85
- * Return the Resource held.
89
+ * Return the {@code Resource} held by this {@code EncodedResource} .
86
90
*/
87
91
public final Resource getResource () {
88
92
return this .resource ;
89
93
}
90
94
91
95
/**
92
- * Return the encoding to use for reading from the resource,
96
+ * Return the encoding to use for reading from the {@linkplain #getResource() resource} ,
93
97
* or {@code null} if none specified.
94
98
*/
95
99
public final String getEncoding () {
96
100
return this .encoding ;
97
101
}
98
102
99
103
/**
100
- * Return the charset to use for reading from the resource,
104
+ * Return the {@code Charset} to use for reading from the {@linkplain #getResource() resource} ,
101
105
* or {@code null} if none specified.
102
106
*/
103
107
public final Charset getCharset () {
104
108
return this .charset ;
105
109
}
106
110
107
-
108
111
/**
109
112
* Determine whether a {@link Reader} is required as opposed to an {@link InputStream},
110
- * i.e. whether an encoding or a charset has been specified.
113
+ * i.e. whether an {@linkplain #getEncoding() encoding} or a {@link #getCharset() Charset}
114
+ * has been specified.
111
115
* @see #getReader()
112
116
* @see #getInputStream()
113
117
*/
@@ -116,10 +120,12 @@ public boolean requiresReader() {
116
120
}
117
121
118
122
/**
119
- * Open a {@code java.io.Reader} for the specified resource,
120
- * using the specified encoding (if any).
123
+ * Open a {@code java.io.Reader} for the specified resource, using the specified
124
+ * {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}
125
+ * (if any).
121
126
* @throws IOException if opening the Reader failed
122
127
* @see #requiresReader()
128
+ * @see #getInputStream()
123
129
*/
124
130
public Reader getReader () throws IOException {
125
131
if (this .charset != null ) {
@@ -134,10 +140,11 @@ else if (this.encoding != null) {
134
140
}
135
141
136
142
/**
137
- * Open an {@code java.io.InputStream} for the specified resource,
138
- * typically assuming that there is no specific encoding to use .
143
+ * Open a {@code java.io.InputStream} for the specified resource, ignoring any
144
+ * specified {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding} .
139
145
* @throws IOException if opening the InputStream failed
140
146
* @see #requiresReader()
147
+ * @see #getReader()
141
148
*/
142
149
public InputStream getInputStream () throws IOException {
143
150
return this .resource .getInputStream ();
@@ -150,9 +157,10 @@ public boolean equals(Object obj) {
150
157
return true ;
151
158
}
152
159
if (obj instanceof EncodedResource ) {
153
- EncodedResource otherRes = (EncodedResource ) obj ;
154
- return (this .resource .equals (otherRes .resource ) &&
155
- ObjectUtils .nullSafeEquals (this .encoding , otherRes .encoding ));
160
+ EncodedResource that = (EncodedResource ) obj ;
161
+ return (this .resource .equals (that .resource ) &&
162
+ ObjectUtils .nullSafeEquals (this .charset , that .charset ) &&
163
+ ObjectUtils .nullSafeEquals (this .encoding , that .encoding ));
156
164
}
157
165
return false ;
158
166
}
0 commit comments