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