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);
}
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);
}
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());
}
}
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;
}
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);
}
Aggregations