本文整理汇总了Java中org.apache.mina.filter.codec.ProtocolEncoderOutput类的典型用法代码示例。如果您正苦于以下问题:Java ProtocolEncoderOutput类的具体用法?Java ProtocolEncoderOutput怎么用?Java ProtocolEncoderOutput使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ProtocolEncoderOutput类属于org.apache.mina.filter.codec包,在下文中一共展示了ProtocolEncoderOutput类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception
{
ByteBuffer buffer = encoder.encodeMessage( ( Message ) message );
IoBuffer ioBuffer = IoBuffer.wrap( buffer );
if ( IS_DEBUG )
{
byte[] dumpBuffer = new byte[buffer.limit()];
buffer.get( dumpBuffer );
buffer.flip();
CODEC_LOG.debug( "Encoded message \n " + message + "\n : " + Strings.dumpBytes( dumpBuffer ) );
}
out.write( ioBuffer );
}
示例2: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput output ) throws Exception
{
final IoBuffer data = IoBuffer.allocate ( 0 );
data.order ( ByteOrder.LITTLE_ENDIAN );
data.setAutoExpand ( true );
if ( message instanceof WriteRequestMessage )
{
encodeHeader ( data, (CommonMessage)message );
encodeWriteRequest ( data, (WriteRequestMessage)message );
}
else if ( message instanceof CommonMessage )
{
encodeHeader ( data, (CommonMessage)message );
}
data.flip ();
output.write ( data );
}
示例3: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput output ) throws Exception
{
if ( ! ( message instanceof Frame ) )
{
throw new IllegalStateException ( String.format ( "Can only encode messages of type Frame but got %s", message.getClass () ) );
}
final Frame frame = (Frame)message;
if ( logger.isTraceEnabled () )
{
logger.trace ( "Encode frame - type: {}, data: {}", frame.getType (), frame.getData () );
}
final IoBuffer buffer = IoBuffer.allocate ( 1 + 1 + 4 + frame.getData ().remaining () );
buffer.put ( (byte)0x01 ); // version - #0
buffer.put ( (byte)frame.getType ().ordinal () ); // frame type - #1
buffer.putInt ( frame.getData ().remaining () ); // data size - #2
buffer.put ( frame.getData () ); // data - #6
buffer.flip ();
output.write ( buffer );
}
示例4: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
if (!(message instanceof Serializable)) {
throw new NotSerializableException();
}
IoBuffer buf = IoBuffer.allocate(64);
buf.setAutoExpand(true);
buf.putObject(message);
int objectSize = buf.position() - 4;
if (objectSize > maxObjectSize) {
throw new IllegalArgumentException("The encoded object is too big: " + objectSize + " (> " + maxObjectSize
+ ')');
}
buf.flip();
out.write(buf);
}
示例5: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
if (encoder == null) {
encoder = charset.newEncoder();
session.setAttribute(ENCODER, encoder);
}
String value = (message == null ? "" : message.toString());
IoBuffer buf = IoBuffer.allocate(value.length()).setAutoExpand(true);
buf.putString(value, encoder);
if (buf.position() > maxLineLength) {
throw new IllegalArgumentException("Line length: " + buf.position());
}
buf.putString(delimiter.getValue(), encoder);
buf.flip();
out.write(buf);
}
示例6: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode(IoSession session, Object obj, ProtocolEncoderOutput out) throws Exception {
TcpPacket packet = (TcpPacket) obj;
byte[] byteData = packet.getByteData();
int len = 18 + byteData.length;
IoBuffer buf = IoBuffer.allocate(len);
buf.put(HEAD);
buf.putInt(len);
buf.putInt(packet.gettOpCode());
buf.putInt(packet.lockedId);
buf.putInt(packet.unlockedId);
buf.put(byteData);
buf.flip();
out.write(buf);
}
示例7: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode(final IoSession session, final Object message, final ProtocolEncoderOutput out) throws Exception {
final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY);
if (client != null) {
final byte[] input = ((MaplePacket) message).getBytes();
final byte[] unencrypted = new byte[input.length];
System.arraycopy(input, 0, unencrypted, 0, input.length);
final byte[] ret = new byte[unencrypted.length + 4];
final byte[] header = client.getSendCrypto().getPacketHeader(unencrypted.length);
synchronized(client.getSendCrypto()){
MapleCustomEncryption.encryptData(unencrypted);
client.getSendCrypto().crypt(unencrypted);
System.arraycopy(header, 0, ret, 0, 4);
System.arraycopy(unencrypted, 0, ret, 4, unencrypted.length);
final ByteBuffer out_buffer = ByteBuffer.wrap(ret);
out.write(out_buffer);
}
} else { // no client object created yet, send unencrypted (hello)
out.write(ByteBuffer.wrap(((MaplePacket) message).getBytes()));
}
}
示例8: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
if(!(message instanceof byte[])){
throw new Exception("must send byte[]");
}
byte[] payload=(byte[]) message;
ByteBuffer buf = ByteBuffer.allocate(payload.length, false);
buf.put(payload);
buf.flip();
out.write(buf);
if (isDebugEnabled)
LOGGER.debug(TairUtil.toHex(payload));
}
示例9: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
if (genericClass.isInstance(message)) {
byte[] datas = serializer.serialize(message);
IoBuffer buffer = IoBuffer.allocate(256);
buffer.setAutoExpand(true);
buffer.setAutoShrink(true);
buffer.putInt(datas.length);
buffer.put(datas);
buffer.flip();
session.write(buffer);
}
}
示例10: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode(IoSession session, byte[] message, ProtocolEncoderOutput out) throws Exception {
try {
IoBuffer buf = IoBuffer.allocate(1024);
buf.setAutoExpand(true);
int usedLen = 0;
while (usedLen < message.length) {
Packet pkg = Packet.PARSER.parseFrom(message, 0, message.length - usedLen);
log.debug("Server FLUSH:" + pkg.toString());
usedLen += pkg.toByteArray().length;
buf.put(MessageCodec.makePkg(pkg.getCmdId(), pkg.toByteArray()));
}
buf.flip();
out.write(buf);
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: getSendBuffer
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
/**
* 同步处理要发送的数据。
*
* @param bytes
* @return
*/
public void getSendBuffer(byte[] bytes, ProtocolEncoderOutput out,
MapleClient client) {
IoBuffer buffer = IoBuffer.allocate(bytes.length + 4, true);
try {
mutex.lock();
buffer.put(getPacketHeader(bytes.length));
buffer.put(bytes);
buffer.position(buffer.position() - bytes.length);
crypt(buffer, true);
buffer.flip();
// 建立完成待发送数据。
out.write(buffer);
out.flush();
} finally {
mutex.unlock();
}
buffer.free();
}
示例12: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
try {
byte compressedBytes[] = (byte[]) message;
final IoBuffer buffer = IoBuffer.allocate(compressedBytes.length).setAutoExpand(true);
buffer.put(compressedBytes);
buffer.flip();
out.write(buffer);
WriteFuture wf = out.flush();
wf.addListener(new IoFutureListener<IoFuture>() {
@Override
public void operationComplete(IoFuture future) {
buffer.free();
}
});
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode(IoSession is, Object o, ProtocolEncoderOutput peo) throws Exception {
assert o instanceof RequestEnvelope;
RequestEnvelope env = (RequestEnvelope) o;
ByteArrayOutputStream bufOutStream = new ByteArrayOutputStream();
ProtoWriter writer = new ProtoWriter(bufOutStream);
if (env.ticket != null) {
writer.put(CoreTags.TAG_SESSION_TICKET, env.ticket);
}
if (env.userName != null) {
writer.put(CoreTags.TAG_USER_NAME, env.userName);
writer.put(CoreTags.TAG_PASSWORD, env.password != null ? env.password : "");
}
PlatypusRequestWriter.write(env.request, writer);
writer.flush();
peo.write(IoBuffer.wrap(bufOutStream.toByteArray()));
}
示例14: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
@Override
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
WebSocketConnection conn = (WebSocketConnection) session.getAttribute(Constants.CONNECTION);
IoBuffer resultBuffer;
if (message instanceof Packet) {
Packet packet = (Packet) message;
// if the connection is not native / direct, add websocket encoding
resultBuffer = conn.isWebConnection() ? encodeOutgoingData(packet) : packet.getData();
} else if (message instanceof HandshakeResponse) {
HandshakeResponse resp = (HandshakeResponse) message;
resultBuffer = resp.getResponse();
} else if (message instanceof HandshakeRequest) {
HandshakeRequest req = (HandshakeRequest) message;
resultBuffer = req.getRequest();
} else {
throw new Exception("message not a websocket type");
}
out.write(resultBuffer);
}
示例15: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入依赖的package包/类
/**
* Check the id value and set it into context attachment.
*/
@Override
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out)
throws Exception {
/**
* If the message is already in IoBuffer format,
* directly output it to client.
*
* TODO Maybe used to optimize SessionMessage.
*/
if (msg instanceof IoBuffer) {
out.write(msg);
} else if ( msg instanceof XinqiMessage ) {
XinqiMessage message = (XinqiMessage)msg;
if (message != null) {
IoBuffer body = encodeXinqiMessage(message);
out.write(body);
}
}
}