@@ -5,6 +5,7 @@ package classfile
55
66import java .lang .Float .intBitsToFloat
77import java .lang .Double .longBitsToDouble
8+ import java .io .{ByteArrayInputStream , DataInputStream }
89
910import io .AbstractFile
1011
@@ -14,16 +15,22 @@ import io.AbstractFile
1415 * @author Philippe Altherr
1516 * @version 1.0, 23/03/2004
1617 */
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)
2220
2321 /** the current input pointer
2422 */
2523 var bp : Int = 0
2624
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+
2734 /** return byte at offset 'pos'
2835 */
2936 @ throws(classOf [IndexOutOfBoundsException ])
@@ -60,13 +67,13 @@ class AbstractFileReader(val file: AbstractFile) {
6067 /** extract a character at position bp from buf
6168 */
6269 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
6471
6572 /** extract an integer at position bp from buf
6673 */
6774 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 )
7077
7178 /** extract a long integer at position bp from buf
7279 */
@@ -81,6 +88,9 @@ class AbstractFileReader(val file: AbstractFile) {
8188 */
8289 def getDouble (mybp : Int ): Double = longBitsToDouble(getLong(mybp))
8390
91+ def getUTF (mybp : Int , len : Int ): String =
92+ new DataInputStream (new ByteArrayInputStream (buf, mybp, len)).readUTF
93+
8494 /** skip next 'n' bytes
8595 */
8696 def skip (n : Int ): Unit = { bp += n }
0 commit comments