Java Code Examples for io.netty.buffer.ByteBufUtil#writeShortBE()

The following examples show how to use io.netty.buffer.ByteBufUtil#writeShortBE() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RtspEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@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));
    }
}
 
Example 2
Source File: HttpResponseEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 3
Source File: BrpcHttpResponseEncoder.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: HttpRequestEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@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);
    }
 
Example 5
Source File: SmtpRequestEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@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;
        }
    }
}
 
Example 6
Source File: BrpcHttpRequestEncoder.java    From brpc-java with Apache License 2.0 4 votes vote down vote up
@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);
}