Use String.getBytes(Charset) wherever possible

This commit is contained in:
Trustin Lee 2012-08-17 11:43:59 +09:00
parent de37d75871
commit 0a99a33540
6 changed files with 19 additions and 14 deletions

View File

@ -158,10 +158,10 @@ public abstract class HttpMessageEncoder extends MessageToByteEncoder<Object> {
private static void encodeHeader(ByteBuf buf, String header, String value) private static void encodeHeader(ByteBuf buf, String header, String value)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
buf.writeBytes(header.getBytes("ASCII")); buf.writeBytes(header.getBytes(CharsetUtil.US_ASCII));
buf.writeByte(COLON); buf.writeByte(COLON);
buf.writeByte(SP); buf.writeByte(SP);
buf.writeBytes(value.getBytes("ASCII")); buf.writeBytes(value.getBytes(CharsetUtil.US_ASCII));
buf.writeByte(CR); buf.writeByte(CR);
buf.writeByte(LF); buf.writeByte(LF);
} }

View File

@ -17,6 +17,7 @@ package io.netty.handler.codec.http;
import static io.netty.handler.codec.http.HttpConstants.*; import static io.netty.handler.codec.http.HttpConstants.*;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
/** /**
* Encodes an {@link HttpResponse} or an {@link HttpChunk} into * Encodes an {@link HttpResponse} or an {@link HttpChunk} into
@ -33,11 +34,11 @@ public class HttpResponseEncoder extends HttpMessageEncoder {
@Override @Override
protected void encodeInitialLine(ByteBuf buf, HttpMessage message) throws Exception { protected void encodeInitialLine(ByteBuf buf, HttpMessage message) throws Exception {
HttpResponse response = (HttpResponse) message; HttpResponse response = (HttpResponse) message;
buf.writeBytes(response.getProtocolVersion().toString().getBytes("ASCII")); buf.writeBytes(response.getProtocolVersion().toString().getBytes(CharsetUtil.US_ASCII));
buf.writeByte(SP); buf.writeByte(SP);
buf.writeBytes(String.valueOf(response.getStatus().getCode()).getBytes("ASCII")); buf.writeBytes(String.valueOf(response.getStatus().getCode()).getBytes(CharsetUtil.US_ASCII));
buf.writeByte(SP); buf.writeByte(SP);
buf.writeBytes(String.valueOf(response.getStatus().getReasonPhrase()).getBytes("ASCII")); buf.writeBytes(String.valueOf(response.getStatus().getReasonPhrase()).getBytes(CharsetUtil.US_ASCII));
buf.writeByte(CR); buf.writeByte(CR);
buf.writeByte(LF); buf.writeByte(LF);
} }

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.rtsp;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpMessage; import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.CharsetUtil;
/** /**
* Encodes an RTSP request represented in {@link HttpRequest} into * Encodes an RTSP request represented in {@link HttpRequest} into
@ -30,11 +31,11 @@ public class RtspRequestEncoder extends RtspMessageEncoder {
protected void encodeInitialLine(ByteBuf buf, HttpMessage message) protected void encodeInitialLine(ByteBuf buf, HttpMessage message)
throws Exception { throws Exception {
HttpRequest request = (HttpRequest) message; HttpRequest request = (HttpRequest) message;
buf.writeBytes(request.getMethod().toString().getBytes("ASCII")); buf.writeBytes(request.getMethod().toString().getBytes(CharsetUtil.US_ASCII));
buf.writeByte((byte) ' '); buf.writeByte((byte) ' ');
buf.writeBytes(request.getUri().getBytes("ASCII")); buf.writeBytes(request.getUri().getBytes(CharsetUtil.UTF_8));
buf.writeByte((byte) ' '); buf.writeByte((byte) ' ');
buf.writeBytes(request.getProtocolVersion().toString().getBytes("ASCII")); buf.writeBytes(request.getProtocolVersion().toString().getBytes(CharsetUtil.US_ASCII));
buf.writeByte((byte) '\r'); buf.writeByte((byte) '\r');
buf.writeByte((byte) '\n'); buf.writeByte((byte) '\n');
} }

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.rtsp;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpMessage; import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil;
/** /**
* Encodes an RTSP response represented in {@link HttpResponse} into * Encodes an RTSP response represented in {@link HttpResponse} into
@ -30,11 +31,11 @@ public class RtspResponseEncoder extends RtspMessageEncoder {
protected void encodeInitialLine(ByteBuf buf, HttpMessage message) protected void encodeInitialLine(ByteBuf buf, HttpMessage message)
throws Exception { throws Exception {
HttpResponse response = (HttpResponse) message; HttpResponse response = (HttpResponse) message;
buf.writeBytes(response.getProtocolVersion().toString().getBytes("ASCII")); buf.writeBytes(response.getProtocolVersion().toString().getBytes(CharsetUtil.US_ASCII));
buf.writeByte((byte) ' '); buf.writeByte((byte) ' ');
buf.writeBytes(String.valueOf(response.getStatus().getCode()).getBytes("ASCII")); buf.writeBytes(String.valueOf(response.getStatus().getCode()).getBytes(CharsetUtil.US_ASCII));
buf.writeByte((byte) ' '); buf.writeByte((byte) ' ');
buf.writeBytes(String.valueOf(response.getStatus().getReasonPhrase()).getBytes("ASCII")); buf.writeBytes(String.valueOf(response.getStatus().getReasonPhrase()).getBytes(CharsetUtil.US_ASCII));
buf.writeByte((byte) '\r'); buf.writeByte((byte) '\r');
buf.writeByte((byte) '\n'); buf.writeByte((byte) '\n');
} }

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
final class SpdyCodecUtil { final class SpdyCodecUtil {
@ -251,7 +252,7 @@ final class SpdyCodecUtil {
byte[] SPDY2_DICT_; byte[] SPDY2_DICT_;
try { try {
SPDY2_DICT_ = SPDY2_DICT_S.getBytes("US-ASCII"); SPDY2_DICT_ = SPDY2_DICT_S.getBytes(CharsetUtil.US_ASCII);
// dictionary is null terminated // dictionary is null terminated
SPDY2_DICT_[SPDY2_DICT_.length - 1] = (byte) 0; SPDY2_DICT_[SPDY2_DICT_.length - 1] = (byte) 0;
} catch (Exception e) { } catch (Exception e) {

View File

@ -23,6 +23,7 @@ import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.UnsupportedMessageTypeException; import io.netty.handler.codec.UnsupportedMessageTypeException;
import io.netty.util.CharsetUtil;
import java.util.Set; import java.util.Set;
@ -302,14 +303,14 @@ public class SpdyFrameEncoder extends MessageToByteEncoder<Object> {
ByteBuf headerBlock = Unpooled.buffer(); ByteBuf headerBlock = Unpooled.buffer();
writeLengthField(version, headerBlock, numHeaders); writeLengthField(version, headerBlock, numHeaders);
for (String name: names) { for (String name: names) {
byte[] nameBytes = name.getBytes("UTF-8"); byte[] nameBytes = name.getBytes(CharsetUtil.UTF_8);
writeLengthField(version, headerBlock, nameBytes.length); writeLengthField(version, headerBlock, nameBytes.length);
headerBlock.writeBytes(nameBytes); headerBlock.writeBytes(nameBytes);
int savedIndex = headerBlock.writerIndex(); int savedIndex = headerBlock.writerIndex();
int valueLength = 0; int valueLength = 0;
writeLengthField(version, headerBlock, valueLength); writeLengthField(version, headerBlock, valueLength);
for (String value: headerFrame.getHeaders(name)) { for (String value: headerFrame.getHeaders(name)) {
byte[] valueBytes = value.getBytes("UTF-8"); byte[] valueBytes = value.getBytes(CharsetUtil.UTF_8);
headerBlock.writeBytes(valueBytes); headerBlock.writeBytes(valueBytes);
headerBlock.writeByte(0); headerBlock.writeByte(0);
valueLength += valueBytes.length + 1; valueLength += valueBytes.length + 1;