chiark / gitweb /
Initial commit.
[tripe-android] / main.scala
1 package uk.org.distorted;
2
3 import java.io.{InputStreamReader, OutputStreamWriter};
4 import scala.collection.mutable.StringBuilder;
5 import scala.util.control.Breaks;
6
7 package object tripe {
8   def main(args: Array[String])
9   {
10     println("Hello from Scala");
11     JNI.test();
12     val toy = JNI.make();
13     for (i <- 0 until args.length) println(f"$i%2d: ${args(i)}%s");
14     //toy match { case toy: Array[Byte] => toy(1) = -1; case _ => () }
15     JNI.check(toy);
16
17     val conn = new Connection;
18     try {
19       val rd = new InputStreamReader(new ConnectionInputStream(conn));
20       val wr = new OutputStreamWriter(new ConnectionOutputStream(conn));
21
22       wr.write("Hello, world!\n"); wr.flush();
23
24       val buf = new Array[Char](4096);
25       val line = new StringBuilder;
26
27       val R = new Breaks;
28       val L = new Breaks;
29       var any = false;
30       R.breakable {
31         while (true) {
32           val n = rd.read(buf);
33           if (n <= 0) R.break;
34           var pos = 0;
35           L.breakable {
36             while (true) {
37               val nl = buf.indexOf('\n', pos);
38               if (nl == -1 || nl >= n) {
39                 if (pos < n)
40                   { line.appendAll(buf, pos, n - pos); any = true; }
41                 L.break;
42               }
43               val s = if (!any)
44                 new String(buf, pos, nl - pos);
45               else {
46                 line.appendAll(buf, pos, nl - pos);
47                 val s = line.mkString;
48                 line.clear(); any = false;
49                 s
50               };
51               println(s"found line `$s'");
52               pos = nl + 1;
53             }
54           }
55         }
56       }
57
58       rd.close();
59       wr.close();
60     } finally {
61       conn.close();
62     }
63   }
64 }