本文整理汇总了Java中org.apache.mina.filter.codec.ProtocolEncoderOutput.write方法的典型用法代码示例。如果您正苦于以下问题:Java ProtocolEncoderOutput.write方法的具体用法?Java ProtocolEncoderOutput.write怎么用?Java ProtocolEncoderOutput.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.filter.codec.ProtocolEncoderOutput
的用法示例。
在下文中一共展示了ProtocolEncoderOutput.write方法的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(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);
}
示例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
{
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 );
}
示例4: 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 );
}
示例5: 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);
}
示例6: 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);
}
示例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: 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();
}
示例10: 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);
}
示例11: 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);
}
}
}
示例12: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入方法依赖的package包/类
@Override
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception {
// TODO Auto-generated method stub
// Start packet with 5E
// Include length hash [int]
// Include packet length [int]
// Include data hash [int]
// Command (no clue) [int]
// Append existing data
if (msg instanceof byte[]) {
byte[] src = (byte[]) msg;
byte[] output = new byte[src.length + 5];
output[0] = 0x5E;
output[1] = (byte) (src.length & 0xFF);
output[2] = (byte) (src.length >>> 8 & 0xFF);
output[3] = (byte) (src.length >>> 16 & 0xFF);
output[4] = (byte) (src.length >>> 24 & 0xFF);
System.arraycopy(src, 0, output, 5, src.length);
System.out.println(HexTool.toString(output));
out.write(IoBuffer.wrap(output));
}
}
示例13: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入方法依赖的package包/类
/**
* convert LLRPMessage object to binary format
*/
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
LLRPMessage llrp = (LLRPMessage) message;
log.debug("encoding message " + llrp.getClass());
byte[] byteMsg;
try {
byteMsg = llrp.encodeBinary();
} catch (InvalidLLRPMessageException me) {
log.warn("no message written because error occured: "
+ me.getMessage());
return;
}
// Note: ByteBuffer is renamed in MINA to IOBuffer
IoBuffer buffer = IoBuffer.allocate(byteMsg.length, false);
buffer.put(byteMsg);
buffer.flip();
out.write(buffer);
}
示例14: chunkAndWrite
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入方法依赖的package包/类
public static int chunkAndWrite(ProtocolEncoderOutput out, IoBuffer message, int chunkSize, int desiredSize) {
int sentChunks = 0;
int targetSize = desiredSize > chunkSize ? desiredSize : chunkSize;
int limit = message.limit();
do {
int length = 0;
int pos = message.position();
while (length < targetSize && pos < limit) {
byte basicHeader = message.get(pos);
length += getDataSize(basicHeader) + chunkSize;
pos += length;
}
int remaining = message.remaining();
log.trace("Length: {} remaining: {} pos+len: {} limit: {}", new Object[] { length, remaining, (message.position() + length), limit });
if (length > remaining) {
length = remaining;
}
// send it
out.write(message.getSlice(length));
sentChunks++;
} while (message.hasRemaining());
return sentChunks;
}
示例15: encode
import org.apache.mina.filter.codec.ProtocolEncoderOutput; //导入方法依赖的package包/类
public void encode(IoSession session, UnsubscribeMessage message, ProtocolEncoderOutput out) throws Exception {
if (message.topics().isEmpty()) {
throw new IllegalArgumentException("Found an unsubscribe message with empty topics");
}
if (message.getQos() != QOSType.LEAST_ONE) {
throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos());
}
IoBuffer variableHeaderBuff = IoBuffer.allocate(4).setAutoExpand(true);
Utils.writeWord(variableHeaderBuff, message.getMessageID());
for (String topic : message.topics()) {
variableHeaderBuff.put(Utils.encodeString(topic));
}
variableHeaderBuff.flip();
int variableHeaderSize = variableHeaderBuff.remaining();
byte flags = Utils.encodeFlags(message);
IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize);
buff.put((byte) (AbstractMessage.UNSUBSCRIBE << 4 | flags));
buff.put(Utils.encodeRemainingLength(variableHeaderSize));
buff.put(variableHeaderBuff).flip();
out.write(buff);
}