@@ -5,6 +5,7 @@ package classfile
5
5
6
6
import java .lang .Float .intBitsToFloat
7
7
import java .lang .Double .longBitsToDouble
8
+ import java .io .{ByteArrayInputStream , DataInputStream }
8
9
9
10
import io .AbstractFile
10
11
@@ -14,16 +15,22 @@ import io.AbstractFile
14
15
* @author Philippe Altherr
15
16
* @version 1.0, 23/03/2004
16
17
*/
17
- class AbstractFileReader (val file : AbstractFile ) {
18
-
19
- /** the buffer containing the file
20
- */
21
- val buf : Array [Byte ] = file.toByteArray
18
+ final class AbstractFileReader (val buf : Array [Byte ]) extends DataReader {
19
+ def this (file : AbstractFile ) = this (file.toByteArray)
22
20
23
21
/** the current input pointer
24
22
*/
25
23
var bp : Int = 0
26
24
25
+ /** extract a byte at position bp from buf
26
+ */
27
+ def getByte (mybp : Int ): Byte =
28
+ buf(mybp)
29
+
30
+ def getBytes (mybp : Int , bytes : Array [Byte ]): Unit = {
31
+ System .arraycopy(buf, mybp, bytes, 0 , bytes.length)
32
+ }
33
+
27
34
/** return byte at offset 'pos'
28
35
*/
29
36
@ throws(classOf [IndexOutOfBoundsException ])
@@ -60,13 +67,13 @@ class AbstractFileReader(val file: AbstractFile) {
60
67
/** extract a character at position bp from buf
61
68
*/
62
69
def getChar (mybp : Int ): Char =
63
- (((buf (mybp) & 0xff ) << 8 ) + (buf (mybp + 1 ) & 0xff )).toChar
70
+ (((getByte (mybp) & 0xff ) << 8 ) + (getByte (mybp+ 1 ) & 0xff )).toChar
64
71
65
72
/** extract an integer at position bp from buf
66
73
*/
67
74
def getInt (mybp : Int ): Int =
68
- ((buf (mybp ) & 0xff ) << 24 ) + ((buf (mybp + 1 ) & 0xff ) << 16 ) +
69
- ((buf (mybp + 2 ) & 0xff ) << 8 ) + (buf (mybp + 3 ) & 0xff )
75
+ ((getByte (mybp) & 0xff ) << 24 ) + ((getByte (mybp + 1 ) & 0xff ) << 16 ) +
76
+ ((getByte (mybp + 2 ) & 0xff ) << 8 ) + (getByte (mybp + 3 ) & 0xff )
70
77
71
78
/** extract a long integer at position bp from buf
72
79
*/
@@ -81,6 +88,9 @@ class AbstractFileReader(val file: AbstractFile) {
81
88
*/
82
89
def getDouble (mybp : Int ): Double = longBitsToDouble(getLong(mybp))
83
90
91
+ def getUTF (mybp : Int , len : Int ): String =
92
+ new DataInputStream (new ByteArrayInputStream (buf, mybp, len)).readUTF
93
+
84
94
/** skip next 'n' bytes
85
95
*/
86
96
def skip (n : Int ): Unit = { bp += n }
0 commit comments