当前位置: 首页>>代码示例>>Java>>正文


Java ByteBuffer.hasRemaining方法代码示例

本文整理汇总了Java中org.apache.mina.common.ByteBuffer.hasRemaining方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuffer.hasRemaining方法的具体用法?Java ByteBuffer.hasRemaining怎么用?Java ByteBuffer.hasRemaining使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.mina.common.ByteBuffer的用法示例。


在下文中一共展示了ByteBuffer.hasRemaining方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: doDecode

import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
@Override
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out)
           throws Exception {
       // Get the XML light parser from the IoSession
       XMLLightweightParser parser = (XMLLightweightParser) session.getAttribute(ConnectionHandler.XML_PARSER);
       // Parse as many stanzas as possible from the received data
       parser.read(in);

       if (parser.areThereMsgs()) {
           for (String stanza : parser.getMsgs()) {
               out.write(stanza);
           }
       }
       return !in.hasRemaining();
   }
 
开发者ID:coodeer,项目名称:g3server,代码行数:16,代码来源:XMPPDecoder.java

示例2: parseMessage

import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
/**
 * This method cannot move the buffer position until a message is found or an error
 * has occurred. Otherwise, MINA will compact the buffer and we lose data.
 */
private boolean parseMessage(ByteBuffer in, ProtocolDecoderOutput out)
        throws ProtocolCodecException {
    try {
        boolean messageFound = false;
        while (in.hasRemaining() && !messageFound) {
            if (state == SEEKING_HEADER) {

                int headerOffset = indexOf(in, position, HEADER_PATTERN);
                if (headerOffset == -1) {
                    break;
                }

                in.position(headerOffset);

                if (log.isDebugEnabled()) {
                    log.debug("detected header: " + getBufferDebugInfo(in));
                }

                position = headerOffset + HEADER_PATTERN.length;
                state = SEEKING_END;
            }

            if (state == SEEKING_END) {
                boolean finded = startsWith(in, position, CHECKSUM_PATTERN);
                while(!finded && in.limit() > position) {
                    position += 1;
                    finded = startsWith(in, position, CHECKSUM_PATTERN);

                }

                if(!finded) {                        
                    break;
                }

                position += CHECKSUM_PATTERN.length;                    
                state = SEEKING_HEADER;
                
                String messageString = getMessageString(in);
                out.write(messageString.replace("\r", ""));
                messageFound = true;
                resetState();
            }
        }
        return messageFound;
    } catch (Throwable t) {
        state = SEEKING_HEADER;
        position = 0;
        if (t instanceof ProtocolCodecException) {
            throw (ProtocolCodecException) t;
        } else {
            throw new ProtocolCodecException(t);
        }
    }
}
 
开发者ID:fix-protocol-tools,项目名称:STAFF,代码行数:59,代码来源:FixMessageDecoder.java

示例3: decode

import org.apache.mina.common.ByteBuffer; //导入方法依赖的package包/类
/**
 * Cumulates content of <tt>in</tt> into internal buffer and forwards
 * decoding request to {@link #doDecode(IoSession, ByteBuffer, ProtocolDecoderOutput)}.
 * <tt>doDecode()</tt> is invoked repeatedly until it returns <tt>false</tt>
 * and the cumulative buffer is compacted after decoding ends.
 *
 * @throws IllegalStateException if your <tt>doDecode()</tt> returned
 *                               <tt>true</tt> not consuming the cumulative buffer.
 */
public void decode( IoSession session, ByteBuffer in,
                    ProtocolDecoderOutput out ) throws Exception
{
    ByteBuffer buf = ( ByteBuffer ) session.getAttribute( BUFFER );
    // if we have a session buffer, append data to that otherwise
    // use the buffer read from the network directly
    if( buf != null )
    {
        buf.put( in );
        buf.flip();
    }
    else
    {
        buf = in;
    }

    for( ;; )
    {
        int oldPos = buf.position();
        boolean decoded = doDecode( session, buf, out );
        if( decoded )
        {
            if( buf.position() == oldPos )
            {
                throw new IllegalStateException(
                        "doDecode() can't return true when buffer is not consumed." );
            }

            if( !buf.hasRemaining() )
            {
                break;
            }
        }
        else
        {
            break;
        }
    }

    // if there is any data left that cannot be decoded, we store
    // it in a buffer in the session and next time this decoder is
    // invoked the session buffer gets appended to
    if ( buf.hasRemaining() )
    {
        storeRemainingInSession( buf, session );
    }
    else
    {
        removeSessionBuffer( session );
    }
}
 
开发者ID:wso2,项目名称:andes,代码行数:61,代码来源:AMQDecoder.java


注:本文中的org.apache.mina.common.ByteBuffer.hasRemaining方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。