ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, 
boolean stopOnZero )
------------------------------------------------------------------------------------------------

                 Key: DIRMINA-242
                 URL: http://issues.apache.org/jira/browse/DIRMINA-242
             Project: Directory MINA
          Issue Type: New Feature
    Affects Versions: 0.8.2
            Reporter: Pavel Zdenek
            Priority: Trivial


The method would read an ASCIIZ string off the buffer as the existing method 
without added parameter.
If stopOnZero is false, it would continue blindly reading the stream up to the 
required fieldSize.

So far MINA has no support for reading fixed (small) size strings. This is 
common for protocols constructed at devices with limited resources (i.e. 
network attached microcontrollers) and above the mandatory ending zero may be 
whatever garbage left from previous memory usage.

My temporary implementation follows, to get the idea.
It also solves the problem with getString behavior when reading ASCIIZ at the 
end of buffer (apparently fixed in dev 0.9) 

    public static String getString(ByteBuffer buf, int fieldSize, 
CharsetDecoder decoder, boolean stopOnZero )
    throws CharacterCodingException
    {
        if( fieldSize == 0) fieldSize = buf.remaining(); // convenience "0 ~ 
till the end"
        int origLimit = buf.limit();
        int posStrEnd = 0;
        buf.mark(); // beginning of string
        boolean foundZero = false;
        while(buf.hasRemaining() && (fieldSize-- > 0) )
        {
            if( (buf.get() == 0) && !foundZero ) 
            {
                foundZero = true;
                posStrEnd = buf.position()-1;
                if( stopOnZero ) break;
            }
        }
        int reachedPos = buf.position(); // later must return here
        buf.reset(); // position to string begin
        if(foundZero) buf.limit( posStrEnd ); 
        String str = buf.getString(decoder);
        if(foundZero) buf.limit(origLimit);
        buf.position(reachedPos);
        return str;
}

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to