36 } |
36 } |
37 |
37 |
38 |
38 |
39 /* read */ |
39 /* read */ |
40 |
40 |
41 def read(file: JFile): Bytes = |
41 def read_stream(stream: InputStream, max_length: Int): Bytes = |
42 { |
42 { |
|
43 val bytes = new Array[Byte](max_length) |
43 var i = 0 |
44 var i = 0 |
44 var m = 0 |
45 var m = 0 |
45 val n = file.length.toInt |
|
46 val bytes = new Array[Byte](n) |
|
47 |
46 |
48 val stream = new FileInputStream(file) |
|
49 try { |
47 try { |
50 do { |
48 do { |
51 m = stream.read(bytes, i, n - i) |
49 m = stream.read(bytes, i, max_length - i) |
52 if (m != -1) i += m |
50 if (m != -1) i += m |
53 } while (m != -1 && n > i) |
51 } while (m != -1 && max_length > i) |
54 } |
52 } |
55 finally { stream.close } |
53 finally { stream.close } |
56 |
54 |
57 new Bytes(bytes, 0, bytes.length) |
55 new Bytes(bytes, 0, i) |
58 } |
56 } |
|
57 |
|
58 def read(file: JFile): Bytes = |
|
59 read_stream(new FileInputStream(file), file.length.toInt) |
59 } |
60 } |
60 |
61 |
61 final class Bytes private( |
62 final class Bytes private( |
62 protected val bytes: Array[Byte], |
63 protected val bytes: Array[Byte], |
63 protected val offset: Int, |
64 protected val offset: Int, |