-
- private static int ioe(Exception e) { throw new StreamException(e); }
- public static class StreamException extends RuntimeException {
- public StreamException(Exception e) { super(e); }
- public StreamException(String s) { super(s); }
- }
- public static class EOF extends StreamException { public EOF() { super("End of stream"); } }
- public static class Closed extends StreamException { public Closed(String s) { super(s); } }
-
-
- private static Hashtable blocker = new Hashtable();
- public static void kill(Thread thread) {
- Stream block = (Stream)blocker.get(thread);
- if (block == null) {
- Log.warn(Stream.class, "thread " + thread + " is not blocked on a stream");
- } else {
- Log.warn(Stream.class, "asynchronously closing " + block);
- block.close();
- }
- }
-
- public char peekc() {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- flush(); return in.getc(true);
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
- }
- public char getc() {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- flush(); char ret = in.getc(false); log(ret); return ret;
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
- }
- public String readln() {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- flush(); String s = in.readln(); log(s); log('\n'); return s;
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
- }
-
- public void print(String s) {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- logWrite(s);
- out.write(s);
- flush();
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
- }
- public void println(String s) {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- logWrite(s);
- out.write(s);
- out.write(newLine);
- flush();
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
- }
- public void flush() {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- if (out != null) try { out.w.flush(); } catch(IOException e) { ioe(e); }
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
- }
- public int read(byte[] b, int off, int len) {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- flush();
- int ret = in.readBytes(b, off, len);
- if (log != null) log("\n[read " + ret + " bytes of binary data ]\n");
- nnl = false;
- return ret;
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }
- }
- public int read(char[] c, int off, int len) {
- Stream old = (Stream)blocker.get(Thread.currentThread());
- try {
- blocker.put(Thread.currentThread(), this);
- flush();
- int ret = in.read(c, off, len);
- if (log != null && ret != -1) log(new String(c, off, ret));
- return ret;
- } finally { if (old == null) blocker.remove(Thread.currentThread()); else blocker.put(Thread.currentThread(), old); }