本文整理汇总了Java中com.alibaba.dubbo.remoting.buffer.ChannelBuffer类的典型用法代码示例。如果您正苦于以下问题:Java ChannelBuffer类的具体用法?Java ChannelBuffer怎么用?Java ChannelBuffer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelBuffer类属于com.alibaba.dubbo.remoting.buffer包,在下文中一共展示了ChannelBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleWrite
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
Connection<?> connection = context.getConnection();
GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
try {
ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(1024); // 不需要关闭
Object msg = context.getMessage();
codec.encode(channel, channelBuffer, msg);
GrizzlyChannel.removeChannelIfDisconnectd(connection);
Buffer buffer = connection.getTransport().getMemoryManager().allocate(channelBuffer.readableBytes());
buffer.put(channelBuffer.toByteBuffer());
buffer.flip();
buffer.allowBufferDispose(true);
context.setMessage(buffer);
} finally {
GrizzlyChannel.removeChannelIfDisconnectd(connection);
}
return context.getInvokeAction();
}
示例2: testDecode_PersonWithEnterByte
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
private void testDecode_PersonWithEnterByte(byte[] enterbytes ,boolean isNeedmore) throws IOException{
//init channel
Channel channel = getServerSideChannel(url);
//init request string
Person request = new Person();
byte[] newbuf = join(objectToByte(request), enterbytes);
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf);
//decode
Object obj = codec.decode(channel, buffer);
if (isNeedmore){
Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT , obj);
}else {
Assert.assertTrue("return must string ", obj instanceof String);
}
}
示例3: encode
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
public void encode( Channel channel, ChannelBuffer buffer, Object message )
throws IOException {
if ( message instanceof Request ) {
encodeRequest( channel, buffer, ( Request ) message );
}
else if ( message instanceof Response ) {
encodeResponse( channel, buffer, ( Response ) message );
} else {
throw new UnsupportedOperationException(
new StringBuilder( 32 )
.append( "Thrift codec only support encode " )
.append( Request.class.getName() )
.append( " and " )
.append( Response.class.getName() )
.toString() );
}
}
示例4: testMessageLengthExceedPayloadLimitWhenEncode
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
Request request = new Request(1L);
request.setData("hello");
ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
try {
codec.encode(channel, encodeBuffer, request);
Assert.fail();
} catch (IOException e) {
Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
}
Response response = new Response(1L);
response.setResult("hello");
encodeBuffer = ChannelBuffers.dynamicBuffer(512);
channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
codec.encode(channel, encodeBuffer, response);
Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
Response receiveMessage = (Response) channel.getReceivedMessage();
Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
示例5: encodeRequest
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
throws IOException {
Invocation invocation = (Invocation) request.getData();
TProtocol protocol = newProtocol(channel.getUrl(), buffer);
try {
protocol.writeMessageBegin(new TMessage(
invocation.getMethodName(), TMessageType.CALL,
thriftSeq.getAndIncrement()));
protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args"));
for(int i = 0; i < invocation.getParameterTypes().length; i++) {
Class<?> type = invocation.getParameterTypes()[i];
}
} catch (TException e) {
throw new IOException(e.getMessage(), e);
}
}
示例6: decode
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
int save = buffer.readerIndex();
MultiMessage result = MultiMessage.create();
do {
Object obj = codec.decode(channel, buffer);
if (Codec2.DecodeResult.NEED_MORE_INPUT == obj) {
buffer.readerIndex(save);
break;
} else {
result.addMessage(obj);
logMessageLength(obj, buffer.readerIndex() - save);
save = buffer.readerIndex();
}
} while (true);
if (result.isEmpty()) {
return Codec2.DecodeResult.NEED_MORE_INPUT;
}
if (result.size() == 1) {
return result.get(0);
}
return result;
}
示例7: test_Encode_Request
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Test
public void test_Encode_Request() throws IOException{
ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(2014);
Channel channel = getCliendSideChannel(url);
Request request = new Request();
Person person = new Person();
request.setData(person);
codec.encode(channel, encodeBuffer, request);
//encode resault check need decode
byte[] data = new byte[encodeBuffer.writerIndex()];
encodeBuffer.readBytes(data);
ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
Request obj = (Request)codec.decode(channel, decodeBuffer);
Assert.assertEquals(request.isBroken(), obj.isBroken());
Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
Assert.assertEquals(request.isTwoWay(), obj.isTwoWay());
Assert.assertEquals(person, obj.getData());
}
示例8: test_Decode_Error_Length
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Test
public void test_Decode_Error_Length() throws IOException{
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 0x20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Person person = new Person();
byte[] request = getRequestBytes(person, header);
Channel channel = getServerSideChannel(url);
byte[] baddata = new byte[]{1,2};
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(join(request, baddata));
Response obj = (Response)codec.decode(channel, buffer);
Assert.assertEquals(person, obj.getResult());
//only decode necessary bytes
Assert.assertEquals(request.length, buffer.readerIndex());
}
示例9: writeBytes
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Override
public void writeBytes(ChannelBuffer src, int length) {
// careful
if (length > src.readableBytes()) {
throw new IndexOutOfBoundsException();
}
writeBytes(src, src.readerIndex(), length);
src.readerIndex(src.readerIndex() + length);
}
示例10: getBytes
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Override
public void getBytes(int index, ChannelBuffer dst, int length) {
// careful
if (length > dst.writableBytes()) {
throw new IndexOutOfBoundsException();
}
getBytes(index, dst, dst.writerIndex(), length);
dst.writerIndex(dst.writerIndex() + length);
}
示例11: readBytes
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
public void readBytes(ChannelBuffer dst, int length) {
// carefule
if (length > dst.writableBytes()) {
throw new IndexOutOfBoundsException();
}
readBytes(dst, dst.writerIndex(), length);
dst.writerIndex(dst.writerIndex() + length);
}
示例12: setBytes
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
public void setBytes(int index, ChannelBuffer src, int length) {
// careful
if (length > src.readableBytes()) {
throw new IndexOutOfBoundsException();
}
setBytes(index, src, src.readerIndex(), length);
src.readerIndex(src.readerIndex() + length);
}
示例13: writeBytes
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Override
public void writeBytes(ChannelBuffer src, int srcIndex, int length) {
// careful
byte[] data = new byte[length];
src.getBytes(srcIndex, data, 0, length);
writeBytes(data, 0, length);
}
示例14: readBytes
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
@Override
public void readBytes(ChannelBuffer dst, int dstIndex, int length) {
// careful
if (readableBytes() < length) {
throw new IndexOutOfBoundsException();
}
byte[] data = new byte[length];
buffer.readBytes(data, 0, length);
dst.setBytes(dstIndex, data, 0, length);
}
示例15: decode
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; //导入依赖的package包/类
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
byte[] bytes = new byte[buffer.readableBytes()];
int savedReaderIndex = buffer.readerIndex();
buffer.readBytes(bytes);
UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
Object result = codec.decode(channel, is);
buffer.readerIndex(savedReaderIndex + is.position());
return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}