You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Pavel Zdenek (JIRA)" <ji...@apache.org> on 2006/08/10 16:27:15 UTC

[jira] Created: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Trustin Lee (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12432731 ] 
            
Trustin Lee commented on DIRMINA-242:
-------------------------------------

One more question:

Is there any reason that you don't use ByteBuffer.getString(CharsetDecoder)?  It stops on zero.  Is it because it cannot specify the max. field size?

> 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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Pavel Zdenek (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12432768 ] 
            
Pavel Zdenek commented on DIRMINA-242:
--------------------------------------

Let's stop talking about "string". I would name it "fixed buffer space allocated for the string".
The existing getString(...) pushes the buffer position only to the end of  "string".
I would like it to be pushed to the end of whole "fixed space", and on the way, reading the valid string in it. But not stopping once the string is finished, if it is shorter than the fixed space!

Maybe the extra parameter should not be stopOnZero, behaving like existing getString when true.
For clarity, it probably should be named something like continueAfterZero, default false.


> 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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Trustin Lee (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12432555 ] 
            
Trustin Lee commented on DIRMINA-242:
-------------------------------------

Ah I see.  The problem you are describing is fixed in 0.9.  OK, now let's talk about stopOnZero.

If your intention is to read data beyond NUL, is the data a string?  Isn't it a byte array?  Is there any reason that you want to get it as a string?

> 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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Pavel Zdenek (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12430751 ] 
            
Pavel Zdenek commented on DIRMINA-242:
--------------------------------------

You are right, there is no difference. Method getString(int, CharsetDecoder) is not vulnerable to this problem. The test case would be your existing testGetString(), only allocating a buffer of exact size for the test string, i.e. 

public void testGetString() throws Exception
{
        ByteBuffer buf = ByteBuffer.allocate( 4 );
... instantiate ISO decoder...
... put ABC\0 ....
        buf.position( 0 );
        Assert.assertEquals( 4, buf.getString( decoder ) );
}

Assert fails, because string includes trailing zero. The condition is when the trailing zero is the last character in the buffer (i.e. position after reading equals capacity ).

SECONDLY, sorry for mixing two requests in one issue. The bug above is NOT my primary request, as i have a bypass implementation for 0.8 and it's fixed in 0.9. I would like to have a method
ByteBuffer.getString(int fieldSize, CharsetDecoder decoder, boolean stopOnZero )
which would (on stopOnZero==false) forward the buffer position up to the field size, even if the ASCIIZ string has ended earlier. The purpose has been explained in the first comment. So far, reading any values from a buffer of simple protocol with fixed-length values is very clean and understandable, except strings.


> 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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Pavel Zdenek (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12432600 ] 
            
Pavel Zdenek commented on DIRMINA-242:
--------------------------------------

The request is not to get EVERYTHING of length fieldSize as "the one" return string, including nulls and all garbage.

"This" ByteBuffer is expected to REALLY hold a VALID ASCIIZ string at the current position. Method would use CharsetDecoder on it and save it as return value, just like the current method does. I only want the method to return AFTER this ByteBuffer is at the right position for get()-ting the next entity, just like with any other fixed-length value (primitives, arrays, etc.).  Even when the spoken string has not occupied entire _fixed_ space allocated for it in the protocol. So above the ending null, just forward the position, not storing anything. In the application which is the reason for all this fuss, i have a stream of 5-bytes chunks, each holding a string of 1-5 chars,mostly 4. The sender is an average microcontroller and the clean way (sending a string length and then just the appropriate number of bytes, resulting in variable length of chunks) would be worse in terms of code size and volatile memory requirements.

> 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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Pavel Zdenek (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12427236 ] 
            
Pavel Zdenek commented on DIRMINA-242:
--------------------------------------

The title should be
ByteBuffer.getString(int fieldSize, CharsetDecoder decoder, boolean stopOnZero )
that is, a method of byte buffer, not the signature of my helper method :) Sorry.

> 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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Pavel Zdenek (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12433352 ] 
            
Pavel Zdenek commented on DIRMINA-242:
--------------------------------------

Ok it works. Sorry for wasting your time.
After some time spent with feeling outrageously stupid, i have realized that i actually never tested it. My code is originally ported from Netty. I had found it not working with 0.8 and downgraded to my getString implementation (also a legacy of Netty). After realizing that the main problem was the reading till the end of buffer (fixed in 0.9), i never thought of re-testing it with Mina getString. I think i will leave it as-is until 1.0 :) Thank you for your patience.

> 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

        

[jira] Resolved: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Trustin Lee (JIRA)" <ji...@apache.org>.
     [ http://issues.apache.org/jira/browse/DIRMINA-242?page=all ]

Trustin Lee resolved DIRMINA-242.
---------------------------------

    Resolution: Invalid
      Assignee: Trustin Lee

> 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
>         Assigned To: Trustin Lee
>            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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Trustin Lee (JIRA)" <ji...@apache.org>.
    [ http://issues.apache.org/jira/browse/DIRMINA-242?page=comments#action_12427439 ] 
            
Trustin Lee commented on DIRMINA-242:
-------------------------------------

I don't see any difference in getString(int, CharsetDecoder) between 0.8.2 and 0.9.4.  You told that the problem you are trying to solve already has been fixed in 0.9.  Can you give me an example code (like JUnit test case) to demonstrate what is different or what the problem is?

> 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

        

[jira] Commented: (DIRMINA-242) ByteBuffer.getString(ByteBuffer buf, int fieldSize, CharsetDecoder decoder, boolean stopOnZero )

Posted by "Trustin Lee (JIRA)" <ji...@apache.org>.
    [ 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