users@grizzly.java.net

Re: Simple input manipulation

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Tue, 20 May 2008 17:27:51 -0400

Salut,

Wayne Gemmell wrote:
> On Tuesday 20 May 2008 14:03:26 Oleksiy Stashok wrote:
>> can you provide complete source of your Filter? As here I don't see
>> how you get the buffer :)
> K, I've put it below. Its really just Jeanfrancois's echo server in 20 lines
> that I'm hacking with.
>
> I access this service with the command "echo hello |nc dude 8013"
>>> I'm doing this bit by bit and I have a wierd problem with reading
>>> the buffer
>>> into a string.
>>> Heres a code snippet.
>>> <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?


>>> cbuf.flip();
>>> String tmp = cbuf.toString();
>>> System.out.println(tmp);
>> And blank squares are printed just here?
> This prints the other 3 squares in the same line.

Is the buffer already flipped here? I suspect your buffer position/limit
are incorrect and you are getting some empty junk.

Thanks

-- Jeanfrancois


>
>>> OutputWriter.flushChannel(channel, buffer);
> This performs as expected. hello is echoes on the console.
>
>>> channel.close();
>>> }
>>> </code>
>>>
>> Not sure. Think CharBuffer.toString() should work properly.
>
> <code>
> private static void startListener() throws IOException {
> Controller controller = new Controller();
> TCPSelectorHandler tcpSelectorHandler = new TCPSelectorHandler();
> tcpSelectorHandler.setPort(PORT);
> controller.addSelectorHandler(tcpSelectorHandler);
> System.out.println("Adding chains");
>
>
> ProtocolChainInstanceHandler pciHandler =
> new ProtocolChainInstanceHandler() {
>
> final private ProtocolChain protocolChain = new
> DefaultProtocolChain();
>
> public ProtocolChain poll() {
> return protocolChain;
> }
>
> public boolean offer(ProtocolChain instance) {
> return true;
> }
> };
> controller.setProtocolChainInstanceHandler(pciHandler);
> //ProtocolChain protocolChain = new DefaultProtocolChain();
> ProtocolChain protocolChain = pciHandler.poll();
> protocolChain.addFilter(new ReadFilter());
> protocolChain.addFilter(new InputFilter());
>
> //protocolChain.addFilter(new EchoFilter());
> System.out.println("Done");
> controller.start();
> }
>
> public class InputFilter implements ProtocolFilter {
>
> //public ProtocolParser newProtocolParser() {
> // return new MyProtocolParser());
> //}
>
> public boolean execute(Context ctx) throws IOException {
> final WorkerThread workerThread =
> ((WorkerThread)Thread.currentThread());
> ByteBuffer buffer = workerThread.getByteBuffer();
> buffer.flip();
> if (buffer.hasRemaining()) {
> // Depending on protocol perform echo
> SelectableChannel channel = ctx.getSelectionKey().channel();
> try {
>
> CharBuffer cbuf = buffer.asCharBuffer();
> while(cbuf.hasRemaining())
> System.out.print(cbuf.get());
> cbuf.flip();
> String tmp = cbuf.toString();
> System.out.println(tmp);
> OutputWriter.flushChannel(channel, buffer);
>
> } catch (IOException ex) {
> // Store incoming data in byte[]
> byte[] data = new byte[buffer.remaining()];
> int position = buffer.position();
> buffer.get(data);
> buffer.position(position);
> throw ex;
> }
> channel.close();
> }
>
> buffer.clear();
>
> return false;
> }
>
> public boolean postExecute(Context ctx) throws IOException {
> return false;
> }
>
> }
> </code>