11 |
13 Sep 07 |
nicklas |
1 |
///////////////////////////////////////////////////////////////// |
11 |
13 Sep 07 |
nicklas |
2 |
// |
11 |
13 Sep 07 |
nicklas |
// Copyright (C) 2005 Affymetrix, Inc. |
11 |
13 Sep 07 |
nicklas |
4 |
// |
11 |
13 Sep 07 |
nicklas |
// This library is free software; you can redistribute it and/or modify |
11 |
13 Sep 07 |
nicklas |
// it under the terms of the GNU Lesser General Public License as published |
11 |
13 Sep 07 |
nicklas |
// by the Free Software Foundation; either version 2.1 of the License, |
11 |
13 Sep 07 |
nicklas |
// or (at your option) any later version. |
11 |
13 Sep 07 |
nicklas |
9 |
// |
11 |
13 Sep 07 |
nicklas |
// This library is distributed in the hope that it will be useful, but |
11 |
13 Sep 07 |
nicklas |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 |
13 Sep 07 |
nicklas |
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
11 |
13 Sep 07 |
nicklas |
// for more details. |
11 |
13 Sep 07 |
nicklas |
14 |
// |
11 |
13 Sep 07 |
nicklas |
// You should have received a copy of the GNU Lesser General Public License |
11 |
13 Sep 07 |
nicklas |
// along with this library; if not, write to the Free Software Foundation, Inc., |
11 |
13 Sep 07 |
nicklas |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
11 |
13 Sep 07 |
nicklas |
18 |
// |
11 |
13 Sep 07 |
nicklas |
19 |
///////////////////////////////////////////////////////////////// |
11 |
13 Sep 07 |
nicklas |
20 |
|
11 |
13 Sep 07 |
nicklas |
21 |
|
11 |
13 Sep 07 |
nicklas |
22 |
package affymetrix.calvin.parsers; |
11 |
13 Sep 07 |
nicklas |
23 |
|
11 |
13 Sep 07 |
nicklas |
24 |
import java.io.*; |
11 |
13 Sep 07 |
nicklas |
25 |
import java.nio.*; |
11 |
13 Sep 07 |
nicklas |
26 |
import affymetrix.portability.*; |
11 |
13 Sep 07 |
nicklas |
27 |
|
11 |
13 Sep 07 |
nicklas |
/** Provides functions for reading atom data items from a file or stream. */ |
11 |
13 Sep 07 |
nicklas |
29 |
public class FileInput { |
11 |
13 Sep 07 |
nicklas |
30 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit integer from a big endian file. |
11 |
13 Sep 07 |
nicklas |
32 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file. |
11 |
13 Sep 07 |
nicklas |
35 |
*/ |
15 |
17 Sep 07 |
nicklas |
36 |
public static byte ReadInt8(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
37 |
try |
11 |
13 Sep 07 |
nicklas |
38 |
{ |
11 |
13 Sep 07 |
nicklas |
39 |
return (byte) fis.read(); |
11 |
13 Sep 07 |
nicklas |
40 |
} |
11 |
13 Sep 07 |
nicklas |
41 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
42 |
{ |
11 |
13 Sep 07 |
nicklas |
43 |
return 0; |
11 |
13 Sep 07 |
nicklas |
44 |
} |
11 |
13 Sep 07 |
nicklas |
45 |
} |
11 |
13 Sep 07 |
nicklas |
46 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit integer from a big endian file. |
11 |
13 Sep 07 |
nicklas |
48 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file. |
11 |
13 Sep 07 |
nicklas |
51 |
*/ |
15 |
17 Sep 07 |
nicklas |
52 |
public static short ReadInt16(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
53 |
byte[] b = new byte[DataSizes.SHORT_SIZE]; |
11 |
13 Sep 07 |
nicklas |
54 |
try |
11 |
13 Sep 07 |
nicklas |
55 |
{ |
11 |
13 Sep 07 |
nicklas |
56 |
fis.read(b); |
11 |
13 Sep 07 |
nicklas |
57 |
return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getShort(); |
11 |
13 Sep 07 |
nicklas |
58 |
} |
11 |
13 Sep 07 |
nicklas |
59 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
60 |
{ |
11 |
13 Sep 07 |
nicklas |
61 |
return 0; |
11 |
13 Sep 07 |
nicklas |
62 |
} |
11 |
13 Sep 07 |
nicklas |
63 |
} |
11 |
13 Sep 07 |
nicklas |
64 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 32 bit integer from a big endian file. |
11 |
13 Sep 07 |
nicklas |
66 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file. |
11 |
13 Sep 07 |
nicklas |
69 |
*/ |
15 |
17 Sep 07 |
nicklas |
70 |
public static int ReadInt32(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
71 |
byte[] b = new byte[DataSizes.INT_SIZE]; |
11 |
13 Sep 07 |
nicklas |
72 |
try |
11 |
13 Sep 07 |
nicklas |
73 |
{ |
11 |
13 Sep 07 |
nicklas |
74 |
fis.read(b); |
11 |
13 Sep 07 |
nicklas |
75 |
return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getInt(); |
11 |
13 Sep 07 |
nicklas |
76 |
} |
11 |
13 Sep 07 |
nicklas |
77 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
78 |
{ |
11 |
13 Sep 07 |
nicklas |
79 |
return 0; |
11 |
13 Sep 07 |
nicklas |
80 |
} |
11 |
13 Sep 07 |
nicklas |
81 |
} |
11 |
13 Sep 07 |
nicklas |
82 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit unsigned integer from a big endian file. |
11 |
13 Sep 07 |
nicklas |
84 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file. |
11 |
13 Sep 07 |
nicklas |
87 |
*/ |
15 |
17 Sep 07 |
nicklas |
88 |
public static byte ReadUInt8(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
89 |
try |
11 |
13 Sep 07 |
nicklas |
90 |
{ |
11 |
13 Sep 07 |
nicklas |
91 |
return (byte) fis.read(); |
11 |
13 Sep 07 |
nicklas |
92 |
} |
11 |
13 Sep 07 |
nicklas |
93 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
94 |
{ |
11 |
13 Sep 07 |
nicklas |
95 |
return 0; |
11 |
13 Sep 07 |
nicklas |
96 |
} |
11 |
13 Sep 07 |
nicklas |
97 |
} |
11 |
13 Sep 07 |
nicklas |
98 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit unsigned integer from a big endian file. |
11 |
13 Sep 07 |
nicklas |
100 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file. |
11 |
13 Sep 07 |
nicklas |
103 |
*/ |
15 |
17 Sep 07 |
nicklas |
104 |
public static short ReadUInt16(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
105 |
byte[] b = new byte[DataSizes.SHORT_SIZE]; |
11 |
13 Sep 07 |
nicklas |
106 |
try |
11 |
13 Sep 07 |
nicklas |
107 |
{ |
11 |
13 Sep 07 |
nicklas |
108 |
fis.read(b); |
11 |
13 Sep 07 |
nicklas |
109 |
return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getShort(); |
11 |
13 Sep 07 |
nicklas |
110 |
} |
11 |
13 Sep 07 |
nicklas |
111 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
112 |
{ |
11 |
13 Sep 07 |
nicklas |
113 |
return 0; |
11 |
13 Sep 07 |
nicklas |
114 |
} |
11 |
13 Sep 07 |
nicklas |
115 |
} |
11 |
13 Sep 07 |
nicklas |
116 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 32 bit unsigned integer from a big endian file. |
11 |
13 Sep 07 |
nicklas |
118 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file. |
11 |
13 Sep 07 |
nicklas |
121 |
*/ |
15 |
17 Sep 07 |
nicklas |
122 |
public static int ReadUInt32(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
123 |
byte[] b = new byte[DataSizes.INT_SIZE]; |
11 |
13 Sep 07 |
nicklas |
124 |
try |
11 |
13 Sep 07 |
nicklas |
125 |
{ |
11 |
13 Sep 07 |
nicklas |
126 |
fis.read(b); |
11 |
13 Sep 07 |
nicklas |
127 |
return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getInt(); |
11 |
13 Sep 07 |
nicklas |
128 |
} |
11 |
13 Sep 07 |
nicklas |
129 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
130 |
{ |
11 |
13 Sep 07 |
nicklas |
131 |
return 0; |
11 |
13 Sep 07 |
nicklas |
132 |
} |
11 |
13 Sep 07 |
nicklas |
133 |
} |
11 |
13 Sep 07 |
nicklas |
134 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 32 bit floating point number from a big endian file. |
11 |
13 Sep 07 |
nicklas |
136 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The floating point number read from the file. |
11 |
13 Sep 07 |
nicklas |
139 |
*/ |
15 |
17 Sep 07 |
nicklas |
140 |
public static float ReadFloat(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
141 |
byte[] b = new byte[DataSizes.FLOAT_SIZE]; |
11 |
13 Sep 07 |
nicklas |
142 |
try |
11 |
13 Sep 07 |
nicklas |
143 |
{ |
11 |
13 Sep 07 |
nicklas |
144 |
fis.read(b); |
11 |
13 Sep 07 |
nicklas |
145 |
return ByteBuffer.wrap(b).order(ByteOrder.BIG_ENDIAN).getFloat(); |
11 |
13 Sep 07 |
nicklas |
146 |
} |
11 |
13 Sep 07 |
nicklas |
147 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
148 |
{ |
11 |
13 Sep 07 |
nicklas |
149 |
return 0.0f; |
11 |
13 Sep 07 |
nicklas |
150 |
} |
11 |
13 Sep 07 |
nicklas |
151 |
} |
11 |
13 Sep 07 |
nicklas |
152 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit integer from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
154 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file stream. |
11 |
13 Sep 07 |
nicklas |
157 |
*/ |
11 |
13 Sep 07 |
nicklas |
158 |
public static byte ReadInt8(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
159 |
return b.get(); |
11 |
13 Sep 07 |
nicklas |
160 |
} |
11 |
13 Sep 07 |
nicklas |
161 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit integer from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
163 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file stream. |
11 |
13 Sep 07 |
nicklas |
166 |
*/ |
11 |
13 Sep 07 |
nicklas |
167 |
public static short ReadInt16(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
168 |
return b.getShort(); |
11 |
13 Sep 07 |
nicklas |
169 |
} |
11 |
13 Sep 07 |
nicklas |
170 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 32 bit integer from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
172 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file stream. |
11 |
13 Sep 07 |
nicklas |
175 |
*/ |
11 |
13 Sep 07 |
nicklas |
176 |
public static int ReadInt32(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
177 |
return b.getInt(); |
11 |
13 Sep 07 |
nicklas |
178 |
} |
11 |
13 Sep 07 |
nicklas |
179 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit unsigned integer from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
181 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file stream. |
11 |
13 Sep 07 |
nicklas |
184 |
*/ |
11 |
13 Sep 07 |
nicklas |
185 |
public static byte ReadUInt8(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
186 |
return b.get(); |
11 |
13 Sep 07 |
nicklas |
187 |
} |
11 |
13 Sep 07 |
nicklas |
188 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit unsigned integer from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
190 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file stream. |
11 |
13 Sep 07 |
nicklas |
193 |
*/ |
11 |
13 Sep 07 |
nicklas |
194 |
public static short ReadUInt16(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
195 |
return b.getShort(); |
11 |
13 Sep 07 |
nicklas |
196 |
} |
11 |
13 Sep 07 |
nicklas |
197 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 32 bit unsigned integer from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
199 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The integer read from the file stream. |
11 |
13 Sep 07 |
nicklas |
202 |
*/ |
11 |
13 Sep 07 |
nicklas |
203 |
public static int ReadUInt32(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
204 |
return b.getInt(); |
11 |
13 Sep 07 |
nicklas |
205 |
} |
11 |
13 Sep 07 |
nicklas |
206 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 32 bit floating point number from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
208 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The floating point number read from the file stream. |
11 |
13 Sep 07 |
nicklas |
211 |
*/ |
11 |
13 Sep 07 |
nicklas |
212 |
public static float ReadFloat(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
213 |
return b.getFloat(); |
11 |
13 Sep 07 |
nicklas |
214 |
} |
11 |
13 Sep 07 |
nicklas |
215 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit unicode string of fixed size from a big endian file. |
11 |
13 Sep 07 |
nicklas |
217 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @param len The length of the string. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
221 |
*/ |
15 |
17 Sep 07 |
nicklas |
222 |
public static String ReadString16(InputStream fis, int len) { |
11 |
13 Sep 07 |
nicklas |
223 |
try |
11 |
13 Sep 07 |
nicklas |
224 |
{ |
11 |
13 Sep 07 |
nicklas |
225 |
byte[] b = new byte[2*len]; |
11 |
13 Sep 07 |
nicklas |
226 |
fis.read(b); |
11 |
13 Sep 07 |
nicklas |
227 |
String str = new String(); |
11 |
13 Sep 07 |
nicklas |
228 |
for (int ichar=0, ibyte=0; ichar<len; ichar++, ibyte+=2) |
11 |
13 Sep 07 |
nicklas |
229 |
{ |
11 |
13 Sep 07 |
nicklas |
230 |
if (b[ibyte] == 0 && b[ibyte+1] == 0) |
11 |
13 Sep 07 |
nicklas |
231 |
continue; |
11 |
13 Sep 07 |
nicklas |
232 |
|
11 |
13 Sep 07 |
nicklas |
233 |
char c; |
11 |
13 Sep 07 |
nicklas |
234 |
if (b[ibyte] < 0) |
11 |
13 Sep 07 |
nicklas |
235 |
c = (char)((b[ibyte]+256) << 8); |
11 |
13 Sep 07 |
nicklas |
236 |
else |
11 |
13 Sep 07 |
nicklas |
237 |
c = (char)(b[ibyte] << 8); |
11 |
13 Sep 07 |
nicklas |
238 |
|
11 |
13 Sep 07 |
nicklas |
239 |
if (b[ibyte+1] < 0) |
11 |
13 Sep 07 |
nicklas |
240 |
c += (char)(b[ibyte+1] + 256); |
11 |
13 Sep 07 |
nicklas |
241 |
else |
11 |
13 Sep 07 |
nicklas |
242 |
c += (char)b[ibyte+1]; |
11 |
13 Sep 07 |
nicklas |
243 |
|
11 |
13 Sep 07 |
nicklas |
244 |
str += c; |
11 |
13 Sep 07 |
nicklas |
245 |
} |
11 |
13 Sep 07 |
nicklas |
246 |
return str; |
11 |
13 Sep 07 |
nicklas |
247 |
} |
11 |
13 Sep 07 |
nicklas |
248 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
249 |
{ |
11 |
13 Sep 07 |
nicklas |
250 |
} |
11 |
13 Sep 07 |
nicklas |
251 |
return null; |
11 |
13 Sep 07 |
nicklas |
252 |
} |
11 |
13 Sep 07 |
nicklas |
253 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit unicode string from a big endian file. |
11 |
13 Sep 07 |
nicklas |
255 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
258 |
*/ |
15 |
17 Sep 07 |
nicklas |
259 |
public static String ReadString16(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
260 |
int len = ReadInt32(fis); |
11 |
13 Sep 07 |
nicklas |
261 |
return ReadString16(fis, len); |
11 |
13 Sep 07 |
nicklas |
262 |
} |
11 |
13 Sep 07 |
nicklas |
263 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit string of fixed size from a big endian file. |
11 |
13 Sep 07 |
nicklas |
265 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @param len The length of the string. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
269 |
*/ |
15 |
17 Sep 07 |
nicklas |
270 |
public static String ReadString8(InputStream fis, int len) { |
11 |
13 Sep 07 |
nicklas |
271 |
try |
11 |
13 Sep 07 |
nicklas |
272 |
{ |
11 |
13 Sep 07 |
nicklas |
273 |
byte[] b = new byte[len]; |
11 |
13 Sep 07 |
nicklas |
274 |
fis.read(b); |
11 |
13 Sep 07 |
nicklas |
275 |
String str = new String(); |
11 |
13 Sep 07 |
nicklas |
276 |
for (int i=0; i<len; i++) |
11 |
13 Sep 07 |
nicklas |
277 |
{ |
11 |
13 Sep 07 |
nicklas |
278 |
if (b[i] == 0) |
11 |
13 Sep 07 |
nicklas |
279 |
break; |
11 |
13 Sep 07 |
nicklas |
280 |
str += (char) b[i]; |
11 |
13 Sep 07 |
nicklas |
281 |
} |
11 |
13 Sep 07 |
nicklas |
282 |
return str; |
11 |
13 Sep 07 |
nicklas |
283 |
} |
11 |
13 Sep 07 |
nicklas |
284 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
285 |
{ |
11 |
13 Sep 07 |
nicklas |
286 |
} |
11 |
13 Sep 07 |
nicklas |
287 |
return null; |
11 |
13 Sep 07 |
nicklas |
288 |
} |
11 |
13 Sep 07 |
nicklas |
289 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit string from a big endian file. |
11 |
13 Sep 07 |
nicklas |
291 |
* |
11 |
13 Sep 07 |
nicklas |
* @param fis The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
294 |
*/ |
15 |
17 Sep 07 |
nicklas |
295 |
public static String ReadString8(InputStream fis) { |
11 |
13 Sep 07 |
nicklas |
296 |
int len = ReadInt32(fis); |
11 |
13 Sep 07 |
nicklas |
297 |
return ReadString8(fis, len); |
11 |
13 Sep 07 |
nicklas |
298 |
} |
11 |
13 Sep 07 |
nicklas |
299 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit unicode string of fixed size from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
301 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @param len The length of the string. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
305 |
*/ |
11 |
13 Sep 07 |
nicklas |
306 |
public static String ReadString16(ByteBuffer b, int len) { |
11 |
13 Sep 07 |
nicklas |
307 |
try |
11 |
13 Sep 07 |
nicklas |
308 |
{ |
11 |
13 Sep 07 |
nicklas |
309 |
String str = new String(); |
11 |
13 Sep 07 |
nicklas |
310 |
for (int i=0; i<len; i++) |
11 |
13 Sep 07 |
nicklas |
311 |
{ |
11 |
13 Sep 07 |
nicklas |
312 |
byte b1=b.get(); |
11 |
13 Sep 07 |
nicklas |
313 |
byte b2=b.get(); |
11 |
13 Sep 07 |
nicklas |
314 |
if (b1 == 0 && b2 == 0) |
11 |
13 Sep 07 |
nicklas |
315 |
continue; |
11 |
13 Sep 07 |
nicklas |
316 |
|
11 |
13 Sep 07 |
nicklas |
317 |
char c; |
11 |
13 Sep 07 |
nicklas |
318 |
if (b1 < 0) |
11 |
13 Sep 07 |
nicklas |
319 |
c = (char)((b1+256) << 8); |
11 |
13 Sep 07 |
nicklas |
320 |
else |
11 |
13 Sep 07 |
nicklas |
321 |
c = (char)(b1 << 8); |
11 |
13 Sep 07 |
nicklas |
322 |
|
11 |
13 Sep 07 |
nicklas |
323 |
if (b2 < 0) |
11 |
13 Sep 07 |
nicklas |
324 |
c += (char)(b2 + 256); |
11 |
13 Sep 07 |
nicklas |
325 |
else |
11 |
13 Sep 07 |
nicklas |
326 |
c += (char)b2; |
11 |
13 Sep 07 |
nicklas |
327 |
|
11 |
13 Sep 07 |
nicklas |
328 |
str += c; |
11 |
13 Sep 07 |
nicklas |
329 |
} |
11 |
13 Sep 07 |
nicklas |
330 |
return str; |
11 |
13 Sep 07 |
nicklas |
331 |
} |
11 |
13 Sep 07 |
nicklas |
332 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
333 |
{ |
11 |
13 Sep 07 |
nicklas |
334 |
} |
11 |
13 Sep 07 |
nicklas |
335 |
return null; |
11 |
13 Sep 07 |
nicklas |
336 |
} |
11 |
13 Sep 07 |
nicklas |
337 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a 16 bit unicode string from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
339 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
342 |
*/ |
11 |
13 Sep 07 |
nicklas |
343 |
public static String ReadString16(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
344 |
int len = ReadInt32(b); |
11 |
13 Sep 07 |
nicklas |
345 |
return ReadString16(b, len); |
11 |
13 Sep 07 |
nicklas |
346 |
} |
11 |
13 Sep 07 |
nicklas |
347 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit string of fixed size from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
349 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @param len The length of the string. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
353 |
*/ |
11 |
13 Sep 07 |
nicklas |
354 |
public static String ReadString8(ByteBuffer b, int len) { |
11 |
13 Sep 07 |
nicklas |
355 |
try |
11 |
13 Sep 07 |
nicklas |
356 |
{ |
11 |
13 Sep 07 |
nicklas |
357 |
String str = ""; |
11 |
13 Sep 07 |
nicklas |
358 |
byte bvalue; |
11 |
13 Sep 07 |
nicklas |
359 |
for (int i=0; i<len; i++) |
11 |
13 Sep 07 |
nicklas |
360 |
{ |
11 |
13 Sep 07 |
nicklas |
361 |
bvalue = b.get(); |
11 |
13 Sep 07 |
nicklas |
362 |
if (bvalue == 0) |
11 |
13 Sep 07 |
nicklas |
363 |
continue; |
11 |
13 Sep 07 |
nicklas |
364 |
str += (char) bvalue; |
11 |
13 Sep 07 |
nicklas |
365 |
} |
11 |
13 Sep 07 |
nicklas |
366 |
return str; |
11 |
13 Sep 07 |
nicklas |
367 |
} |
11 |
13 Sep 07 |
nicklas |
368 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
369 |
{ |
11 |
13 Sep 07 |
nicklas |
370 |
} |
11 |
13 Sep 07 |
nicklas |
371 |
return null; |
11 |
13 Sep 07 |
nicklas |
372 |
} |
11 |
13 Sep 07 |
nicklas |
373 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads an 8 bit string from a big endian file stream (memory map pointer). |
11 |
13 Sep 07 |
nicklas |
375 |
* |
11 |
13 Sep 07 |
nicklas |
* @param b The input file stream. |
11 |
13 Sep 07 |
nicklas |
* @return The string read from the file stream. |
11 |
13 Sep 07 |
nicklas |
378 |
*/ |
11 |
13 Sep 07 |
nicklas |
379 |
public static String ReadString8(ByteBuffer b) { |
11 |
13 Sep 07 |
nicklas |
380 |
int len = ReadInt32(b); |
11 |
13 Sep 07 |
nicklas |
381 |
return ReadString8(b, len); |
11 |
13 Sep 07 |
nicklas |
382 |
} |
11 |
13 Sep 07 |
nicklas |
383 |
|
11 |
13 Sep 07 |
nicklas |
/** Reads a blob from a file. |
11 |
13 Sep 07 |
nicklas |
385 |
*/ |
15 |
17 Sep 07 |
nicklas |
386 |
public static byte[] ReadBlob(InputStream fis) |
11 |
13 Sep 07 |
nicklas |
387 |
{ |
11 |
13 Sep 07 |
nicklas |
388 |
try |
11 |
13 Sep 07 |
nicklas |
389 |
{ |
11 |
13 Sep 07 |
nicklas |
390 |
int size = FileInput.ReadInt32(fis); |
11 |
13 Sep 07 |
nicklas |
391 |
byte[] value = new byte[size]; |
11 |
13 Sep 07 |
nicklas |
392 |
fis.read(value); |
11 |
13 Sep 07 |
nicklas |
393 |
return value; |
11 |
13 Sep 07 |
nicklas |
394 |
} |
11 |
13 Sep 07 |
nicklas |
395 |
catch (Throwable t) |
11 |
13 Sep 07 |
nicklas |
396 |
{ |
11 |
13 Sep 07 |
nicklas |
397 |
} |
11 |
13 Sep 07 |
nicklas |
398 |
return null; |
11 |
13 Sep 07 |
nicklas |
399 |
} |
11 |
13 Sep 07 |
nicklas |
400 |
|
11 |
13 Sep 07 |
nicklas |
401 |
} |