users@grizzly.java.net

Re: Simple input manipulation

From: Wayne Gemmell <wayne_at_flashmedia.co.za>
Date: Wed, 21 May 2008 11:49:40 +0200

On Tuesday 20 May 2008 23:27:51 Jeanfrancois Arcand wrote:
> >>> <code>
> >>> SelectableChannel channel = ctx.getSelectionKey().channel();
> >>>            try {
> >>>
> >>>                CharBuffer cbuf = buffer.asCharBuffer();
> >>>                while(cbuf.hasRemaining())
> >>>                    System.out.print(cbuf.get());
> >>
> >> Does this print what you need?
> >
> > This prints 3 squares, 1 on each line in debug (Netbeans) and 3 on the
> > same line in normal run time.
>
> This is really strange as cbuf.get() should really return a char. Can
> you try:
>
> buffer.flip();
> byte[] bytes = new byte[buffer.limit()];
> buffer.get(bytes);
> Systen.out.println(new String(bytes));
>
> What are you seeing on the console?
This outputs the input string as expected.

Interestingly enough flipping the buffer before running asCharBuffer(); sets
the buffer position to 0 and the limit to 6, asCharBuffer then sets the
charbuffer limit to 3 and stated in the java docs (Any idea why it would do
this?) but cbuf.flip sets the limit to 0.

-- 
Regards
Wayne