本文整理匯總了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;
}