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

The following examples show how to use io.netty.buffer.ByteBufUtil#writeAscii() . 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: SrvUnicastHostsProviderTest.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Copied over from {@link DefaultDnsRecordEncoder#encodeName(String, ByteBuf)} as it is not accessible.
 */
private void encodeName(String name, ByteBuf buf) {
    if (".".equals(name)) {
        // Root domain
        buf.writeByte(0);
        return;
    }

    final String[] labels = name.split("\\.");
    for (String label : labels) {
        final int labelLen = label.length();
        if (labelLen == 0) {
            // zero-length label means the end of the name.
            break;
        }

        buf.writeByte(labelLen);
        ByteBufUtil.writeAscii(buf, label);
    }

    buf.writeByte(0); // marks end of name field
}
 
Example 2
Source File: Socks4ClientEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, Socks4CommandRequest msg, ByteBuf out) throws Exception {
    out.writeByte(msg.version().byteValue());
    out.writeByte(msg.type().byteValue());
    out.writeShort(msg.dstPort());
    if (NetUtil.isValidIpV4Address(msg.dstAddr())) {
        out.writeBytes(NetUtil.createByteArrayFromIpAddressString(msg.dstAddr()));
        ByteBufUtil.writeAscii(out, msg.userId());
        out.writeByte(0);
    } else {
        out.writeBytes(IPv4_DOMAIN_MARKER);
        ByteBufUtil.writeAscii(out, msg.userId());
        out.writeByte(0);
        ByteBufUtil.writeAscii(out, msg.dstAddr());
        out.writeByte(0);
    }
}
 
Example 3
Source File: HttpUploadHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND) with a
 * Content-Length header.
 */
@Test
public void httpErrorsWithContentAreSupported() {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpUploadHandler(null, ImmutableList.of()));
  ByteArrayInputStream data = new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5});
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(new UploadCommand(CACHE_URI, true, "abcdef", data, 5), writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request).isInstanceOf(HttpRequest.class);
  HttpChunkedInput content = ch.readOutbound();
  assertThat(content).isInstanceOf(HttpChunkedInput.class);

  ByteBuf errorMsg = ByteBufUtil.writeAscii(ch.alloc(), "error message");
  FullHttpResponse response =
      new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, errorMsg);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

  ch.writeInbound(response);

  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).response().status())
      .isEqualTo(HttpResponseStatus.NOT_FOUND);
  assertThat(ch.isOpen()).isTrue();
}
 
Example 4
Source File: NettyDataBuffer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public DataBuffer write(CharSequence charSequence, Charset charset) {
	Assert.notNull(charSequence, "CharSequence must not be null");
	Assert.notNull(charset, "Charset must not be null");
	if (StandardCharsets.UTF_8.equals(charset)) {
		ByteBufUtil.writeUtf8(this.byteBuf, charSequence);
	}
	else if (StandardCharsets.US_ASCII.equals(charset)) {
		ByteBufUtil.writeAscii(this.byteBuf, charSequence);
	}
	else {
		return PooledDataBuffer.super.write(charSequence, charset);
	}
	return this;
}
 
Example 5
Source File: JsonFragment.java    From cassandra-exporter with Apache License 2.0 5 votes vote down vote up
static void writeFloat(final ByteBuf buffer, final float f) {
    if (Float.isNaN(f)) {
        ByteBufUtil.writeAscii(buffer, "\"NaN\"");
        return;
    }

    if (Float.isInfinite(f)) {
        ByteBufUtil.writeAscii(buffer, (f < 0 ? "\"-Inf\"" : "\"+Inf\""));
        return;
    }

    Floats.writeFloatString(buffer, f);
}
 
Example 6
Source File: Http2Handler.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId,
                          Http2Headers headers, int padding, boolean endOfStream) {
    if (endOfStream) {
        ByteBuf content = ctx.alloc().buffer();
        content.writeBytes(RESPONSE_BYTES.duplicate());
        ByteBufUtil.writeAscii(content, " -established");
        sendResponse(ctx, streamId, content);
    }
}
 
Example 7
Source File: Socks5ClientEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void encodePasswordAuthRequest(Socks5PasswordAuthRequest msg, ByteBuf out) {
    out.writeByte(0x01);

    final String username = msg.username();
    out.writeByte(username.length());
    ByteBufUtil.writeAscii(out, username);

    final String password = msg.password();
    out.writeByte(password.length());
    ByteBufUtil.writeAscii(out, password);
}
 
Example 8
Source File: JsonFragment.java    From cassandra-exporter with Apache License 2.0 4 votes vote down vote up
static void writeNull(final ByteBuf buffer) {
    ByteBufUtil.writeAscii(buffer, "null");
}
 
Example 9
Source File: JsonFragment.java    From cassandra-exporter with Apache License 2.0 4 votes vote down vote up
static void writeLong(final ByteBuf buffer, final long l) {
    ByteBufUtil.writeAscii(buffer, Long.toString(l));
}
 
Example 10
Source File: SmtpCommand.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
void encode(ByteBuf buffer) {
    ByteBufUtil.writeAscii(buffer, name);
}
 
Example 11
Source File: SlicedByteBufBenchmark.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void writeAsciiStringSliceAbstract() {
    slicedAbstractByteBuf.resetWriterIndex();
    ByteBufUtil.writeAscii(slicedAbstractByteBuf, ascii);
}
 
Example 12
Source File: ByteBufUtilBenchmark.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void writeAsciiString() {
    buffer.resetWriterIndex();
    ByteBufUtil.writeAscii(buffer, ascii);
}
 
Example 13
Source File: ByteBufUtilBenchmark.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void writeAsciiWrapped() {
    wrapped.resetWriterIndex();
    ByteBufUtil.writeAscii(wrapped, asciiSequence);
}
 
Example 14
Source File: TextFormatMetricFamilyWriter.java    From cassandra-exporter with Apache License 2.0 4 votes vote down vote up
void write(final ByteBuf buffer) {
    ByteBufUtil.writeAscii(buffer, encoded);
}
 
Example 15
Source File: ByteBufUtilBenchmark.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void writeAsciiStringWrapped() {
    wrapped.resetWriterIndex();
    ByteBufUtil.writeAscii(wrapped, ascii);
}
 
Example 16
Source File: ByteBufUtilBenchmark.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void writeAsciiString() {
    buffer.resetWriterIndex();
    ByteBufUtil.writeAscii(buffer, ascii);
}
 
Example 17
Source File: ByteBufUtilBenchmark.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void writeAsciiWrapped() {
    wrapped.resetWriterIndex();
    ByteBufUtil.writeAscii(wrapped, asciiSequence);
}
 
Example 18
Source File: ByteBufUtilBenchmark.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void writeAscii() {
    buffer.resetWriterIndex();
    ByteBufUtil.writeAscii(buffer, asciiSequence);
}
 
Example 19
Source File: SmtpSession.java    From NioSmtpClient with Apache License 2.0 4 votes vote down vote up
@SuppressFBWarnings("VA_FORMAT_STRING_USES_NEWLINE") // we shouldn't use platform-specific newlines for SMTP
private ByteBuf getBdatRequestWithData(ByteBuf data, boolean isLast) {
  String request = String.format("BDAT %d%s\r\n", data.readableBytes(), isLast ? " LAST" : "");
  ByteBuf requestBuf = channel.alloc().buffer(request.length());
  ByteBufUtil.writeAscii(requestBuf, request);

  return channel.alloc().compositeBuffer().addComponents(true, requestBuf, data);
}
 
Example 20
Source File: NettyBuffer.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Override
public Buffer writeAscii(CharSequence seq) {
    ByteBufUtil.writeAscii(buffer, seq);
    return this;
}