本文整理匯總了Java中org.apache.mina.core.buffer.IoBuffer.reset方法的典型用法代碼示例。如果您正苦於以下問題:Java IoBuffer.reset方法的具體用法?Java IoBuffer.reset怎麽用?Java IoBuffer.reset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.reset方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: writeBuffer
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private int writeBuffer(S session, WriteRequest req, boolean hasFragmentation, int maxLength, long currentTime)
throws Exception {
IoBuffer buf = (IoBuffer) req.getMessage();
int localWrittenBytes = 0;
if (buf.hasRemaining()) {
int length;
if (hasFragmentation) {
length = Math.min(buf.remaining(), maxLength);
} else {
length = buf.remaining();
}
try {
localWrittenBytes = write(session, buf, length);
} catch (IOException ioe) {
// We have had an issue while trying to send data to the
// peer : let's close the session.
session.close(true);
}
}
session.increaseWrittenBytes(localWrittenBytes, currentTime);
if (!buf.hasRemaining() || (!hasFragmentation && (localWrittenBytes != 0))) {
// Buffer has been sent, clear the current request.
int pos = buf.position();
buf.reset();
fireMessageSent(session, req);
// And set it back to its position
buf.position(pos);
}
return localWrittenBytes;
}
示例4: flush
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private boolean flush( S session, long currentTime ) throws Exception
{
final WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue();
final int maxWrittenBytes = session.getConfig().getMaxReadBufferSize()
+ ( session.getConfig().getMaxReadBufferSize() >>> 1 );
int writtenBytes = 0;
try
{
for ( ;; )
{
WriteRequest req = session.getCurrentWriteRequest();
if ( req == null )
{
req = writeRequestQueue.poll( session );
if ( req == null )
{
setInterestedInWrite( session, false );
break;
}
session.setCurrentWriteRequest( req );
}
IoBuffer buf = ( IoBuffer ) req.getMessage();
if ( buf.remaining() == 0 )
{
// Clear and fire event
session.setCurrentWriteRequest( null );
buf.reset();
session.getFilterChain().fireMessageSent( req );
continue;
}
SocketAddress destination = req.getDestination();
if ( destination == null )
{
destination = session.getRemoteAddress();
}
int localWrittenBytes = send( session, buf, destination );
if ( ( localWrittenBytes == 0 ) || ( writtenBytes >= maxWrittenBytes ) )
{
// Kernel buffer is full or wrote too much
setInterestedInWrite( session, true );
return false;
}
else
{
setInterestedInWrite( session, false );
// Clear and fire event
session.setCurrentWriteRequest( null );
writtenBytes += localWrittenBytes;
buf.reset();
session.getFilterChain().fireMessageSent( req );
}
}
}
finally
{
session.increaseWrittenBytes( writtenBytes, currentTime );
}
return true;
}
示例5: 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;
}