1 | /* This file is part of Aard Dictionary for Android <http://aarddict.org>. |
2 | * |
3 | * This program is free software: you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License version 3 |
5 | * as published by the Free Software Foundation. |
6 | * |
7 | * This program is distributed in the hope that it will be useful, |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | * GNU General Public License <http://www.gnu.org/licenses/gpl-3.0.txt> |
11 | * for more details. |
12 | * |
13 | * Copyright (C) 2010 Igor Tkach |
14 | */ |
15 | |
16 | package aarddict; |
17 | |
18 | import java.io.EOFException; |
19 | import java.io.File; |
20 | import java.io.FileNotFoundException; |
21 | import java.io.IOException; |
22 | import java.util.UUID; |
23 | |
24 | final class RandomAccessFile extends java.io.RandomAccessFile { |
25 | |
26 | public RandomAccessFile(File file, String mode) throws FileNotFoundException { |
27 | super(file, mode); |
28 | } |
29 | |
30 | public RandomAccessFile(String fileName, String mode) throws FileNotFoundException { |
31 | super(fileName, mode); |
32 | } |
33 | |
34 | public final long readUnsignedInt() throws IOException { |
35 | int ch1 = this.read(); |
36 | int ch2 = this.read(); |
37 | int ch3 = this.read(); |
38 | int ch4 = this.read(); |
39 | if ((ch1 | ch2 | ch3 | ch4) < 0) |
40 | throw new EOFException(); |
41 | return ((long) (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)) & 0xFFFFFFFFL; |
42 | } |
43 | |
44 | public final long readUnsignedLong() throws IOException { |
45 | int ch1 = this.read(); |
46 | int ch2 = this.read(); |
47 | int ch3 = this.read(); |
48 | int ch4 = this.read(); |
49 | int ch5 = this.read(); |
50 | int ch6 = this.read(); |
51 | int ch7 = this.read(); |
52 | int ch8 = this.read(); |
53 | if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8) < 0) |
54 | throw new EOFException(); |
55 | return ((long) (ch1 << 56) + (ch2 << 48) + (ch3 << 40) + (ch4 << 32) + (ch5 << 24) + (ch6 << 16) + (ch7 << 8) + (ch8 << 0)) & 0xFFFFFFFFFFFFFFFFL; |
56 | } |
57 | |
58 | public final String readUTF8(int length) throws IOException { |
59 | byte[] s = new byte[length]; |
60 | this.read(s); |
61 | return Volume.utf8(s); |
62 | } |
63 | |
64 | public final UUID readUUID() throws IOException { |
65 | byte[] s = new byte[16]; |
66 | this.read(s); |
67 | return Volume.uuid(s); |
68 | } |
69 | |
70 | public final long readSpec(char spec) throws IOException { |
71 | if (spec == 'L' || spec == 'I') { |
72 | return readUnsignedInt(); |
73 | } |
74 | if (spec == 'Q') { |
75 | return readUnsignedLong(); |
76 | } |
77 | if (spec == 'H') { |
78 | return readUnsignedShort(); |
79 | } |
80 | if (spec == 'l' || spec == 'i') { |
81 | return readInt(); |
82 | } |
83 | if (spec == 'q') { |
84 | return readLong(); |
85 | } |
86 | if (spec == 'h') { |
87 | return this.readShort(); |
88 | } |
89 | throw new IOException("Unsupported spec character " + spec); |
90 | } |
91 | |
92 | } |