1 package info.mikethomas.fahview.v6project.utilities;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27
28
29
30
31
32
33
34 public class QueueReader {
35 private RandomAccessFile file;
36
37
38
39
40
41
42 public QueueReader(String fileName) {
43 try {
44 file = new RandomAccessFile(fileName, "r");
45 } catch (FileNotFoundException ex) {
46 System.err.println("File " + fileName + " Not Found");
47 }
48 }
49
50
51
52
53
54
55
56
57 public byte[] read(int position, int length) {
58 byte[] buf = new byte[length];
59 try {
60 file.seek(position);
61 file.read(buf);
62 } catch (IOException ex) {
63 System.err.println("Failed to read " + length + "bytes from position " + position);
64 }
65 return buf;
66 }
67
68
69
70
71
72
73
74
75
76 public long readBEUInt(int position, int length) {
77 byte[] buf = this.read(position, length);
78
79 int index = 0;
80 int firstByte = (0xFF & ((int)buf[index]));
81 int secondByte = (0xFF & ((int)buf[index+1]));
82 int thirdByte = (0xFF & ((int)buf[index+2]));
83 int fourthByte = (0xFF & ((int)buf[index+3]));
84 return ((long) (firstByte << 24
85 | secondByte << 16
86 | thirdByte << 8
87 | fourthByte))
88 & 0xFFFFFFFFL;
89 }
90
91
92
93
94
95
96
97
98 public long readLEUInt(int position, int length) {
99 return ByteSwapper.swap((int) readBEUInt(position, length));
100 }
101
102
103
104
105
106
107
108
109
110 public int readBEUShort(int position, int length) {
111 byte[] buf = this.read(position, length);
112
113 int index = 0;
114 int firstByte = (0xFF & ((int)buf[index]));
115 int secondByte = (0xFF & ((int)buf[index+1]));
116 return (char) (firstByte << 8 | secondByte);
117 }
118
119
120
121
122
123
124
125
126 public int readLEUShort(int position, int length) {
127 return ByteSwapper.swap((short) readBEUShort(position, length));
128 }
129
130
131
132
133
134
135
136 public byte[] readIP(int position) {
137 byte[] buf = this.read(position, 4);
138 byte[] ip = new byte[buf.length];
139 ip[0] = buf[3];
140 ip[1] = buf[2];
141 ip[2] = buf[1];
142 ip[3] = buf[0];
143 return ip;
144 }
145
146
147
148
149
150
151
152
153 public String readString(int position, int length) {
154 byte[] buf = this.read(position, length);
155 StringBuilder result = new StringBuilder();
156 for(int i = 0; i < buf.length; i++) {
157 if (buf[i] != 0) {
158 char c = (char)buf[i];
159 result.append(c);
160 }
161 }
162 return result.toString();
163 }
164 }