FileDocCategorySizeDatePackage
TestStdin.javaAPI DocExample1357Mon Jan 27 10:08:22 GMT 2003com.ronsoft.books.nio.channels

TestStdin

public class TestStdin extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)

		Selector selector = Selector.open();
		SystemInPipe stdinPipe = new SystemInPipe();
		SelectableChannel stdin = stdinPipe.getStdinChannel();
		ByteBuffer buffer = ByteBuffer.allocate (32);

		stdin.register (selector, SelectionKey.OP_READ);
		stdinPipe.start();

		System.out.println ("Entering select(), type something:");

		while (true) {
			selector.select (2000);

			Iterator it = selector.selectedKeys().iterator();

			if ( ! it.hasNext()) {
				System.out.println ("I'm waiting");
				continue;
			}

			SelectionKey key = (SelectionKey) it.next();

			it.remove();
			buffer.clear();

			ReadableByteChannel channel =
				(ReadableByteChannel) key.channel();
			int count = channel.read (buffer);

			if (count < 0) {
				System.out.println ("EOF, bye");

				channel.close();
				break;
			}

			buffer.flip();

			System.out.println ("Hey, read " + count + " chars:");

			while (buffer.hasRemaining()) {
				System.out.print ((char) buffer.get());
			}

			System.out.println();
		}