It will really only work for ASCII although you might get away with full 8 bit (ISO-8859-x) if your clients and server and homogeneous.

Of course UTF8 matches ASCII for values up to 128 so if your string happens to be UTF8 it will work for that subset of the character set.

We use it for cases where we are sure the string is ASCII.

RG


Michael Bauroth <[EMAIL PROTECTED]>

13/04/2006 09:45
Please respond to mina-dev

       
        To:        [email protected]
        cc:        
        Subject:        Re: converting ASCII represented longs from ByteBuffer to simple data type?



Thank you for your comments!

Two questions regarding Roberts sample:

1. This sample will work only for UTF8 strings, right?
2. Is it worth to insert it in the Mina project?!

I tried another way for the first part. I used

<code>
char[] buf = in.asCharBuffer().array();
</code>

Unfortunately an UnsupportedOperationException was thrown :(
This happens, when the underlying array is null (not heap allocated).

Regards
Michael


[EMAIL PROTECTED] wrote:

>
> One thing we found with string parsing is that if you know your string
> is purely ASCII it is *much much* faster to grab the string as a byte
> array, cast each element to a char array and construct the String from
> that. The charset decoders have a significant overhead.
>
> For example:
>
>             byte[] stringBytes = new byte[length];
>             buffer.get(stringBytes, 0, length);
>             char[] stringChars = new char[length];
>             for (int i = 0; i < stringChars.length; i++)
>             {
>                 stringChars[i] = (char) stringBytes[i];
>             }
>             return new String(stringChars);
>
> Although it looks a bit cumbersome it is five times faster (from my
> measurements) than constructing the String directly from a byte array.
>
> Robert
>
>
>                  *"Trustin Lee" <[EMAIL PROTECTED]>*
>
> 13/04/2006 08:47
> Please respond to mina-dev
>
>                        
>         To:        [email protected]
>         cc:        
>         Subject:        Re: converting ASCII represented longs from
> ByteBuffer to simple data type?
>
>
>
>
> On 4/13/06, Michael Bauroth <[EMAIL PROTECTED]> wrote:
>  >
>  > Hi,
>  >
>  > has somebody a short code snippet, how I can convert an ASCII span,
>  > which represents a long (e.g. 1234) from ByteBuffer to the simple java
>  > type long most efficiently (minimum of new object generation / buffer
>  > copies ...)
>
>
> For now, you have to use ByteBuffer.getString() and then parse it using
> Integer.parseInt().  It's not optimal but works fine. :)
>
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP key fingerprints:
> * E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
> * B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6
>
>
>
> This communication is for informational purposes only. It is not intended
> as an offer or solicitation for the purchase or sale of any financial
> instrument or as an official confirmation of any transaction. All market
> prices,
> data and other information are not warranted as to completeness or
> accuracy and
> are subject to change without notice. Any comments or statements made
> herein
> do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
> and affiliates.




This communication is for informational purposes only. It is not intended
as an offer or solicitation for the purchase or sale of any financial
instrument or as an official confirmation of any transaction. All market prices,
data and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein
do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

Reply via email to