| 1 | package uk.org.distorted.tripe; |
| 2 | |
| 3 | import java.io.{InputStream, OutputStream}; |
| 4 | |
| 5 | class Connection { |
| 6 | val conn = JNI.connect(); |
| 7 | def close() { JNI.close(conn, JNI.CF_CLOSEMASK); } |
| 8 | override protected def finalize() { close(); } |
| 9 | } |
| 10 | |
| 11 | class ConnectionInputStream(val conn: Connection) extends InputStream { |
| 12 | override def read(): Int = { |
| 13 | val buf = new Array[Byte](1); |
| 14 | val n = read(buf, 0, 1); |
| 15 | if (n < 0) -1 else buf(0)&0xff; |
| 16 | } |
| 17 | override def read(buf: Array[Byte]): Int = |
| 18 | read(buf, 0, buf.length); |
| 19 | override def read(buf: Array[Byte], start: Int, len: Int) = |
| 20 | JNI.recv(conn.conn, buf, start, len); |
| 21 | override def close() { JNI.close(conn.conn, JNI.CF_CLOSERD); } |
| 22 | } |
| 23 | |
| 24 | class ConnectionOutputStream(val conn: Connection) extends OutputStream { |
| 25 | override def write(b: Int) = { |
| 26 | val buf = Array[Byte](b.toByte); |
| 27 | write(buf, 0, 1); |
| 28 | } |
| 29 | override def write(buf: Array[Byte]) { write(buf, 0, buf.length); } |
| 30 | override def write(buf: Array[Byte], start: Int, len: Int) = |
| 31 | JNI.send(conn.conn, buf, start, len); |
| 32 | override def close() { JNI.close(conn.conn, JNI.CF_CLOSEWR); } |
| 33 | } |