本文整理汇总了Java中org.apache.mina.core.buffer.IoBuffer.mark方法的典型用法代码示例。如果您正苦于以下问题:Java IoBuffer.mark方法的具体用法?Java IoBuffer.mark怎么用?Java IoBuffer.mark使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.mark方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decodable
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public MessageDecoderResult decodable(IoSession session, IoBuffer buf) {
int remain = buf.remaining();
if(remain <= 1){
return MessageDecoderResult.NOT_OK;
}
buf.mark();
byte[] data = new byte[remain - 2];
buf.get(data);
byte cs = buf.get();
int t = 0;
for( int i =0; i< data.length; i ++){
t += data[i];
}
int cs_cal = (t%256) ;
//如果校验码校验不通过,则为无效消息
if ( (cs & 0xFF) != (cs_cal & 0xFF)){
buf.reset();
return MessageDecoderResult.NOT_OK;
}
buf.reset();
return MessageDecoderResult.OK;
}
示例2: getMessageCopy
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private Object getMessageCopy(Object message) {
Object messageCopy = message;
if (message instanceof IoBuffer) {
IoBuffer rb = (IoBuffer) message;
rb.mark();
IoBuffer wb = IoBuffer.allocate(rb.remaining());
wb.put(rb);
wb.flip();
rb.reset();
messageCopy = wb;
}
return messageCopy;
}
示例3: filterWrite
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
AbstractIoSession s = (AbstractIoSession) session;
// Maintain counters.
if (writeRequest.getMessage() instanceof IoBuffer) {
IoBuffer buffer = (IoBuffer) writeRequest.getMessage();
// I/O processor implementation will call buffer.reset()
// it after the write operation is finished, because
// the buffer will be specified with messageSent event.
buffer.mark();
int remaining = buffer.remaining();
if (remaining == 0) {
// Zero-sized buffer means the internal message
// delimiter.
s.increaseScheduledWriteMessages();
} else {
s.increaseScheduledWriteBytes(remaining);
}
} else {
s.increaseScheduledWriteMessages();
}
WriteRequestQueue writeRequestQueue = s.getWriteRequestQueue();
if (!s.isWriteSuspended()) {
if (writeRequestQueue.size() == 0) {
// We can write directly the message
s.getProcessor().write(s, writeRequest);
} else {
s.getWriteRequestQueue().offer(s, writeRequest);
s.getProcessor().flush(s);
}
} else {
s.getWriteRequestQueue().offer(s, writeRequest);
}
}
示例4: decode
import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public MessageDecoderResult decode(IoSession session, IoBuffer buf, ProtocolDecoderOutput out) throws Exception {
CjyTcpMessage message = new CjyTcpMessage();
logger.info("收到采集仪消息" + buf.getHexDump());
buf.mark();
//帧起始符 0x68
buf.get();
// 获取设备编码
int address_i = buf.getInt();
message.setAddress(CommUtils.highAndLowAddressSwap(address_i));
//帧起始符 0x16
if ( buf.get() != 0x68){
return null;
}
//控制码
byte control = buf.get();
message.setD7(control >> 7);
message.setD6(control >> 6);
//控制码 , 协议簇
message.setCmd(control&0x0f);
//数据体长度
int length = buf.get();
message.setLength(length);
//数据体
byte[] data = MessageUtil.getBytes(buf, length);
message.setData(data);
//CRC
byte crc = (buf.get());
//结束标志
int finished = buf.get();
if ( finished != 0x16 ){
return MessageDecoderResult.NOT_OK;
}
buf.reset();
//获取校验位前字节
byte[] cpData = MessageUtil.getBytes(buf, 8 + length);
int t = 0;
for( int i =0; i< cpData.length; i ++){
t += cpData[i];
}
int crc_cal = (t%256) ;
logger.info("获取的CRC值" + (crc & 0xFF) + ",计算的crc值:" + crc_cal + ",address:" + message.getAddress());
//如果校验码校验不通过,则为无效消息
if ( (crc & 0xFF) != (crc_cal & 0xFF)){
return MessageDecoderResult.NOT_OK;
}
buf.get();
buf.get();
out.write(message);
return MessageDecoderResult.OK;
}