001: /*
002: * $RCSfile: BEBufferedRandomAccessFile.java,v $
003: * $Revision: 1.1 $
004: * $Date: 2005/02/11 05:02:15 $
005: * $State: Exp $
006: *
007: * Interface: RandomAccessIO.java
008: *
009: * Description: Class for random access I/O (big-endian ordering).
010: *
011: *
012: *
013: * COPYRIGHT:
014: *
015: * This software module was originally developed by Raphaël Grosbois and
016: * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
017: * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
018: * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
019: * Centre France S.A) in the course of development of the JPEG2000
020: * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
021: * software module is an implementation of a part of the JPEG 2000
022: * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
023: * Systems AB and Canon Research Centre France S.A (collectively JJ2000
024: * Partners) agree not to assert against ISO/IEC and users of the JPEG
025: * 2000 Standard (Users) any of their rights under the copyright, not
026: * including other intellectual property rights, for this software module
027: * with respect to the usage by ISO/IEC and Users of this software module
028: * or modifications thereof for use in hardware or software products
029: * claiming conformance to the JPEG 2000 Standard. Those intending to use
030: * this software module in hardware or software products are advised that
031: * their use may infringe existing patents. The original developers of
032: * this software module, JJ2000 Partners and ISO/IEC assume no liability
033: * for use of this software module or modifications thereof. No license
034: * or right to this software module is granted for non JPEG 2000 Standard
035: * conforming products. JJ2000 Partners have full right to use this
036: * software module for his/her own purpose, assign or donate this
037: * software module to any third party and to inhibit third parties from
038: * using this software module for non JPEG 2000 Standard conforming
039: * products. This copyright notice must be included in all copies or
040: * derivative works of this software module.
041: *
042: * Copyright (c) 1999/2000 JJ2000 Partners.
043: */
044:
045: package jj2000.j2k.io;
046:
047: import java.io.*;
048:
049: /**
050: * This class defines a Buffered Random Access File, where all I/O is
051: * considered to be big-endian, and extends the
052: * <tt>BufferedRandomAccessFile</tt> class.
053: *
054: * @see RandomAccessIO
055: * @see BinaryDataOutput
056: * @see BinaryDataInput
057: * @see BufferedRandomAccessFile */
058: public class BEBufferedRandomAccessFile extends
059: BufferedRandomAccessFile implements RandomAccessIO, EndianType {
060:
061: /**
062: * Constructor. Always needs a size for the buffer.
063: *
064: * @param file The file associated with the buffer
065: *
066: * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+"
067: * opens the file for update whereas "rw" removes it
068: * before. So the 2 modes are different only if the file
069: * already exists).
070: *
071: * @param bufferSize The number of bytes to buffer
072: *
073: * @exception java.io.IOException If an I/O error ocurred.
074: * */
075: public BEBufferedRandomAccessFile(File file, String mode,
076: int bufferSize) throws IOException {
077: super (file, mode, bufferSize);
078: byteOrdering = BIG_ENDIAN;
079: }
080:
081: /**
082: * Constructor. Uses the default value for the byte-buffer size (512
083: * bytes).
084: *
085: * @param file The file associated with the buffer
086: *
087: * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+"
088: * opens the file for update whereas "rw" removes it
089: * before. So the 2 modes are different only if the file
090: * already exists).
091: *
092: * @exception java.io.IOException If an I/O error ocurred.
093: * */
094: public BEBufferedRandomAccessFile(File file, String mode)
095: throws IOException {
096: super (file, mode);
097: byteOrdering = BIG_ENDIAN;
098: }
099:
100: /**
101: * Constructor. Always needs a size for the buffer.
102: *
103: * @param name The name of the file associated with the buffer
104: *
105: * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+"
106: * opens the file for update whereas "rw" removes it
107: * before. So the 2 modes are different only if the file
108: * already exists).
109: *
110: * @param bufferSize The number of bytes to buffer
111: *
112: * @exception java.io.IOException If an I/O error ocurred.
113: * */
114: public BEBufferedRandomAccessFile(String name, String mode,
115: int bufferSize) throws IOException {
116: super (name, mode, bufferSize);
117: byteOrdering = BIG_ENDIAN;
118: }
119:
120: /**
121: * Constructor. Uses the default value for the byte-buffer size (512
122: * bytes).
123: *
124: * @param name The name of the file associated with the buffer
125: *
126: * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+"
127: * opens the file for update whereas "rw" removes it
128: * before. So the 2 modes are different only if the file
129: * already exists).
130: *
131: * @exception java.io.IOException If an I/O error ocurred.
132: * */
133: public BEBufferedRandomAccessFile(String name, String mode)
134: throws IOException {
135: super (name, mode);
136: byteOrdering = BIG_ENDIAN;
137: }
138:
139: /**
140: * Writes the short value of <tt>v</tt> (i.e., 16 least significant bits)
141: * to the output. Prior to writing, the output should be realigned at the
142: * byte level.
143: *
144: * <P>Signed or unsigned data can be written. To write a signed value just
145: * pass the <tt>short</tt> value as an argument. To write unsigned data
146: * pass the <tt>int</tt> value as an argument (it will be automatically
147: * casted, and only the 16 least significant bits will be written).
148: *
149: * @param v The value to write to the output
150: *
151: * @exception java.io.IOException If an I/O error ocurred.
152: * */
153: public final void writeShort(int v) throws IOException {
154: write(v >>> 8);
155: write(v);
156: }
157:
158: /**
159: * Writes the int value of <tt>v</tt> (i.e., the 32 bits) to the
160: * output. Prior to writing, the output should be realigned at the byte
161: * level.
162: *
163: * @param v The value to write to the output
164: *
165: * @exception java.io.IOException If an I/O error ocurred.
166: * */
167: public final void writeInt(int v) throws IOException {
168: write(v >>> 24);
169: write(v >>> 16);
170: write(v >>> 8);
171: write(v);
172: }
173:
174: /**
175: * Writes the long value of <tt>v</tt> (i.e., the 64 bits) to the
176: * output. Prior to writing, the output should be realigned at the byte
177: * level.
178: *
179: * @param v The value to write to the output
180: *
181: * @exception java.io.IOException If an I/O error ocurred.
182: * */
183: public final void writeLong(long v) throws IOException {
184: write((int) (v >>> 56));
185: write((int) (v >>> 48));
186: write((int) (v >>> 40));
187: write((int) (v >>> 32));
188: write((int) (v >>> 24));
189: write((int) (v >>> 16));
190: write((int) (v >>> 8));
191: write((int) v);
192: }
193:
194: /**
195: * Writes the IEEE float value <tt>v</tt> (i.e., 32 bits) to the
196: * output. Prior to writing, the output should be realigned at the byte
197: * level.
198: *
199: * @param v The value to write to the output
200: *
201: * @exception java.io.IOException If an I/O error ocurred.
202: * */
203: public final void writeFloat(float v) throws IOException {
204: int intV = Float.floatToIntBits(v);
205:
206: write(intV >>> 24);
207: write(intV >>> 16);
208: write(intV >>> 8);
209: write(intV);
210: }
211:
212: /**
213: * Writes the IEEE double value <tt>v</tt> (i.e., 64 bits) to the
214: * output. Prior to writing, the output should be realigned at the byte
215: * level.
216: *
217: * @param v The value to write to the output
218: *
219: * @exception java.io.IOException If an I/O error ocurred.
220: * */
221: public final void writeDouble(double v) throws IOException {
222: long longV = Double.doubleToLongBits(v);
223:
224: write((int) (longV >>> 56));
225: write((int) (longV >>> 48));
226: write((int) (longV >>> 40));
227: write((int) (longV >>> 32));
228: write((int) (longV >>> 24));
229: write((int) (longV >>> 16));
230: write((int) (longV >>> 8));
231: write((int) (longV));
232: }
233:
234: /**
235: * Reads a signed short (i.e., 16 bit) from the input. Prior to reading,
236: * the input should be realigned at the byte level.
237: *
238: * @return The next byte-aligned signed short (16 bit) from the
239: * input.
240: *
241: * @exception java.io.EOFException If the end-of file was reached before
242: * getting all the necessary data.
243: *
244: * @exception java.io.IOException If an I/O error ocurred.
245: * */
246: public final short readShort() throws IOException, EOFException {
247: return (short) ((read() << 8) | (read()));
248: }
249:
250: /**
251: * Reads an unsigned short (i.e., 16 bit) from the input. It is returned
252: * as an <tt>int</tt> since Java does not have an unsigned short
253: * type. Prior to reading, the input should be realigned at the byte
254: * level.
255: *
256: * @return The next byte-aligned unsigned short (16 bit) from the
257: * input, as an <tt>int</tt>.
258: *
259: * @exception java.io.EOFException If the end-of file was reached before
260: * getting all the necessary data.
261: *
262: * @exception java.io.IOException If an I/O error ocurred.
263: * */
264: public final int readUnsignedShort() throws IOException,
265: EOFException {
266: return ((read() << 8) | read());
267: }
268:
269: /**
270: * Reads a signed int (i.e., 32 bit) from the input. Prior to reading, the
271: * input should be realigned at the byte level.
272: *
273: * @return The next byte-aligned signed int (32 bit) from the
274: * input.
275: *
276: * @exception java.io.EOFException If the end-of file was reached before
277: * getting all the necessary data.
278: *
279: * @exception java.io.IOException If an I/O error ocurred.
280: * */
281: public final int readInt() throws IOException, EOFException {
282: return ((read() << 24) | (read() << 16) | (read() << 8) | read());
283: }
284:
285: /**
286: * Reads an unsigned int (i.e., 32 bit) from the input. It is returned as
287: * a <tt>long</tt> since Java does not have an unsigned short type. Prior
288: * to reading, the input should be realigned at the byte level.
289: *
290: * @return The next byte-aligned unsigned int (32 bit) from the
291: * input, as a <tt>long</tt>.
292: *
293: * @exception java.io.EOFException If the end-of file was reached before
294: * getting all the necessary data.
295: *
296: * @exception java.io.IOException If an I/O error ocurred.
297: * */
298: public final long readUnsignedInt() throws IOException,
299: EOFException {
300: return (long) ((read() << 24) | (read() << 16) | (read() << 8) | read());
301: }
302:
303: /**
304: * Reads a signed long (i.e., 64 bit) from the input. Prior to reading,
305: * the input should be realigned at the byte level.
306: *
307: * @return The next byte-aligned signed long (64 bit) from the
308: * input.
309: *
310: * @exception java.io.EOFException If the end-of file was reached before
311: * getting all the necessary data.
312: *
313: * @exception java.io.IOException If an I/O error ocurred.
314: * */
315: public final long readLong() throws IOException, EOFException {
316: return (((long) read() << 56) | ((long) read() << 48)
317: | ((long) read() << 40) | ((long) read() << 32)
318: | ((long) read() << 24) | ((long) read() << 16)
319: | ((long) read() << 8) | ((long) read()));
320: }
321:
322: /**
323: * Reads an IEEE single precision (i.e., 32 bit) floating-point number
324: * from the input. Prior to reading, the input should be realigned at the
325: * byte level.
326: *
327: * @return The next byte-aligned IEEE float (32 bit) from the
328: * input.
329: *
330: * @exception java.io.EOFException If the end-of file was reached before
331: * getting all the necessary data.
332: *
333: * @exception java.io.IOException If an I/O error ocurred.
334: * */
335: public final float readFloat() throws EOFException, IOException {
336: return Float.intBitsToFloat((read() << 24) | (read() << 16)
337: | (read() << 8) | (read()));
338: }
339:
340: /**
341: * Reads an IEEE double precision (i.e., 64 bit) floating-point number
342: * from the input. Prior to reading, the input should be realigned at the
343: * byte level.
344: *
345: * @return The next byte-aligned IEEE double (64 bit) from the
346: * input.
347: *
348: * @exception java.io.EOFException If the end-of file was reached before
349: * getting all the necessary data.
350: *
351: * @exception java.io.IOException If an I/O error ocurred.
352: * */
353: public final double readDouble() throws IOException, EOFException {
354: return Double.longBitsToDouble(((long) read() << 56)
355: | ((long) read() << 48) | ((long) read() << 40)
356: | ((long) read() << 32) | ((long) read() << 24)
357: | ((long) read() << 16) | ((long) read() << 8)
358: | ((long) read()));
359: }
360:
361: /**
362: * Returns a string of information about the file and the endianess
363: */
364: public String toString() {
365: return super .toString() + "\nBig-Endian ordering";
366: }
367: }
|