corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * CVS Identifier:
3 *
4 * $Id: BEBufferedRandomAccessFile.java,v 1.18 2001/07/17 13:13:35 grosbois Exp $
5 *
6 * Interface: RandomAccessIO.java
7 *
8 * Description: Class for random access I/O (big-endian ordering).
9 *
10 *
11 *
12 * COPYRIGHT:
13 *
14 * This software module was originally developed by Raphaël Grosbois and
15 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
16 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
17 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
18 * Centre France S.A) in the course of development of the JPEG2000
19 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
20 * software module is an implementation of a part of the JPEG 2000
21 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
22 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
23 * Partners) agree not to assert against ISO/IEC and users of the JPEG
24 * 2000 Standard (Users) any of their rights under the copyright, not
25 * including other intellectual property rights, for this software module
26 * with respect to the usage by ISO/IEC and Users of this software module
27 * or modifications thereof for use in hardware or software products
28 * claiming conformance to the JPEG 2000 Standard. Those intending to use
29 * this software module in hardware or software products are advised that
30 * their use may infringe existing patents. The original developers of
31 * this software module, JJ2000 Partners and ISO/IEC assume no liability
32 * for use of this software module or modifications thereof. No license
33 * or right to this software module is granted for non JPEG 2000 Standard
34 * conforming products. JJ2000 Partners have full right to use this
35 * software module for his/her own purpose, assign or donate this
36 * software module to any third party and to inhibit third parties from
37 * using this software module for non JPEG 2000 Standard conforming
38 * products. This copyright notice must be included in all copies or
39 * derivative works of this software module.
40 *
41 * Copyright (c) 1999/2000 JJ2000 Partners.
42 * */
43 using System;
44 namespace CSJ2K.j2k.io
45 {
46  
47 /// <summary> This class defines a Buffered Random Access File, where all I/O is
48 /// considered to be big-endian. It extends the
49 /// <tt>BufferedRandomAccessFile</tt> class.
50 ///
51 /// </summary>
52 /// <seealso cref="RandomAccessIO">
53 /// </seealso>
54 /// <seealso cref="BinaryDataOutput">
55 /// </seealso>
56 /// <seealso cref="BinaryDataInput">
57 /// </seealso>
58 /// <seealso cref="BufferedRandomAccessFile">
59 ///
60 /// </seealso>
61 public class BEBufferedRandomAccessFile:BufferedRandomAccessFile, RandomAccessIO, EndianType
62 {
63  
64 /// <summary> Constructor. Always needs a size for the buffer.
65 ///
66 /// </summary>
67 /// <param name="file">The file associated with the buffer
68 ///
69 /// </param>
70 /// <param name="mode">"r" for read, "rw" or "rw+" for read and write mode ("rw+"
71 /// opens the file for update whereas "rw" removes it
72 /// before. So the 2 modes are different only if the file
73 /// already exists).
74 ///
75 /// </param>
76 /// <param name="bufferSize">The number of bytes to buffer
77 ///
78 /// </param>
79 /// <exception cref="java.io.IOException">If an I/O error ocurred.
80 ///
81 /// </exception>
82 public BEBufferedRandomAccessFile(System.IO.FileInfo file, System.String mode, int bufferSize):base(file, mode, bufferSize)
83 {
84 byte_Ordering = CSJ2K.j2k.io.EndianType_Fields.BIG_ENDIAN;
85 }
86  
87 /// <summary> Constructor. Uses the default value for the byte-buffer size (512
88 /// bytes).
89 ///
90 /// </summary>
91 /// <param name="file">The file associated with the buffer
92 ///
93 /// </param>
94 /// <param name="mode">"r" for read, "rw" or "rw+" for read and write mode ("rw+"
95 /// opens the file for update whereas "rw" removes it
96 /// before. So the 2 modes are different only if the file
97 /// already exists).
98 ///
99 /// </param>
100 /// <exception cref="java.io.IOException">If an I/O error ocurred.
101 ///
102 /// </exception>
103 public BEBufferedRandomAccessFile(System.IO.FileInfo file, System.String mode):base(file, mode)
104 {
105 byte_Ordering = CSJ2K.j2k.io.EndianType_Fields.BIG_ENDIAN;
106 }
107  
108 /// <summary> Constructor. Always needs a size for the buffer.
109 ///
110 /// </summary>
111 /// <param name="name">The name of the file associated with the buffer
112 ///
113 /// </param>
114 /// <param name="mode">"r" for read, "rw" or "rw+" for read and write mode ("rw+"
115 /// opens the file for update whereas "rw" removes it
116 /// before. So the 2 modes are different only if the file
117 /// already exists).
118 ///
119 /// </param>
120 /// <param name="bufferSize">The number of bytes to buffer
121 ///
122 /// </param>
123 /// <exception cref="java.io.IOException">If an I/O error ocurred.
124 ///
125 /// </exception>
126 public BEBufferedRandomAccessFile(System.String name, System.String mode, int bufferSize):base(name, mode, bufferSize)
127 {
128 byte_Ordering = CSJ2K.j2k.io.EndianType_Fields.BIG_ENDIAN;
129 }
130  
131 /// <summary> Constructor. Uses the default value for the byte-buffer size (512
132 /// bytes).
133 ///
134 /// </summary>
135 /// <param name="name">The name of the file associated with the buffer
136 ///
137 /// </param>
138 /// <param name="mode">"r" for read, "rw" or "rw+" for read and write mode ("rw+"
139 /// opens the file for update whereas "rw" removes it
140 /// before. So the 2 modes are different only if the file
141 /// already exists).
142 ///
143 /// </param>
144 /// <exception cref="java.io.IOException">If an I/O error ocurred.
145 ///
146 /// </exception>
147 public BEBufferedRandomAccessFile(System.String name, System.String mode):base(name, mode)
148 {
149 byte_Ordering = CSJ2K.j2k.io.EndianType_Fields.BIG_ENDIAN;
150 }
151  
152 /// <summary> Writes the short value of <tt>v</tt> (i.e., 16 least significant bits)
153 /// to the output. Prior to writing, the output should be realigned at the
154 /// byte level.
155 ///
156 /// <p>Signed or unsigned data can be written. To write a signed value just
157 /// pass the <tt>short</tt> value as an argument. To write unsigned data
158 /// pass the <tt>int</tt> value as an argument (it will be automatically
159 /// casted, and only the 16 least significant bits will be written).</p>
160 ///
161 /// </summary>
162 /// <param name="v">The value to write to the output
163 ///
164 /// </param>
165 /// <exception cref="java.io.IOException">If an I/O error ocurred.
166 ///
167 /// </exception>
168 public override void writeShort(int v)
169 {
170 write(SupportClass.URShift(v, 8));
171 write(v);
172 }
173  
174 /// <summary> Writes the int value of <tt>v</tt> (i.e., the 32 bits) to the
175 /// output. Prior to writing, the output should be realigned at the byte
176 /// level.
177 ///
178 /// </summary>
179 /// <param name="v">The value to write to the output
180 ///
181 /// </param>
182 /// <exception cref="java.io.IOException">If an I/O error ocurred.
183 ///
184 /// </exception>
185 public override void writeInt(int v)
186 {
187 write(SupportClass.URShift(v, 24));
188 write(SupportClass.URShift(v, 16));
189 write(SupportClass.URShift(v, 8));
190 write(v);
191 }
192  
193 /// <summary> Writes the long value of <tt>v</tt> (i.e., the 64 bits) to the
194 /// output. Prior to writing, the output should be realigned at the byte
195 /// level.
196 ///
197 /// </summary>
198 /// <param name="v">The value to write to the output
199 ///
200 /// </param>
201 /// <exception cref="java.io.IOException">If an I/O error ocurred.
202 ///
203 /// </exception>
204 public override void writeLong(long v)
205 {
206 write((int) (SupportClass.URShift(v, 56)));
207 write((int) (SupportClass.URShift(v, 48)));
208 write((int) (SupportClass.URShift(v, 40)));
209 write((int) (SupportClass.URShift(v, 32)));
210 write((int) (SupportClass.URShift(v, 24)));
211 write((int) (SupportClass.URShift(v, 16)));
212 write((int) (SupportClass.URShift(v, 8)));
213 write((int) v);
214 }
215  
216 /// <summary> Writes the IEEE float value <tt>v</tt> (i.e., 32 bits) to the
217 /// output. Prior to writing, the output should be realigned at the byte
218 /// level.
219 ///
220 /// </summary>
221 /// <param name="v">The value to write to the output
222 ///
223 /// </param>
224 /// <exception cref="java.io.IOException">If an I/O error ocurred.
225 ///
226 /// </exception>
227 public override void writeFloat(float v)
228 {
229 // CONVERSION PROBLEM? OPTIMIZE!!!
230 //byte[] floatbytes = BitConverter.GetBytes(v);
231 //for (int i = floatbytes.Length-1; i >= 0 ; i--) write(floatbytes[i]);
232  
233 //UPGRADE_ISSUE: Method 'java.lang.Float.floatToIntBits' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javalangFloatfloatToIntBits_float'"
234 //int intV = Float.floatToIntBits(v);
235 int intV = BitConverter.ToInt32(BitConverter.GetBytes(v), 0);
236 write(SupportClass.URShift(intV, 24));
237 write(SupportClass.URShift(intV, 16));
238 write(SupportClass.URShift(intV, 8));
239 write(intV);
240 }
241  
242 /// <summary> Writes the IEEE double value <tt>v</tt> (i.e., 64 bits) to the
243 /// output. Prior to writing, the output should be realigned at the byte
244 /// level.
245 ///
246 /// </summary>
247 /// <param name="v">The value to write to the output
248 ///
249 /// </param>
250 /// <exception cref="java.io.IOException">If an I/O error ocurred.
251 ///
252 /// </exception>
253 public override void writeDouble(double v)
254 {
255 //byte[] doublebytes = BitConverter.GetBytes(v);
256 //for (int i = doublebytes.Length-1; i >= 0 ; i--) write(doublebytes[i]);
257  
258 //UPGRADE_ISSUE: Method 'java.lang.Double.doubleToLongBits' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javalangDoubledoubleToLongBits_double'"
259 //long longV = Double.doubleToLongBits(v);
260 long longV = BitConverter.ToInt64(BitConverter.GetBytes(v), 0);
261 write((int) (SupportClass.URShift(longV, 56)));
262 write((int) (SupportClass.URShift(longV, 48)));
263 write((int) (SupportClass.URShift(longV, 40)));
264 write((int) (SupportClass.URShift(longV, 32)));
265 write((int) (SupportClass.URShift(longV, 24)));
266 write((int) (SupportClass.URShift(longV, 16)));
267 write((int) (SupportClass.URShift(longV, 8)));
268 write((int) (longV));
269  
270 }
271  
272 /// <summary> Reads a signed short (i.e. 16 bit) from the input. Prior to reading,
273 /// the input should be realigned at the byte level.
274 ///
275 /// </summary>
276 /// <returns> The next byte-aligned signed short (16 bit) from the input.
277 ///
278 /// </returns>
279 /// <exception cref="java.io.EOFException">If the end-of file was reached before
280 /// getting all the necessary data.
281 ///
282 /// </exception>
283 /// <exception cref="java.io.IOException">If an I/O error ocurred.
284 ///
285 /// </exception>
286 public override short readShort()
287 {
288 return (short) ((read() << 8) | (read()));
289 }
290  
291 /// <summary> Reads an unsigned short (i.e., 16 bit) from the input. It is returned
292 /// as an <tt>int</tt> since Java does not have an unsigned short
293 /// type. Prior to reading, the input should be realigned at the byte
294 /// level.
295 ///
296 /// </summary>
297 /// <returns> The next byte-aligned unsigned short (16 bit) from the input,
298 /// as an <tt>int</tt>.
299 ///
300 /// </returns>
301 /// <exception cref="java.io.EOFException">If the end-of file was reached before
302 /// getting all the necessary data.
303 ///
304 /// </exception>
305 /// <exception cref="java.io.IOException">If an I/O error ocurred.
306 ///
307 /// </exception>
308 public override int readUnsignedShort()
309 {
310 return ((read() << 8) | read());
311 }
312  
313 /// <summary> Reads a signed int (i.e., 32 bit) from the input. Prior to reading, the
314 /// input should be realigned at the byte level.
315 ///
316 /// </summary>
317 /// <returns> The next byte-aligned signed int (32 bit) from the input.
318 ///
319 /// </returns>
320 /// <exception cref="java.io.EOFException">If the end-of file was reached before
321 /// getting all the necessary data.
322 ///
323 /// </exception>
324 /// <exception cref="java.io.IOException">If an I/O error ocurred.
325 ///
326 /// </exception>
327 public override int readInt()
328 {
329 return ((read() << 24) | (read() << 16) | (read() << 8) | read());
330 }
331  
332 /// <summary> Reads an unsigned int (i.e., 32 bit) from the input. It is returned as
333 /// a <tt>long</tt> since Java does not have an unsigned short type. Prior
334 /// to reading, the input should be realigned at the byte level.
335 ///
336 /// </summary>
337 /// <returns> The next byte-aligned unsigned int (32 bit) from the input, as
338 /// a <tt>long</tt>.
339 ///
340 /// </returns>
341 /// <exception cref="java.io.EOFException">If the end-of file was reached before
342 /// getting all the necessary data.
343 ///
344 /// </exception>
345 /// <exception cref="java.io.IOException">If an I/O error ocurred.
346 ///
347 /// </exception>
348 public override long readUnsignedInt()
349 {
350 return (long) ((read() << 24) | (read() << 16) | (read() << 8) | read());
351 }
352  
353 /// <summary> Reads a signed long (i.e., 64 bit) from the input. Prior to reading,
354 /// the input should be realigned at the byte level.
355 ///
356 /// </summary>
357 /// <returns> The next byte-aligned signed long (64 bit) from the input.
358 ///
359 /// </returns>
360 /// <exception cref="java.io.EOFException">If the end-of file was reached before
361 /// getting all the necessary data.
362 ///
363 /// </exception>
364 /// <exception cref="java.io.IOException">If an I/O error ocurred.
365 ///
366 /// </exception>
367 public override long readLong()
368 {
369 //byte[] longbytes = new byte[8];
370 //for (int i = longbytes.Length-1; i >= 0; i--) longbytes[i] = read();
371 //return BitConverter.ToInt64(longbytes, 0);
372 return ((long)(((ulong) read() << 56) | ((ulong) read() << 48) | ((ulong) read() << 40) | ((ulong) read() << 32) | ((ulong) read() << 24) | ((ulong) read() << 16) | ((ulong) read() << 8) | ((ulong) read())));
373 }
374  
375 /// <summary> Reads an IEEE single precision (i.e., 32 bit) floating-point number
376 /// from the input. Prior to reading, the input should be realigned at the
377 /// byte level.
378 ///
379 /// </summary>
380 /// <returns> The next byte-aligned IEEE float (32 bit) from the input.
381 ///
382 /// </returns>
383 /// <exception cref="java.io.EOFException">If the end-of file was reached before
384 /// getting all the necessary data.
385 ///
386 /// </exception>
387 /// <exception cref="java.io.IOException">If an I/O error ocurred.
388 ///
389 /// </exception>
390 public override float readFloat()
391 {
392 // CONVERSION PROBLEM? OPTIMIZE!!!
393 //byte[] floatbytes = new byte[4];
394 //for (int i = floatbytes.Length-1; i >= 0 ; i--) floatbytes[i] = (byte)read();
395 //return BitConverter.ToSingle(floatbytes, 0);
396  
397 //UPGRADE_ISSUE: Method 'java.lang.Float.intBitsToFloat' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javalangFloatintBitsToFloat_int'"
398 //return Float.intBitsToFloat((read() << 24) | (read() << 16) | (read() << 8) | (read()));
399 return BitConverter.ToSingle(BitConverter.GetBytes((read() << 24) | (read() << 16) | (read() << 8) | (read())), 0);
400  
401 }
402  
403 /// <summary> Reads an IEEE double precision (i.e., 64 bit) floating-point number
404 /// from the input. Prior to reading, the input should be realigned at the
405 /// byte level.
406 ///
407 /// </summary>
408 /// <returns> The next byte-aligned IEEE double (64 bit) from the input.
409 ///
410 /// </returns>
411 /// <exception cref="java.io.EOFException">If the end-of file was reached before
412 /// getting all the necessary data.
413 ///
414 /// </exception>
415 /// <exception cref="java.io.IOException">If an I/O error ocurred.
416 ///
417 /// </exception>
418 public override double readDouble()
419 {
420 // CONVERSION PROBLEM? OPTIMIZE!!!
421 //byte[] doublebytes = new byte[8];
422 //for (int i = doublebytes.Length-1; i >=0 ; i--) doublebytes[i] = (byte)read();
423 //return BitConverter.ToDouble(doublebytes, 0);
424  
425 //UPGRADE_ISSUE: Method 'java.lang.Double.longBitsToDouble' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javalangDoublelongBitsToDouble_long'"
426 //return Double.longBitsToDouble(((long) read() << 56) | ((long) read() << 48) | ((long) read() << 40) | ((long) read() << 32) | ((long) read() << 24) | ((long) read() << 16) | ((long) read() << 8) | ((long) read()));
427  
428 return BitConverter.ToDouble(BitConverter.GetBytes(((long) read() << 56) | ((long) read() << 48) | ((long) read() << 40) | ((long) read() << 32) | ((long) read() << 24) | ((long) read() << 16) | ((long) read() << 8) | ((long) read())), 0);
429  
430 }
431  
432 /// <summary> Returns a string of information about the file and the endianess
433 ///
434 /// </summary>
435 public override System.String ToString()
436 {
437 return base.ToString() + "\nBig-Endian ordering";
438 }
439 }
440 }