本文整理汇总了Java中io.netty.buffer.ByteBufUtil.writeAscii方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBufUtil.writeAscii方法的具体用法?Java ByteBufUtil.writeAscii怎么用?Java ByteBufUtil.writeAscii使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.ByteBufUtil
的用法示例。
在下文中一共展示了ByteBufUtil.writeAscii方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
if (!(msg instanceof SmtpRequest)) {
return;
}
boolean release = true;
final ByteBuf buffer = ctx.alloc().buffer();
try {
final SmtpRequest req = (SmtpRequest) msg;
ByteBufUtil.writeAscii(buffer, req.command().name());
writeParameters(req.parameters(), buffer);
buffer.writeBytes(CRLF);
out.add(buffer);
release = false;
} finally {
if (release) {
buffer.release();
}
}
}
示例2: channelRead0
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
if (HttpUtil.is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
boolean keepAlive = HttpUtil.isKeepAlive(req);
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")");
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
ctx.writeAndFlush(response);
}
}
示例3: fromDataString
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
/**
* Read a DeviceConnectInformation from a Base64 encoded String, which was read from a QR Code.
*/
public static DeviceConnectInformation fromDataString(String data) throws IOException {
final ByteBuf base64 = UnpooledByteBufAllocator.DEFAULT.heapBuffer(data.length());
ByteBufUtil.writeAscii(base64, data);
final ByteBuf byteBuf = decode(base64);
if (byteBuf.readableBytes() != DATA_LENGTH) {
throw new IOException("too many bytes encoded");
}
final byte[] addressData = new byte[ADDRESS_LENGTH];
byteBuf.readBytes(addressData);
final InetAddress address = InetAddress.getByAddress(addressData);
final int port = byteBuf.readUnsignedShort();
final byte[] idData = new byte[DeviceID.ID_LENGTH];
byteBuf.readBytes(idData);
final DeviceID id = new DeviceID(idData);
final byte[] encodedToken = new byte[TOKEN_BASE64_LENGTH];
byteBuf.readBytes(encodedToken);
final byte[] token = decodeToken(new String(encodedToken));
return new DeviceConnectInformation(address, port, id, token);
}
示例4: onPingRead
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@Override
public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception {
if (keepAliveManager != null) {
keepAliveManager.onDataReceived();
}
if (!keepAliveEnforcer.pingAcceptable()) {
ByteBuf debugData = ByteBufUtil.writeAscii(ctx.alloc(), "too_many_pings");
goAway(ctx, connection().remote().lastStreamCreated(), Http2Error.ENHANCE_YOUR_CALM.code(),
debugData, ctx.newPromise());
Status status = Status.RESOURCE_EXHAUSTED.withDescription("Too many pings from client");
try {
forcefulClose(ctx, new ForcefulCloseCommand(status), ctx.newPromise());
} catch (Exception ex) {
onError(ctx, ex);
}
}
}
示例5: sendResponseString
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
void sendResponseString(ChannelHandlerContext ctx, int streamId, String responseString) {
ByteBuf content = ctx.alloc().buffer();
ByteBufUtil.writeAscii(content, responseString);
encoder().writeHeaders(
ctx, streamId, createDefaultResponseHeaders(), 0, false, ctx.newPromise());
encoder().writeData(ctx, streamId, content, 0, true, ctx.newPromise());
ctx.flush();
}
示例6: getBdatRequestWithData
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@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);
}
示例7: encode
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, RconMessage message, ByteBuf buf) throws Exception {
buf.writeIntLE(message.getId());
buf.writeIntLE(message.getType());
ByteBufUtil.writeAscii(buf, message.getBody());
// 2 null bytes
buf.writeByte(0);
buf.writeByte(0);
}
示例8: onHeadersRead
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
/**
* If receive a frame with end-of-stream set, send a pre-canned response.
*/
public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers)
throws Exception {
if (headers.isEndStream()) {
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(RESPONSE_BYTES);
ByteBufUtil.writeAscii(content, " - via HTTP/2");
sendResponse(ctx, content);
}
}
示例9: onHeadersRead
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@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, " - via HTTP/2");
sendResponse(ctx, streamId, content);
}
}
示例10: escape
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
public static void escape(@NotNull CharSequence value, @NotNull ByteBuf buffer) {
int length = value.length();
buffer.ensureWritable(length * 2);
buffer.writeByte('"');
int last = 0;
for (int i = 0; i < length; i++) {
char c = value.charAt(i);
String replacement;
if (c < 128) {
replacement = REPLACEMENT_CHARS[c];
if (replacement == null) {
continue;
}
}
else if (c == '\u2028') {
replacement = "\\u2028";
}
else if (c == '\u2029') {
replacement = "\\u2029";
}
else {
continue;
}
if (last < i) {
ByteBufUtilEx.writeUtf8(buffer, value, last, i);
}
ByteBufUtil.writeAscii(buffer, replacement);
last = i + 1;
}
if (last < length) {
ByteBufUtilEx.writeUtf8(buffer, value, last, length);
}
buffer.writeByte('"');
}
示例11: makeASCIIStringMessage
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
public static ByteBuf makeASCIIStringMessage(short cmd, int msgId, String data) {
int dataLength = data.length();
ByteBuf byteBuf = DEFAULT.buffer(HEADER_LENGTH + dataLength)
.writeByte(cmd)
.writeShort(msgId)
.writeShort(dataLength);
ByteBufUtil.writeAscii(byteBuf, data);
return byteBuf;
}
示例12: onHeadersRead
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
/**
* If receive a frame with end-of-stream set, send a pre-canned response.
*
* @param ctx channel handler context
* @param headers headers frame
*/
public void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) {
if (headers.isEndStream()) {
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(RESPONSE_BYTES);
ByteBufUtil.writeAscii(content, " - via HTTP/2");
sendResponse(ctx, content);
}
}
示例13: encode
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
public void encode(String str, ByteBuf target) {
if (str == null) {
return;
}
if (utf8) {
ByteBufUtil.writeUtf8(target, str);
return;
}
if (ascii) {
ByteBufUtil.writeAscii(target, str);
return;
}
CharsetEncoder encoder = CharsetUtil.encoder(charset);
int length = (int) ((double) str.length() * encoder.maxBytesPerChar());
target.ensureWritable(length);
try {
final ByteBuffer dstBuf = target.nioBuffer(0, length);
final int pos = dstBuf.position();
CoderResult cr = encoder.encode(CharBuffer.wrap(str), dstBuf, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = encoder.flush(dstBuf);
if (!cr.isUnderflow()) {
cr.throwException();
}
target.writerIndex(target.writerIndex() + dstBuf.position() - pos);
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
}
示例14: writeIntAsAscii
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
public static void writeIntAsAscii(int value, @NotNull ByteBuf buffer) {
ByteBufUtil.writeAscii(buffer, new StringBuilder().append(value));
}
示例15: writeAsciiString
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@Benchmark
public void writeAsciiString() {
buffer.resetWriterIndex();
ByteBufUtil.writeAscii(buffer, ascii);
}