chiark / gitweb /
wip
[tripe-android] / sock.scala
index 773b0af07b63a111e41080f93f2ba4b88b64b62d..1b52bf1d07060bd6e2fa971dbe1c3b2524ec616b 100644 (file)
@@ -1,11 +1,15 @@
 package uk.org.distorted.tripe;
 
-import java.io.{InputStream, OutputStream};
+import java.io.{Closeable, File, InputStream, OutputStream};
+import jni.Constants._;
 
-class Connection {
-  val conn = JNI.connect();
-  def close() { JNI.close(conn, JNI.CF_CLOSEMASK); }
-  override protected def finalize() { close(); }
+class Connection(path: File) extends Closeable {
+  def this(path: String) { this(new File(path)); }
+  val conn = jni.connect(path.getPath);
+  override def close() { jni.close(conn, CF_CLOSEMASK); }
+  lazy val input = new ConnectionInputStream(this);
+  lazy val output = new ConnectionOutputStream(this);
+  override protected def finalize() { super.finalize(); close(); }
 }
 
 class ConnectionInputStream(val conn: Connection) extends InputStream {
@@ -17,8 +21,8 @@ class ConnectionInputStream(val conn: Connection) extends InputStream {
   override def read(buf: Array[Byte]): Int =
     read(buf, 0, buf.length);
   override def read(buf: Array[Byte], start: Int, len: Int) =
-    JNI.recv(conn.conn, buf, start, len);
-  override def close() { JNI.close(conn.conn, JNI.CF_CLOSERD); }
+    jni.recv(conn.conn, buf, start, len);
+  override def close() { jni.close(conn.conn, CF_CLOSERD); }
 }
 
 class ConnectionOutputStream(val conn: Connection) extends OutputStream {
@@ -28,6 +32,6 @@ class ConnectionOutputStream(val conn: Connection) extends OutputStream {
   }
   override def write(buf: Array[Byte]) { write(buf, 0, buf.length); }
   override def write(buf: Array[Byte], start: Int, len: Int) =
-    JNI.send(conn.conn, buf, start, len);
-  override def close() { JNI.close(conn.conn, JNI.CF_CLOSEWR); }
+    jni.send(conn.conn, buf, start, len);
+  override def close() { jni.close(conn.conn, CF_CLOSEWR); }
 }