io.netty.buffer.ByteBufUtil#writeAscii ( )源码实例Demo

下面列出了io.netty.buffer.ByteBufUtil#writeAscii ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bazel   文件: HttpUploadHandlerTest.java
/**
 * 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();
}
 
源代码2 项目: netty-4.1.22   文件: Socks4ClientEncoder.java
@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);
    }
}
 
源代码3 项目: crate   文件: SrvUnicastHostsProviderTest.java
/**
 * 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
}
 
源代码4 项目: NioSmtpClient   文件: SmtpSession.java
@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);
}
 
源代码5 项目: product-microgateway   文件: Http2Handler.java
@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);
    }
}
 
源代码6 项目: netty-4.1.22   文件: Socks5ClientEncoder.java
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);
}
 
源代码7 项目: cassandra-exporter   文件: JsonFragment.java
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);
}
 
源代码8 项目: java-technology-stack   文件: NettyDataBuffer.java
@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;
}
 
源代码9 项目: netty4.0.27Learn   文件: ByteBufUtilBenchmark.java
@Benchmark
public void writeAsciiString() {
    buffer.resetWriterIndex();
    ByteBufUtil.writeAscii(buffer, ascii);
}
 
源代码10 项目: servicetalk   文件: NettyBuffer.java
@Override
public Buffer writeAscii(CharSequence seq) {
    ByteBufUtil.writeAscii(buffer, seq);
    return this;
}
 
源代码11 项目: netty4.0.27Learn   文件: ByteBufUtilBenchmark.java
@Benchmark
public void writeAscii() {
    buffer.resetWriterIndex();
    ByteBufUtil.writeAscii(buffer, asciiSequence);
}
 
源代码12 项目: netty4.0.27Learn   文件: ByteBufUtilBenchmark.java
@Benchmark
public void writeAsciiWrapped() {
    wrapped.resetWriterIndex();
    ByteBufUtil.writeAscii(wrapped, asciiSequence);
}
 
源代码13 项目: netty-4.1.22   文件: ByteBufUtilBenchmark.java
@Benchmark
public void writeAsciiString() {
    buffer.resetWriterIndex();
    ByteBufUtil.writeAscii(buffer, ascii);
}
 
源代码14 项目: netty-4.1.22   文件: ByteBufUtilBenchmark.java
@Benchmark
public void writeAsciiStringWrapped() {
    wrapped.resetWriterIndex();
    ByteBufUtil.writeAscii(wrapped, ascii);
}
 
void write(final ByteBuf buffer) {
    ByteBufUtil.writeAscii(buffer, encoded);
}
 
源代码16 项目: netty-4.1.22   文件: ByteBufUtilBenchmark.java
@Benchmark
public void writeAsciiWrapped() {
    wrapped.resetWriterIndex();
    ByteBufUtil.writeAscii(wrapped, asciiSequence);
}
 
源代码17 项目: cassandra-exporter   文件: JsonFragment.java
static void writeNull(final ByteBuf buffer) {
    ByteBufUtil.writeAscii(buffer, "null");
}
 
源代码18 项目: netty-4.1.22   文件: SlicedByteBufBenchmark.java
@Benchmark
public void writeAsciiStringSliceAbstract() {
    slicedAbstractByteBuf.resetWriterIndex();
    ByteBufUtil.writeAscii(slicedAbstractByteBuf, ascii);
}
 
源代码19 项目: netty-4.1.22   文件: SmtpCommand.java
void encode(ByteBuf buffer) {
    ByteBufUtil.writeAscii(buffer, name);
}
 
源代码20 项目: cassandra-exporter   文件: JsonFragment.java
static void writeLong(final ByteBuf buffer, final long l) {
    ByteBufUtil.writeAscii(buffer, Long.toString(l));
}