下面列出了io.netty.buffer.ByteBufUtil#writeShortBE ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void encodeInitialLine(final ByteBuf buf, final HttpMessage message)
throws Exception {
if (message instanceof HttpRequest) {
HttpRequest request = (HttpRequest) message;
ByteBufUtil.copy(request.method().asciiName(), buf);
buf.writeByte(SP);
buf.writeCharSequence(request.uri(), CharsetUtil.UTF_8);
buf.writeByte(SP);
buf.writeCharSequence(request.protocolVersion().toString(), CharsetUtil.US_ASCII);
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
} else if (message instanceof HttpResponse) {
HttpResponse response = (HttpResponse) message;
buf.writeCharSequence(response.protocolVersion().toString(), CharsetUtil.US_ASCII);
buf.writeByte(SP);
ByteBufUtil.copy(response.status().codeAsText(), buf);
buf.writeByte(SP);
buf.writeCharSequence(response.status().reasonPhrase(), CharsetUtil.US_ASCII);
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
} else {
throw new UnsupportedMessageTypeException("Unsupported type "
+ StringUtil.simpleClassName(message));
}
}
@Override
protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception {
response.protocolVersion().encode(buf);
buf.writeByte(SP);
response.status().encode(buf);
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
}
@Override
protected void encodeInitialLine(ByteBuf buf, HttpResponse response) throws Exception {
buf.writeCharSequence(response.protocolVersion().text(), CharsetUtil.US_ASCII);
buf.writeByte(SP);
ByteBufUtil.copy(response.status().codeAsText(), buf);
buf.writeByte(SP);
buf.writeCharSequence(response.status().reasonPhrase(), CharsetUtil.US_ASCII);
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
}
@Override
protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception {
ByteBufUtil.copy(request.method().asciiName(), buf);
// 获取请求资源路径
String uri = request.uri();
if (uri.isEmpty()) {
// Add " / " as absolute path if uri is not present.如果uri不存在,则添加“/”作为绝对路径。
// See http://tools.ietf.org/html/rfc2616#section-5.1.2
ByteBufUtil.writeMediumBE(buf, SPACE_SLASH_AND_SPACE_MEDIUM);
} else {
CharSequence uriCharSequence = uri;
boolean needSlash = false;
int start = uri.indexOf("://");
if (start != -1 && uri.charAt(0) != SLASH) {
start += 3;
// Correctly handle query params.正确处理查询参数。
// See https://github.com/netty/netty/issues/2732
int index = uri.indexOf(QUESTION_MARK, start);
if (index == -1) {
if (uri.lastIndexOf(SLASH) < start) {
needSlash = true;
}
} else {
if (uri.lastIndexOf(SLASH, index) < start) {
uriCharSequence = new StringBuilder(uri).insert(index, SLASH);
}
}
}
buf.writeByte(SP).writeCharSequence(uriCharSequence, CharsetUtil.UTF_8);
if (needSlash) {
// write "/ " after uri
ByteBufUtil.writeShortBE(buf, SLASH_AND_SPACE_SHORT);
} else {
buf.writeByte(SP);
}
}
request.protocolVersion().encode(buf);
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
}
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
if (msg instanceof SmtpRequest) {
final SmtpRequest req = (SmtpRequest) msg;
if (contentExpected) {
if (req.command().equals(SmtpCommand.RSET)) {
contentExpected = false;
} else {
throw new IllegalStateException("SmtpContent expected");
}
}
boolean release = true;
final ByteBuf buffer = ctx.alloc().buffer();
try {
req.command().encode(buffer);
writeParameters(req.parameters(), buffer);
ByteBufUtil.writeShortBE(buffer, CRLF_SHORT);
out.add(buffer);
release = false;
if (req.command().isContentExpected()) {
contentExpected = true;
}
} finally {
if (release) {
buffer.release();
}
}
}
if (msg instanceof SmtpContent) {
if (!contentExpected) {
throw new IllegalStateException("No SmtpContent expected");
}
final ByteBuf content = ((SmtpContent) msg).content();
out.add(content.retain());
if (msg instanceof LastSmtpContent) {
out.add(DOT_CRLF_BUFFER.retainedDuplicate());
contentExpected = false;
}
}
}
@Override
protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception {
ByteBufUtil.copy(request.method().asciiName(), buf);
String uri = request.uri();
if (uri.isEmpty()) {
// Add " / " as absolute path if uri is not present.
// See http://tools.ietf.org/html/rfc2616#section-5.1.2
ByteBufUtil.writeMediumBE(buf, SPACE_SLASH_AND_SPACE_MEDIUM);
} else {
CharSequence uriCharSequence = uri;
boolean needSlash = false;
int start = uri.indexOf("://");
if (start != -1 && uri.charAt(0) != SLASH) {
start += 3;
// Correctly handle query params.
// See https://github.com/netty/netty/issues/2732
int index = uri.indexOf(QUESTION_MARK, start);
if (index == -1) {
if (uri.lastIndexOf(SLASH) < start) {
needSlash = true;
}
} else {
if (uri.lastIndexOf(SLASH, index) < start) {
uriCharSequence = new StringBuilder(uri).insert(index, SLASH);
}
}
}
buf.writeByte(SP).writeCharSequence(uriCharSequence, CharsetUtil.UTF_8);
if (needSlash) {
// write "/ " after uri
ByteBufUtil.writeShortBE(buf, SLASH_AND_SPACE_SHORT);
} else {
buf.writeByte(SP);
}
}
buf.writeCharSequence(request.protocolVersion().text(), CharsetUtil.US_ASCII);
ByteBufUtil.writeShortBE(buf, CRLF_SHORT);
}