Search in sources :

Example 41 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class AsciiStringBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    byte[] bytes = new byte[size];
    random.nextBytes(bytes);
    asciiString = new AsciiString(bytes, false);
    string = new String(bytes, CharsetUtil.US_ASCII);
}
Also used : AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString) Setup(org.openjdk.jmh.annotations.Setup)

Example 42 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class HeadersBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    Map<String, String> headers = ExampleHeaders.EXAMPLES.get(exampleHeader);
    httpNames = new AsciiString[headers.size()];
    http2Names = new AsciiString[headers.size()];
    httpValues = new AsciiString[headers.size()];
    httpHeaders = new DefaultHttpHeaders(false);
    http2Headers = new DefaultHttp2Headers(false);
    int idx = 0;
    for (Map.Entry<String, String> header : headers.entrySet()) {
        String name = header.getKey();
        String httpName = toHttpName(name);
        String http2Name = toHttp2Name(name);
        String value = header.getValue();
        httpNames[idx] = new AsciiString(httpName);
        http2Names[idx] = new AsciiString(http2Name);
        httpValues[idx] = new AsciiString(value);
        httpHeaders.add(httpNames[idx], httpValues[idx]);
        http2Headers.add(http2Names[idx], httpValues[idx]);
        idx++;
    }
    slowHttp2Headers = new SlowHeaders(http2Headers);
    emptyHttpHeaders = new DefaultHttpHeaders(true);
    emptyHttp2Headers = new DefaultHttp2Headers(true);
    emptyHttpHeadersNoValidate = new DefaultHttpHeaders(false);
    emptyHttp2HeadersNoValidate = new DefaultHttp2Headers(false);
}
Also used : DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString) Map(java.util.Map) Setup(org.openjdk.jmh.annotations.Setup)

Example 43 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class ReadOnlyHttp2HeadersBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    headerNames = new AsciiString[headerCount];
    headerValues = new AsciiString[headerCount];
    for (int i = 0; i < headerCount; ++i) {
        headerNames[i] = new AsciiString("key-" + i);
        headerValues[i] = new AsciiString(UUID.randomUUID().toString());
    }
}
Also used : AsciiString(io.netty.util.AsciiString) Setup(org.openjdk.jmh.annotations.Setup)

Example 44 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class ByteBufUtil method writeAscii.

/**
     * Encode a {@link CharSequence} in <a href="http://en.wikipedia.org/wiki/ASCII">ASCII</a> and write it
     * to a {@link ByteBuf}.
     *
     * This method returns the actual number of bytes written.
     */
public static int writeAscii(ByteBuf buf, CharSequence seq) {
    // ASCII uses 1 byte per char
    final int len = seq.length();
    buf.ensureWritable(len);
    if (seq instanceof AsciiString) {
        AsciiString asciiString = (AsciiString) seq;
        buf.writeBytes(asciiString.array(), asciiString.arrayOffset(), asciiString.length());
    } else {
        for (; ; ) {
            if (buf instanceof AbstractByteBuf) {
                AbstractByteBuf byteBuf = (AbstractByteBuf) buf;
                int written = writeAscii(byteBuf, byteBuf.writerIndex, seq, len);
                byteBuf.writerIndex += written;
                return written;
            } else if (buf instanceof WrappedByteBuf) {
                // Unwrap as the wrapped buffer may be an AbstractByteBuf and so we can use fast-path.
                buf = buf.unwrap();
            } else {
                buf.writeBytes(seq.toString().getBytes(CharsetUtil.US_ASCII));
            }
        }
    }
    return len;
}
Also used : AsciiString(io.netty.util.AsciiString)

Example 45 with AsciiString

use of io.netty.util.AsciiString in project netty by netty.

the class HttpRequestEncoder method encodeInitialLine.

@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);
}
Also used : AsciiString(io.netty.util.AsciiString) AsciiString(io.netty.util.AsciiString)

Aggregations

AsciiString (io.netty.util.AsciiString)73 Test (org.junit.Test)43 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)27 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)25 ByteBuf (io.netty.buffer.ByteBuf)18 ChannelPromise (io.netty.channel.ChannelPromise)15 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)12 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)12 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)12 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)11 Http2Headers (io.netty.handler.codec.http2.Http2Headers)9 Metadata (io.grpc.Metadata)8 ChannelFuture (io.netty.channel.ChannelFuture)4 Status (io.grpc.Status)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Setup (org.openjdk.jmh.annotations.Setup)3 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2