本文整理匯總了Java中io.netty.channel.embedded.EmbeddedChannel.writeOutbound方法的典型用法代碼示例。如果您正苦於以下問題:Java EmbeddedChannel.writeOutbound方法的具體用法?Java EmbeddedChannel.writeOutbound怎麽用?Java EmbeddedChannel.writeOutbound使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.netty.channel.embedded.EmbeddedChannel
的用法示例。
在下文中一共展示了EmbeddedChannel.writeOutbound方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encode
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
@Tag("fast")
public void encode() {
IsoOnTcpMessage isoOnTcpMessage = new IsoOnTcpMessage(
Unpooled.wrappedBuffer(new byte[]{(byte)0x01,(byte)0x02,(byte)0x03}));
EmbeddedChannel channel = new EmbeddedChannel(new IsoOnTcpProtocol());
channel.writeOutbound(isoOnTcpMessage);
channel.checkException();
Object obj = channel.readOutbound();
assertThat(obj).isInstanceOf(ByteBuf.class);
ByteBuf byteBuf = (ByteBuf) obj;
assertEquals(4 + 3, byteBuf.readableBytes(),
"The TCP on ISO Header should add 4 bytes to the data sent");
assertEquals(IsoOnTcpProtocol.ISO_ON_TCP_MAGIC_NUMBER, byteBuf.getByte(0));
assertEquals(4 + 3, byteBuf.getShort(2),
"The length value in the packet should reflect the size of the entire data being sent");
}
示例2: testEmptyBufferBypass
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testEmptyBufferBypass() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseEncoder());
// Test writing an empty buffer works when the encoder is at ST_INIT.
channel.writeOutbound(Unpooled.EMPTY_BUFFER);
ByteBuf buffer = (ByteBuf) channel.readOutbound();
assertThat(buffer, is(sameInstance(Unpooled.EMPTY_BUFFER)));
// Leave the ST_INIT state.
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
assertTrue(channel.writeOutbound(response));
buffer = (ByteBuf) channel.readOutbound();
assertEquals("HTTP/1.1 200 OK\r\n\r\n", buffer.toString(CharsetUtil.US_ASCII));
buffer.release();
// Test writing an empty buffer works when the encoder is not at ST_INIT.
channel.writeOutbound(Unpooled.EMPTY_BUFFER);
buffer = (ByteBuf) channel.readOutbound();
assertThat(buffer, is(sameInstance(Unpooled.EMPTY_BUFFER)));
assertFalse(channel.finish());
}
示例3: testFullContent
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testFullContent() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
FullHttpResponse res = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(new byte[42]));
res.headers().set(Names.CONTENT_LENGTH, 42);
ch.writeOutbound(res);
assertEncodedResponse(ch);
HttpContent c = (HttpContent) ch.readOutbound();
assertThat(c.content().readableBytes(), is(2));
assertThat(c.content().toString(CharsetUtil.US_ASCII), is("42"));
c.release();
LastHttpContent last = (LastHttpContent) ch.readOutbound();
assertThat(last.content().readableBytes(), is(0));
last.release();
assertThat(ch.readOutbound(), is(nullValue()));
}
示例4: testEmptySplitContent
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
/**
* If the length of the content is unknown, {@link HttpContentEncoder} should not skip encoding the content
* even if the actual length is turned out to be 0.
*/
@Test
public void testEmptySplitContent() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
ch.writeOutbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK));
assertEncodedResponse(ch);
ch.writeOutbound(LastHttpContent.EMPTY_LAST_CONTENT);
HttpContent chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("0"));
assertThat(chunk, is(instanceOf(HttpContent.class)));
chunk.release();
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().isReadable(), is(false));
assertThat(chunk, is(instanceOf(LastHttpContent.class)));
chunk.release();
assertThat(ch.readOutbound(), is(nullValue()));
}
示例5: testEmptyFullContent
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
/**
* If the length of the content is 0 for sure, {@link HttpContentEncoder} should skip encoding.
*/
@Test
public void testEmptyFullContent() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
FullHttpResponse res = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER);
ch.writeOutbound(res);
Object o = ch.readOutbound();
assertThat(o, is(instanceOf(FullHttpResponse.class)));
res = (FullHttpResponse) o;
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue()));
// Content encoding shouldn't be modified.
assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue()));
assertThat(res.content().readableBytes(), is(0));
assertThat(res.content().toString(CharsetUtil.US_ASCII), is(""));
res.release();
assertThat(ch.readOutbound(), is(nullValue()));
}
示例6: testTextWithLen
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
private void testTextWithLen(EmbeddedChannel outChannel, EmbeddedChannel inChannel, int testDataLength) {
String testStr = strTestData.substring(0, testDataLength);
outChannel.writeOutbound(new TextWebSocketFrame(testStr));
// Transfer encoded data into decoder
// Loop because there might be multiple frames (gathering write)
while (true) {
ByteBuf encoded = (ByteBuf) outChannel.readOutbound();
if (encoded != null) {
inChannel.writeInbound(encoded);
} else {
break;
}
}
Object decoded = inChannel.readInbound();
Assert.assertNotNull(decoded);
Assert.assertTrue(decoded instanceof TextWebSocketFrame);
TextWebSocketFrame txt = (TextWebSocketFrame) decoded;
Assert.assertEquals(txt.text(), testStr);
txt.release();
}
示例7: testNonByteBufPassthrough
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testNonByteBufPassthrough() throws Exception {
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
engine.setUseClientMode(false);
EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine));
Object msg1 = new Object();
ch.writeOutbound(msg1);
assertThat(ch.readOutbound(), is(sameInstance(msg1)));
Object msg2 = new Object();
ch.writeInbound(msg2);
assertThat(ch.readInbound(), is(sameInstance(msg2)));
ch.finish();
}
示例8: testEncoder
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testEncoder() {
String writeData = "안녕하세요";
ByteBuf request = Unpooled.wrappedBuffer(writeData.getBytes());
Base64Encoder encoder = new Base64Encoder();
EmbeddedChannel embeddedChannel = new EmbeddedChannel(encoder);
embeddedChannel.writeOutbound(request);
ByteBuf response = (ByteBuf) embeddedChannel.readOutbound();
String expect = "7JWI64WV7ZWY7IS47JqU";
assertEquals(expect, response.toString(Charset.defaultCharset()));
embeddedChannel.finish();
}
示例9: testEmptyFullContent
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
/**
* If the length of the content is 0 for sure, {@link HttpContentEncoder} should skip encoding.
*/
@Test
public void testEmptyFullContent() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
ch.writeInbound(newRequest());
FullHttpResponse res = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER);
ch.writeOutbound(res);
Object o = ch.readOutbound();
assertThat(o, is(instanceOf(FullHttpResponse.class)));
res = (FullHttpResponse) o;
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is(nullValue()));
// Content encoding shouldn't be modified.
assertThat(res.headers().get(Names.CONTENT_ENCODING), is(nullValue()));
assertThat(res.content().readableBytes(), is(0));
assertThat(res.content().toString(CharsetUtil.US_ASCII), is(""));
res.release();
assertThat(ch.readOutbound(), is(nullValue()));
}
示例10: testPrependLengthInLittleEndian
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testPrependLengthInLittleEndian() throws Exception {
final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(ByteOrder.LITTLE_ENDIAN, 4, 0, false));
ch.writeOutbound(msg);
ByteBuf buf = (ByteBuf) ch.readOutbound();
assertEquals(5, buf.readableBytes());
byte[] writtenBytes = new byte[buf.readableBytes()];
buf.getBytes(0, writtenBytes);
assertEquals(1, writtenBytes[0]);
assertEquals(0, writtenBytes[1]);
assertEquals(0, writtenBytes[2]);
assertEquals(0, writtenBytes[3]);
assertEquals('A', writtenBytes[4]);
buf.release();
assertFalse("The channel must have been completely read", ch.finish());
}
示例11: makeEntitySpawnAdjustment
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
public static void makeEntitySpawnAdjustment(Entity entity, EntityPlayerMP player, int serverX, int serverY, int serverZ)
{
EmbeddedChannel embeddedChannel = channelPair.get(Side.SERVER);
embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.PLAYER);
embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
embeddedChannel.writeOutbound(new FMLMessage.EntityAdjustMessage(entity, serverX, serverY, serverZ));
}
示例12: encodeTest
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void encodeTest() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new RconEncoder());
ByteBuf expected = Unpooled.wrappedBuffer(DatatypeConverter.parseHexBinary("00000000030000006d7920766f696365206973206d792070617373706f72740000"));
try {
channel.writeOutbound(new RconMessage(0, 3, "my voice is my passport"));
ByteBuf buf = (ByteBuf) channel.readOutbound();
Assert.assertEquals("Read message is invalid.", expected, buf);
} finally {
expected.release();
channel.close();
}
}
示例13: testAdjustedLengthLessThanZero
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testAdjustedLengthLessThanZero() throws Exception {
final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4, -2));
try {
ch.writeOutbound(msg);
fail(EncoderException.class.getSimpleName() + " must be raised.");
} catch (EncoderException e) {
// Expected
}
}
示例14: testSplitContent
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testSplitContent() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder());
ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"));
ch.writeOutbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK));
ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[3])));
ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[2])));
ch.writeOutbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(new byte[1])));
assertEncodedResponse(ch);
HttpContent chunk;
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("3"));
chunk.release();
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("2"));
chunk.release();
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1"));
chunk.release();
chunk = (HttpContent) ch.readOutbound();
assertThat(chunk.content().isReadable(), is(false));
assertThat(chunk, is(instanceOf(LastHttpContent.class)));
chunk.release();
assertThat(ch.readOutbound(), is(nullValue()));
}
示例15: testPrependAdjustedLength
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testPrependAdjustedLength() throws Exception {
final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4, -1));
ch.writeOutbound(msg);
final ByteBuf buf = (ByteBuf) ch.readOutbound();
assertThat(buf, is(wrappedBuffer(new byte[]{0, 0, 0, 0, 'A'})));
buf.release();
}