Polishing

This commit is contained in:
Juergen Hoeller
2014-11-11 03:26:40 +01:00
parent 6b353e889a
commit e1d6826fcd

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1"); public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
private final Charset defaultCharset; private final Charset defaultCharset;
private final List<Charset> availableCharsets; private final List<Charset> availableCharsets;
@@ -66,6 +67,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values()); this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
} }
/** /**
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request. * Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
* <p>Default is {@code true}. * <p>Default is {@code true}.
@@ -74,6 +76,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
this.writeAcceptCharset = writeAcceptCharset; this.writeAcceptCharset = writeAcceptCharset;
} }
@Override @Override
public boolean supports(Class<?> clazz) { public boolean supports(Class<?> clazz) {
return String.class.equals(clazz); return String.class.equals(clazz);
@@ -86,10 +89,10 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
} }
@Override @Override
protected Long getContentLength(String s, MediaType contentType) { protected Long getContentLength(String str, MediaType contentType) {
Charset charset = getContentTypeCharset(contentType); Charset charset = getContentTypeCharset(contentType);
try { try {
return (long) s.getBytes(charset.name()).length; return (long) str.getBytes(charset.name()).length;
} }
catch (UnsupportedEncodingException ex) { catch (UnsupportedEncodingException ex) {
// should not occur // should not occur
@@ -98,17 +101,19 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
} }
@Override @Override
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
if (this.writeAcceptCharset) { if (this.writeAcceptCharset) {
outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
} }
Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
StreamUtils.copy(s, charset, outputMessage.getBody()); StreamUtils.copy(str, charset, outputMessage.getBody());
} }
/** /**
* Return the list of supported {@link Charset}. * Return the list of supported {@link Charset}s.
* <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses. * <p>By default, returns {@link Charset#availableCharsets()}.
* Can be overridden in subclasses.
* @return the list of accepted charsets * @return the list of accepted charsets
*/ */
protected List<Charset> getAcceptedCharsets() { protected List<Charset> getAcceptedCharsets() {
@@ -123,4 +128,5 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
return this.defaultCharset; return this.defaultCharset;
} }
} }
} }