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 045package jj2000.j2k.io; 046 047import 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 */ 058public class BEBufferedRandomAccessFile extends BufferedRandomAccessFile 059 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, 076 String mode, 077 int bufferSize) throws IOException{ 078 super(file, mode, bufferSize); 079 byteOrdering = BIG_ENDIAN; 080 } 081 082 /** 083 * Constructor. Uses the default value for the byte-buffer size (512 084 * bytes). 085 * 086 * @param file The file associated with the buffer 087 * 088 * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+" 089 * opens the file for update whereas "rw" removes it 090 * before. So the 2 modes are different only if the file 091 * already exists). 092 * 093 * @exception java.io.IOException If an I/O error ocurred. 094 * */ 095 public BEBufferedRandomAccessFile(File file, 096 String mode ) throws IOException{ 097 super(file, mode); 098 byteOrdering = BIG_ENDIAN; 099 } 100 101 /** 102 * Constructor. Always needs a size for the buffer. 103 * 104 * @param name The name of the file associated with the buffer 105 * 106 * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+" 107 * opens the file for update whereas "rw" removes it 108 * before. So the 2 modes are different only if the file 109 * already exists). 110 * 111 * @param bufferSize The number of bytes to buffer 112 * 113 * @exception java.io.IOException If an I/O error ocurred. 114 * */ 115 public BEBufferedRandomAccessFile(String name, 116 String mode, 117 int bufferSize) throws IOException{ 118 super(name, mode, bufferSize); 119 byteOrdering = BIG_ENDIAN; 120 } 121 122 /** 123 * Constructor. Uses the default value for the byte-buffer size (512 124 * bytes). 125 * 126 * @param name The name of the file associated with the buffer 127 * 128 * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+" 129 * opens the file for update whereas "rw" removes it 130 * before. So the 2 modes are different only if the file 131 * already exists). 132 * 133 * @exception java.io.IOException If an I/O error ocurred. 134 * */ 135 public BEBufferedRandomAccessFile(String name, 136 String mode ) throws IOException{ 137 super(name, mode); 138 byteOrdering = BIG_ENDIAN; 139 } 140 141 /** 142 * Writes the short value of <tt>v</tt> (i.e., 16 least significant bits) 143 * to the output. Prior to writing, the output should be realigned at the 144 * byte level. 145 * 146 * <P>Signed or unsigned data can be written. To write a signed value just 147 * pass the <tt>short</tt> value as an argument. To write unsigned data 148 * pass the <tt>int</tt> value as an argument (it will be automatically 149 * casted, and only the 16 least significant bits will be written). 150 * 151 * @param v The value to write to the output 152 * 153 * @exception java.io.IOException If an I/O error ocurred. 154 * */ 155 public final void writeShort(int v) throws IOException{ 156 write(v>>>8); 157 write(v); 158 } 159 160 /** 161 * Writes the int value of <tt>v</tt> (i.e., the 32 bits) to the 162 * output. Prior to writing, the output should be realigned at the byte 163 * level. 164 * 165 * @param v The value to write to the output 166 * 167 * @exception java.io.IOException If an I/O error ocurred. 168 * */ 169 public final void writeInt(int v)throws IOException{ 170 write(v>>>24); 171 write(v>>>16); 172 write(v>>>8); 173 write(v); 174 } 175 176 /** 177 * Writes the long value of <tt>v</tt> (i.e., the 64 bits) to the 178 * output. Prior to writing, the output should be realigned at the byte 179 * level. 180 * 181 * @param v The value to write to the output 182 * 183 * @exception java.io.IOException If an I/O error ocurred. 184 * */ 185 public final void writeLong(long v)throws IOException{ 186 write((int)(v>>>56)); 187 write((int)(v>>>48)); 188 write((int)(v>>>40)); 189 write((int)(v>>>32)); 190 write((int)(v>>>24)); 191 write((int)(v>>>16)); 192 write((int)(v>>>8)); 193 write((int)v); 194 } 195 196 /** 197 * Writes the IEEE float value <tt>v</tt> (i.e., 32 bits) to the 198 * output. Prior to writing, the output should be realigned at the byte 199 * level. 200 * 201 * @param v The value to write to the output 202 * 203 * @exception java.io.IOException If an I/O error ocurred. 204 * */ 205 public final void writeFloat(float v) throws IOException{ 206 int intV = Float.floatToIntBits(v); 207 208 write(intV>>>24); 209 write(intV>>>16); 210 write(intV>>>8); 211 write(intV); 212 } 213 214 /** 215 * Writes the IEEE double value <tt>v</tt> (i.e., 64 bits) to the 216 * output. Prior to writing, the output should be realigned at the byte 217 * level. 218 * 219 * @param v The value to write to the output 220 * 221 * @exception java.io.IOException If an I/O error ocurred. 222 * */ 223 public final void writeDouble(double v)throws IOException{ 224 long longV = Double.doubleToLongBits(v); 225 226 write((int)(longV>>>56)); 227 write((int)(longV>>>48)); 228 write((int)(longV>>>40)); 229 write((int)(longV>>>32)); 230 write((int)(longV>>>24)); 231 write((int)(longV>>>16)); 232 write((int)(longV>>>8)); 233 write((int)(longV)); 234 } 235 236 /** 237 * Reads a signed short (i.e., 16 bit) from the input. Prior to reading, 238 * the input should be realigned at the byte level. 239 * 240 * @return The next byte-aligned signed short (16 bit) from the 241 * input. 242 * 243 * @exception java.io.EOFException If the end-of file was reached before 244 * getting all the necessary data. 245 * 246 * @exception java.io.IOException If an I/O error ocurred. 247 * */ 248 public final short readShort() throws IOException, EOFException{ 249 return (short)( 250 (read()<<8)| 251 (read()) 252 ); 253 } 254 255 /** 256 * Reads an unsigned short (i.e., 16 bit) from the input. It is returned 257 * as an <tt>int</tt> since Java does not have an unsigned short 258 * type. Prior to reading, the input should be realigned at the byte 259 * level. 260 * 261 * @return The next byte-aligned unsigned short (16 bit) from the 262 * input, as an <tt>int</tt>. 263 * 264 * @exception java.io.EOFException If the end-of file was reached before 265 * getting all the necessary data. 266 * 267 * @exception java.io.IOException If an I/O error ocurred. 268 * */ 269 public final int readUnsignedShort() throws IOException, EOFException{ 270 return ( 271 (read()<<8)| 272 read() 273 ); 274 } 275 276 /** 277 * Reads a signed int (i.e., 32 bit) from the input. Prior to reading, the 278 * input should be realigned at the byte level. 279 * 280 * @return The next byte-aligned signed int (32 bit) from the 281 * input. 282 * 283 * @exception java.io.EOFException If the end-of file was reached before 284 * getting all the necessary data. 285 * 286 * @exception java.io.IOException If an I/O error ocurred. 287 * */ 288 public final int readInt() throws IOException, EOFException{ 289 return ( 290 (read()<<24)| 291 (read()<<16)| 292 (read()<<8)| 293 read() 294 ); 295 } 296 297 /** 298 * Reads an unsigned int (i.e., 32 bit) from the input. It is returned as 299 * a <tt>long</tt> since Java does not have an unsigned short type. Prior 300 * to reading, the input should be realigned at the byte level. 301 * 302 * @return The next byte-aligned unsigned int (32 bit) from the 303 * input, as a <tt>long</tt>. 304 * 305 * @exception java.io.EOFException If the end-of file was reached before 306 * getting all the necessary data. 307 * 308 * @exception java.io.IOException If an I/O error ocurred. 309 * */ 310 public final long readUnsignedInt() throws IOException, EOFException{ 311 return (long)( 312 (read()<<24)| 313 (read()<<16)| 314 (read()<<8)| 315 read() 316 ); 317 } 318 319 /** 320 * Reads a signed long (i.e., 64 bit) from the input. Prior to reading, 321 * the input should be realigned at the byte level. 322 * 323 * @return The next byte-aligned signed long (64 bit) from the 324 * input. 325 * 326 * @exception java.io.EOFException If the end-of file was reached before 327 * getting all the necessary data. 328 * 329 * @exception java.io.IOException If an I/O error ocurred. 330 * */ 331 public final long readLong() throws IOException, EOFException{ 332 return ( 333 ((long)read()<<56)| 334 ((long)read()<<48)| 335 ((long)read()<<40)| 336 ((long)read()<<32)| 337 ((long)read()<<24)| 338 ((long)read()<<16)| 339 ((long)read()<<8)| 340 ((long)read()) 341 ); 342 } 343 344 /** 345 * Reads an IEEE single precision (i.e., 32 bit) floating-point number 346 * from the input. Prior to reading, the input should be realigned at the 347 * byte level. 348 * 349 * @return The next byte-aligned IEEE float (32 bit) from the 350 * input. 351 * 352 * @exception java.io.EOFException If the end-of file was reached before 353 * getting all the necessary data. 354 * 355 * @exception java.io.IOException If an I/O error ocurred. 356 * */ 357 public final float readFloat() throws EOFException, IOException{ 358 return Float.intBitsToFloat( 359 (read()<<24)| 360 (read()<<16)| 361 (read()<<8)| 362 (read()) 363 ); 364 } 365 366 /** 367 * Reads an IEEE double precision (i.e., 64 bit) floating-point number 368 * from the input. Prior to reading, the input should be realigned at the 369 * byte level. 370 * 371 * @return The next byte-aligned IEEE double (64 bit) from the 372 * input. 373 * 374 * @exception java.io.EOFException If the end-of file was reached before 375 * getting all the necessary data. 376 * 377 * @exception java.io.IOException If an I/O error ocurred. 378 * */ 379 public final double readDouble() throws IOException, EOFException{ 380 return Double.longBitsToDouble( 381 ((long)read()<<56)| 382 ((long)read()<<48)| 383 ((long)read()<<40)| 384 ((long)read()<<32)| 385 ((long)read()<<24)| 386 ((long)read()<<16)| 387 ((long)read()<<8)| 388 ((long)read()) 389 ); 390 } 391 392 /** 393 * Returns a string of information about the file and the endianess 394 */ 395 public String toString(){ 396 return super.toString()+"\nBig-Endian ordering"; 397 } 398}