mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Trustin Lee (JIRA)" <j...@apache.org>
Subject [jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )
Date Wed, 06 Sep 2006 07:28:23 GMT
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12432772 ] 
            
Trustin Lee commented on DIRMINA-242:
-------------------------------------

If I understood correctly, MINA already provides that functionality:

        // Test a trailing garbage
        buf.clear();
        buf.put( (byte) 'A' );
        buf.put( (byte) 'B' );
        buf.put( (byte) 0 );
        buf.put( (byte) 'C' );
        buf.position( 0 );
        Assert.assertEquals( "AB", buf.getString( 4, decoder ) );
        Assert.assertEquals( 4, buf.position() );

If I misunderstood your problem again, please provide us a JUnit TestCase.

> 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

        

Mime
View raw message