32
32
/**
33
33
* Implementation of {@link HttpMessageConverter} that can read and write strings.
34
34
*
35
- * <p>By default, this converter supports all media types (<code>*/*</code>), and writes with a {@code
36
- * Content-Type} of {@code text/plain}. This can be overridden by setting the {@link
37
- * #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property.
35
+ * <p>By default, this converter supports all media types (<code>*/*</code>),
36
+ * and writes with a {@code Content-Type} of {@code text/plain}. This can be overridden
37
+ * by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
38
38
*
39
39
* @author Arjen Poutsma
40
40
* @since 3.0
41
41
*/
42
42
public class StringHttpMessageConverter extends AbstractHttpMessageConverter <String > {
43
43
44
+ public static final Charset DEFAULT_CHARSET = Charset .forName ("ISO-8859-1" );
45
+
44
46
private final Charset defaultCharset ;
45
47
46
48
private final List <Charset > availableCharsets ;
47
49
48
50
private boolean writeAcceptCharset = true ;
49
51
52
+
50
53
/**
51
54
* A default constructor that uses {@code "ISO-8859-1"} as the default charset.
52
55
* @see #StringHttpMessageConverter(Charset)
53
56
*/
54
57
public StringHttpMessageConverter () {
55
- this (Charset . forName ( "ISO-8859-1" ) );
58
+ this (DEFAULT_CHARSET );
56
59
}
57
60
58
61
/**
@@ -73,6 +76,7 @@ public void setWriteAcceptCharset(boolean writeAcceptCharset) {
73
76
this .writeAcceptCharset = writeAcceptCharset ;
74
77
}
75
78
79
+
76
80
@ Override
77
81
public boolean supports (Class <?> clazz ) {
78
82
return String .class .equals (clazz );
@@ -92,13 +96,13 @@ protected Long getContentLength(String s, MediaType contentType) {
92
96
}
93
97
catch (UnsupportedEncodingException ex ) {
94
98
// should not occur
95
- throw new InternalError (ex . getMessage () );
99
+ throw new IllegalStateException (ex );
96
100
}
97
101
}
98
102
99
103
@ Override
100
104
protected void writeInternal (String s , HttpOutputMessage outputMessage ) throws IOException {
101
- if (writeAcceptCharset ) {
105
+ if (this . writeAcceptCharset ) {
102
106
outputMessage .getHeaders ().setAcceptCharset (getAcceptedCharsets ());
103
107
}
104
108
Charset charset = getContentTypeCharset (outputMessage .getHeaders ().getContentType ());
@@ -107,9 +111,7 @@ protected void writeInternal(String s, HttpOutputMessage outputMessage) throws I
107
111
108
112
/**
109
113
* Return the list of supported {@link Charset}.
110
- *
111
114
* <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
112
- *
113
115
* @return the list of accepted charsets
114
116
*/
115
117
protected List <Charset > getAcceptedCharsets () {
0 commit comments