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

The following examples show how to use io.netty.buffer.ByteBufUtil#copy() . 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: HttpHeaders.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static void encodeAscii(CharSequence seq, ByteBuf buf) {
    if (seq instanceof AsciiString) {
        ByteBufUtil.copy((AsciiString) seq, 0, buf, seq.length());
    } else {
        buf.writeCharSequence(seq, CharsetUtil.US_ASCII);
    }
}
 
Example 3
Source File: HttpHeadersEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void writeAscii(ByteBuf buf, int offset, CharSequence value) {
    if (value instanceof AsciiString) {
        ByteBufUtil.copy((AsciiString) value, 0, buf, offset, value.length());
    } else {
        buf.setCharSequence(offset, value, CharsetUtil.US_ASCII);
    }
}
 
Example 4
Source File: HttpResponseStatus.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
void encode(ByteBuf buf) {
    if (bytes == null) {
        ByteBufUtil.copy(codeAsText, buf);
        buf.writeByte(SP);
        buf.writeCharSequence(reasonPhrase, CharsetUtil.US_ASCII);
    } else {
        buf.writeBytes(bytes);
    }
}
 
Example 5
Source File: AsciiHeadersEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void writeAscii(ByteBuf buf, int offset, CharSequence value) {
    if (value instanceof AsciiString) {
        ByteBufUtil.copy((AsciiString) value, 0, buf, offset, value.length());
    } else {
        buf.setCharSequence(offset, value, CharsetUtil.US_ASCII);
    }
}
 
Example 6
Source File: HttpHeadersEncoder.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
private static void writeAscii(ByteBuf buf, int offset, CharSequence value) {
    if (value instanceof AsciiString) {
        ByteBufUtil.copy((AsciiString) value, 0, buf, offset, value.length());
    } else {
        buf.setCharSequence(offset, value, CharsetUtil.US_ASCII);
    }
}
 
Example 7
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 8
Source File: ArmeriaServerCall.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void writeAscii(ByteBuf buf, int offset, CharSequence value, int valueLen) {
    if (value instanceof AsciiString) {
        ByteBufUtil.copy((AsciiString) value, 0, buf, offset, valueLen);
    } else {
        writeCharSequence(buf, offset, value, valueLen);
    }
}
 
Example 9
Source File: HttpRequestEncoderInsertBenchmark.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 {
    AsciiString method = request.method().asciiName();
    ByteBufUtil.copy(method, method.arrayOffset(), buf, method.length());
    buf.writeByte(SP);

    // Add / as absolute path if no is present.
    // See http://tools.ietf.org/html/rfc2616#section-5.1.2
    String uri = request.uri();

    if (uri.isEmpty()) {
        uri += SLASH;
    } else {
        int start = uri.indexOf("://");
        if (start != -1 && uri.charAt(0) != SLASH) {
            int startIndex = start + 3;
            // Correctly handle query params.
            // See https://github.com/netty/netty/issues/2732
            int index = uri.indexOf(QUESTION_MARK, startIndex);
            if (index == -1) {
                if (uri.lastIndexOf(SLASH) <= startIndex) {
                    uri += SLASH;
                }
            } else {
                if (uri.lastIndexOf(SLASH, index) <= startIndex) {
                    int len = uri.length();
                    StringBuilder sb = new StringBuilder(len + 1);
                    sb.append(uri, 0, index)
                            .append(SLASH)
                            .append(uri, index, len);
                    uri = sb.toString();
                }
            }
        }
    }

    buf.writeBytes(uri.getBytes(CharsetUtil.UTF_8));

    buf.writeByte(SP);
    request.protocolVersion().encode(buf);
    buf.writeBytes(CRLF);
}
 
Example 10
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 11
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);
}