本文整理汇总了Java中io.netty.buffer.Unpooled类的典型用法代码示例。如果您正苦于以下问题:Java Unpooled类的具体用法?Java Unpooled怎么用?Java Unpooled使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Unpooled类属于io.netty.buffer包,在下文中一共展示了Unpooled类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testEncodings
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
public void testEncodings() throws Exception {
final PatternChunkSplitter splitter = new PatternChunkSplitter("^(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)");
// "Feb 20 17:05:18 Hällö Wörld\nFeb 20 17:05:18 Büe\n" in ISO-8859-1 encoding
final byte[] bytes = new byte[]{
0x46, 0x65, 0x62, 0x20, 0x32, 0x30, 0x20, 0x31, 0x37, 0x3a, 0x30, 0x35, 0x3a, 0x31, 0x38, 0x20,
0x48, (byte) 0xe4, 0x6c, 0x6c, (byte) 0xf6, 0x20, 0x57, (byte) 0xf6, 0x72, 0x6c, 0x64, 0x0a,
0x46, 0x65, 0x62, 0x20, 0x32, 0x30, 0x20, 0x31, 0x37, 0x3a, 0x30, 0x35, 0x3a, 0x31, 0x38, 0x20,
0x42, (byte) 0xfc, 0x65, 0x0a
};
// With correct encoding
final ByteBuf buffer = Unpooled.copiedBuffer(bytes);
final Iterator<String> iterator = splitter.splitRemaining(buffer, ISO_8859_1).iterator();
assertEquals("Feb 20 17:05:18 Hällö Wörld\n", iterator.next());
assertEquals("Feb 20 17:05:18 Büe\n", iterator.next());
// With wrong encoding
final ByteBuf buffer2 = Unpooled.copiedBuffer(bytes);
final Iterator<String> iterator2 = splitter.splitRemaining(buffer2, UTF_8).iterator();
assertNotEquals("Feb 20 17:05:18 Hällö Wörld\n", iterator2.next());
assertNotEquals("Feb 20 17:05:18 Büe\n", iterator2.next());
}
示例2: testDeserialize
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
public void testDeserialize() throws Exception {
ByteBuf buf = Unpooled.buffer(16);
buf.writeInt(0);
buf.writeInt(1);
buf.writeLong(2);
buf.writeLong(3);
buf.writeInt(4);
buf.writeInt(3);
buf.writeInt(-1);
buf.writeInt(-2);
buf.writeInt(-3);
serverDenseIntRow.deserialize(buf);
assertEquals(serverDenseIntRow.getRowId(), 0);
assertEquals(serverDenseIntRow.getClock(), 1);
assertEquals(serverDenseIntRow.getStartCol(), 2);
assertEquals(serverDenseIntRow.getEndCol(), 3);
assertEquals(serverDenseIntRow.getRowVersion(), 4);
assertEquals(serverDenseIntRow.getData().get(0), -1, 0.0);
assertEquals(serverDenseIntRow.getData().get(1), -2, 0.0);
assertEquals(serverDenseIntRow.getData().get(2), -3, 0.0);
}
示例3: writeBack
import io.netty.buffer.Unpooled; //导入依赖的package包/类
public static int writeBack(Channel channel, boolean isSuccess, String resultStr, boolean isKeepAlive) {
ByteBuf content = Unpooled.copiedBuffer(resultStr, Constants.DEFAULT_CHARSET);
HttpResponseStatus status;
if (isSuccess) {
status = HttpResponseStatus.OK;
} else {
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
}
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, status, content);
//logger.info("result str:{}", resultStr);
res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
HttpHeaders.setContentLength(res, content.readableBytes());
try {
ChannelFuture f = channel.writeAndFlush(res);
if (isKeepAlive) {
HttpHeaders.setKeepAlive(res, true);
} else {
HttpHeaders.setKeepAlive(res, false);//set keepalive closed
f.addListener(ChannelFutureListener.CLOSE);
}
} catch (Exception e2) {
logger.warn("Failed to send HTTP response to remote, cause by:", e2);
}
return content.readableBytes();
}
示例4: testReadFrom
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
public void testReadFrom() throws Exception {
ByteBuf buf = Unpooled.buffer(16);
buf.writeFloat((float) 10.00);
buf.writeFloat((float) 11.00);
buf.writeFloat((float) 12.00);
serverDenseFloatRow.update(RowType.T_FLOAT_DENSE, buf, 3);
DataOutputStream out = new DataOutputStream(new FileOutputStream("data"));
serverDenseFloatRow.writeTo(out);
out.close();
DataInputStream in = new DataInputStream(new FileInputStream("data"));
ServerDenseFloatRow newServerDenseFloatRow = new ServerDenseFloatRow(rowId, startCol, endCol);
newServerDenseFloatRow.readFrom(in);
assertEquals(newServerDenseFloatRow.getData().get(0), serverDenseFloatRow.getData().get(0),
0.00);
assertEquals(newServerDenseFloatRow.getData().get(1), serverDenseFloatRow.getData().get(1),
0.00);
assertEquals(newServerDenseFloatRow.getData().get(2), serverDenseFloatRow.getData().get(2),
0.00);
}
示例5: fireAndForget
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Override
public Mono<Void> fireAndForget(Payload payload) {
try {
ByteBuf metadata = Unpooled.wrappedBuffer(payload.getMetadata());
int namespaceId = ProteusMetadata.namespaceId(metadata);
int serviceId = ProteusMetadata.serviceId(metadata);
ProteusService proteusService = getService(namespaceId, serviceId);
if (proteusService == null) {
return Mono.error(new ServiceNotFound(namespaceId, serviceId));
}
return proteusService.fireAndForget(payload);
} catch (Throwable t) {
return Mono.error(t);
}
}
示例6: testDeserialize
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
public void testDeserialize() throws Exception {
ByteBuf buf = Unpooled.buffer(16);
buf.writeInt(0);
buf.writeInt(1);
buf.writeLong(2);
buf.writeLong(3);
buf.writeInt(4);
buf.writeInt(3);
buf.writeFloat((float) -1.0);
buf.writeFloat((float) -2.0);
buf.writeFloat((float) -3.0);
serverDenseFloatRow.deserialize(buf);
assertEquals(serverDenseFloatRow.getRowId(), 0);
assertEquals(serverDenseFloatRow.getClock(), 1);
assertEquals(serverDenseFloatRow.getStartCol(), 2);
assertEquals(serverDenseFloatRow.getEndCol(), 3);
assertEquals(serverDenseFloatRow.getRowVersion(), 4);
assertEquals(serverDenseFloatRow.getData().get(0), -1, 0.0);
assertEquals(serverDenseFloatRow.getData().get(1), -2, 0.0);
assertEquals(serverDenseFloatRow.getData().get(2), -3, 0.0);
}
示例7: testEncodeDecode
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
public void testEncodeDecode() throws Exception {
BasicCancel testFrame = new BasicCancel(1,
ShortString.parseString("1"),
true);
ByteBuf buf = Unpooled.buffer((int) testFrame.getMethodBodySize());
testFrame.writeMethod(buf);
BasicCancel decodedFrame = (BasicCancel) BasicCancel.getFactory()
.newInstance(buf, 1, testFrame.getMethodBodySize());
Assert.assertEquals(decodedFrame.getChannel(), testFrame.getChannel(), "Decoded frame's channel should match"
+ " the original frame's channel");
Assert.assertEquals(decodedFrame.getConsumerTag(), testFrame.getConsumerTag(), "Decoded frame's consumer-tag "
+ "should match the original frame's consumer-tag");
Assert.assertEquals(decodedFrame.isNoWait(), testFrame.isNoWait(), "Decoded frame's no-wait bit "
+ "should match the original frame's no-wait bit");
}
示例8: testEncodeDecode
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
public void testEncodeDecode() throws Exception {
QueueBind testFrame = new QueueBind(1,
ShortString.parseString("queue"),
ShortString.parseString("amq.topic"),
ShortString.parseString("a.b.c"),
true,
FieldTable.EMPTY_TABLE);
ByteBuf buf = Unpooled.buffer((int) testFrame.getMethodBodySize());
testFrame.writeMethod(buf);
QueueBind decodedFrame = (QueueBind) QueueBind.getFactory()
.newInstance(buf, 1, testFrame.getMethodBodySize());
Assert.assertEquals(decodedFrame.getChannel(), testFrame.getChannel(), "Decoded frame's channel should match"
+ " the original frame's channel");
Assert.assertEquals(decodedFrame.getQueue(), testFrame.getQueue(), "Decoded frame's queue should match"
+ " the original frame's queue");
Assert.assertEquals(decodedFrame.getExchange(), testFrame.getExchange(), "Decoded frame's exchange "
+ "should match the original frame's exchange");
Assert.assertEquals(decodedFrame.getRoutingKey(), testFrame.getRoutingKey(), "Decoded frame's routing-key "
+ "should match the original frame's no-local bit");
Assert.assertEquals(decodedFrame.isNoWait(), testFrame.isNoWait(), "Decoded frame's no-wait bit "
+ "should match the original frame's no-wait bit");
Assert.assertEquals(decodedFrame.getArguments(), testFrame.getArguments(), "Decoded frame's arguments "
+ "should match the original frame's arguments");
}
示例9: channelRead
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
boolean keepAlive = HttpUtil.isKeepAlive(req);
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT));
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, KEEP_ALIVE);
ctx.write(response);
}
}
}
示例10: channelRead
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if(msg instanceof HttpContent){
System.out.println(msg);
}
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
String path = URI.create(req.getUri()).getPath();
boolean keepAlive = HttpHeaders.isKeepAlive(req);
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT));
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, Values.KEEP_ALIVE);
ctx.write(response);
}
}
}
示例11: testUpdate
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
public void testUpdate() throws Exception {
serverDenseIntRow = new ServerDenseIntRow(rowId, startCol, endCol);
ByteBuf buf = Unpooled.buffer(16);
buf.writeInt(0);
buf.writeInt(1);
buf.writeInt(-1);
int newValue0 = buf.getInt(0) + serverDenseIntRow.getData().get(0);
int newValue1 = buf.getInt(4) + serverDenseIntRow.getData().get(1);
serverDenseIntRow.update(RowType.T_INT_DENSE, buf, 3);
assertEquals(serverDenseIntRow.getData().get(0), newValue0, 0.000);
assertEquals(serverDenseIntRow.getData().get(1), newValue1, 0.000);
assertEquals(serverDenseIntRow.getData().get(2), -1, 0.000);
serverDenseIntRow = new ServerDenseIntRow(rowId, startCol, endCol);
buf = Unpooled.buffer(0);
buf.writeInt(0);
buf.writeInt(1);
buf.writeInt(2);
buf.writeInt(-2);
serverDenseIntRow.update(RowType.T_INT_SPARSE, buf, 2);
assertEquals(serverDenseIntRow.getData().get(0), 1, 0.000);
assertEquals(serverDenseIntRow.getData().get(1), 0, 0.000);
assertEquals(serverDenseIntRow.getData().get(2), -2, 0.000);
}
示例12: decodeDisconnectionConfirm
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Test
@Tag("fast")
public void decodeDisconnectionConfirm() throws Exception {
ChannelHandlerContext ctx = new MockChannelHandlerContext();
ByteBuf buf = Unpooled.buffer();
ArrayList<Object> out = new ArrayList<>();
buf.writeByte(0x5); // header length
buf.writeByte(TpduCode.DISCONNECT_CONFIRM.getCode());
buf.writeShort(0x01); // destination reference
buf.writeShort(0x02); // source reference
buf.writeByte(DisconnectReason.NORMAL.getCode());
IsoOnTcpMessage in = new IsoOnTcpMessage(buf);
isoTPProtocol.decode(ctx, in, out);
assertTrue(out.size() == 1, "Message not decoded");
DisconnectConfirmTpdu requestTpdu = (DisconnectConfirmTpdu) ((IsoTPMessage)out.get(0)).getTpdu();
assertTrue(requestTpdu.getTpduCode() == TpduCode.DISCONNECT_CONFIRM, "Message code not correct");
assertTrue(requestTpdu.getDestinationReference() == (short) 0x1, "Message destination reference not correct");
assertTrue(requestTpdu.getSourceReference() == (short) 0x2, "Message source reference not correct");
assertTrue(requestTpdu.getParameters().isEmpty(), "Message contains paramaters");
}
示例13: newNonSslHandler
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Override
protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
return new ChannelInboundHandlerAdapter() {
private HttpResponseEncoder encoder = new HttpResponseEncoder();
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
LOG.trace("Received non-SSL request, returning redirect");
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.MOVED_PERMANENTLY, Unpooled.EMPTY_BUFFER);
response.headers().set(Names.LOCATION, redirectAddress);
LOG.trace(Constants.LOG_RETURNING_RESPONSE, response);
encoder.write(ctx, response, ctx.voidPromise());
ctx.flush();
}
};
}
示例14: decode
import io.netty.buffer.Unpooled; //导入依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
throws Exception {
byte[] data;
int offset;
int length = msg.readableBytes();
if (msg.hasArray()) {
data = msg.array();
offset = msg.arrayOffset();
msg.skipBytes(length);
} else {
data = new byte[length];
msg.readBytes(data);
offset = 0;
}
out.add(Unpooled.wrappedBuffer(backend.unwrap(data, offset, length)));
}
示例15: main
import io.netty.buffer.Unpooled; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
Bootstrap b = new Bootstrap();
b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
}
});
b.connect("localhost", 8090).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
future.channel().write(Unpooled.buffer().writeBytes("123".getBytes()));
future.channel().flush();
future.channel().close();
}
}
});
}