|
From: <ls...@us...> - 2007-01-02 19:34:58
|
Revision: 2975
http://jnode.svn.sourceforge.net/jnode/?rev=2975&view=rev
Author: lsantha
Date: 2007-01-02 11:34:53 -0800 (Tue, 02 Jan 2007)
Log Message:
-----------
Implemented read(byte[], int,int). Bsh begins to work with this. More work is needed in the console for line edittig.
Modified Paths:
--------------
trunk/core/src/driver/org/jnode/driver/input/KeyboardInputStream.java
Modified: trunk/core/src/driver/org/jnode/driver/input/KeyboardInputStream.java
===================================================================
--- trunk/core/src/driver/org/jnode/driver/input/KeyboardInputStream.java 2007-01-02 16:16:45 UTC (rev 2974)
+++ trunk/core/src/driver/org/jnode/driver/input/KeyboardInputStream.java 2007-01-02 19:34:53 UTC (rev 2975)
@@ -39,7 +39,7 @@
/**
* Create a new instance
- * @param api
+ * @param api the keyboard API
*/
public KeyboardInputStream(KeyboardAPI api) {
this.api = api;
@@ -53,30 +53,86 @@
return 0;
}
- /**
+ /*
+ Doesn't block after the first blocking read.
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (off < 0 || len < 0 || b.length - off < len)
+ throw new IndexOutOfBoundsException();
+
+ long time = System.currentTimeMillis();
+
+ int i = 0;
+ int ch = read(time);
+
+ for(;;) {
+ b[off + i ++] = (byte) ch;
+
+ if(i < len && queue.size() > 0)
+ ch = read(time);
+ else
+ break;
+ }
+
+ return i;
+ }
+ */
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (off < 0 || len < 0 || b.length - off < len)
+ throw new IndexOutOfBoundsException();
+
+ long time = System.currentTimeMillis();
+
+ int i = 0;
+ int ch = read(time);
+
+ for(;;) {
+ b[off + i ++] = (byte) ch;
+
+ if(i < len && ch != '\n')
+ ch = read(time);
+ else
+ break;
+ }
+
+ return i;
+ }
+
+ /**
* @see java.io.InputStream#read()
*/
public int read() throws IOException {
- while (true) {
- KeyboardEvent event = (KeyboardEvent) queue.get();
- if (!event.isConsumed()) {
- event.consume();
- char ch = event.getKeyChar();
- if (ch != 0) {
- if (echo) {
- System.out.print(ch);
- }
- return ch;
- }
- }
- }
+ long time = System.currentTimeMillis();
+ return read(time);
}
- /**
+ private int read(long time){
+ while (true) {
+ KeyboardEvent event = queue.get();
+ int c = event2char(event, time);
+ if(c > 0)
+ return c;
+ }
+ }
+
+ private int event2char(KeyboardEvent event, long time) {
+ if (!event.isConsumed() && event.getTime() - time > -10000) {
+ event.consume();
+ char ch = event.getKeyChar();
+ if (ch != 0) {
+ if (echo) {
+ System.out.print(ch);
+ }
+ return ch;
+ }
+ }
+ return -1;
+ }
+
+ /**
* @see org.jnode.driver.input.KeyboardListener#keyPressed(org.jnode.driver.input.KeyboardEvent)
*/
public void keyPressed(KeyboardEvent event) {
- //log.debug("got event(" + event + ")");
queue.add(event);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|