📄 bufferedrandomaccessfile.java
字号:
/* * CVS Identifier: * * $Id: BufferedRandomAccessFile.java,v 1.20 2000/11/22 17:10:45 qtxjoas Exp $ * * Interface: RandomAccessIO.java * * Description: Abstract class for buffered random access I/O. * * * * COPYRIGHT: * * This software module was originally developed by Rapha雔 Grosbois and * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel * Askel鰂 (Ericsson Radio Systems AB); and Bertrand Berthelot, David * Bouchard, F閘ix Henry, Gerard Mozelle and Patrice Onno (Canon Research * Centre France S.A) in the course of development of the JPEG2000 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This * software module is an implementation of a part of the JPEG 2000 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio * Systems AB and Canon Research Centre France S.A (collectively JJ2000 * Partners) agree not to assert against ISO/IEC and users of the JPEG * 2000 Standard (Users) any of their rights under the copyright, not * including other intellectual property rights, for this software module * with respect to the usage by ISO/IEC and Users of this software module * or modifications thereof for use in hardware or software products * claiming conformance to the JPEG 2000 Standard. Those intending to use * this software module in hardware or software products are advised that * their use may infringe existing patents. The original developers of * this software module, JJ2000 Partners and ISO/IEC assume no liability * for use of this software module or modifications thereof. No license * or right to this software module is granted for non JPEG 2000 Standard * conforming products. JJ2000 Partners have full right to use this * software module for his/her own purpose, assign or donate this * software module to any third party and to inhibit third parties from * using this software module for non JPEG 2000 Standard conforming * products. This copyright notice must be included in all copies or * derivative works of this software module. * * Copyright (c) 1999/2000 JJ2000 Partners. */package jj2000.j2k.io;import java.io.*;/** * This class defines a Buffered Random Access File. It implements the * <tt>BinaryDataInput</tt> and <tt>BinaryDataOutput</tt> interfaces so that * binary data input/output can be performed. This class is abstract since no * assumption is done about the byte ordering type (little Endian, big * Endian). So subclasses will have to implement methods like * <tt>readShort()</tt>, <tt>writeShort()</tt>, <tt>readFloat()</tt>, ... * * <P><tt>BufferedRandomAccessFile</tt> (BRAF for short) is a * <tt>RandomAccessFile</tt> containing an extra buffer. When the BRAF is * accessed, it checks if the requested part of the file is in the buffer or * not. If that is the case, the read/write is done on the buffer. If not, the * file is uppdated to reflect the current status of the buffer and the file * is then accessed for a new buffer containing the requested byte/bit. * * @see RandomAccessIO * @see BinaryDataOutput * @see BinaryDataInput * @see BEBufferedRandomAccessFile * */public abstract class BufferedRandomAccessFile implements RandomAccessIO, EndianType { /** * The name of the current file * */ private String fileName; /** * Whether the opened file is read only or not (defined by the constructor * arguments) * */ private boolean isReadOnly = true; /** * The RandomAccessFile associated with the buffer * */ private RandomAccessFile theFile; /** * Buffer of bytes containing the part of the file that is currently being * accessed * */ protected byte[] byteBuffer; /** * Boolean keeping track of whether the byte buffer has been changed since * it was read. * */ protected boolean byteBufferChanged; /** * The current offset of the buffer (which will differ from the offset of * the file) * */ protected int offset; /** * The current position in the byte-buffer * */ protected int pos; /** * The maximum number of bytes that can be read from the buffer * */ protected int maxByte; /** * Whether the end of the file is in the current buffer or not * */ protected boolean isEOFInBuffer; /* The endianess of the class */ protected int byteOrdering; /** * Constructor. Always needs a size for the buffer. * * @param file The file associated with the buffer * * @param mode "r" for read, "rw" or "rw+" for read and write mode ("rw+" * opens the file for update whereas "rw" removes it * before. So the 2 modes are different only if the file * already exists). * * @param bufferSize The number of bytes to buffer * * @exception java.io.IOException If an I/O error ocurred. * */ protected BufferedRandomAccessFile(File file, String mode, int bufferSize) throws IOException{ fileName = file.getName(); if(mode.equals("rw") || mode.equals("rw+")){ // mode read / write isReadOnly = false; if(mode.equals("rw")){ // mode read / (over)write if(file.exists()) // Output file already exists file.delete(); } mode = "rw"; } theFile=new RandomAccessFile(file,mode); byteBuffer=new byte[bufferSize]; readNewBuffer(0); } /** * Constructor. Uses the default value for the byte-buffer * size (512 bytes). * * @param file The file associated with the buffer * * @param mode "r" for read, "rw" or "rw+" for read and write mode * ("rw+" opens the file for update whereas "rw" removes * it before. So the 2 modes are different only if the * file already exists). * * @exception java.io.IOException If an I/O error ocurred. * */ protected BufferedRandomAccessFile(File file, String mode ) throws IOException{ this(file, mode, 512); } /** * Constructor. Always needs a size for the buffer. * * @param name The name of the file associated with the buffer * * @param mode "r" for read, "rw" or "rw+" for read and write mode * ("rw+" opens the file for update whereas "rw" removes * it before. So the 2 modes are different only if the * file already exists). * * @param bufferSize The number of bytes to buffer * * @exception java.io.IOException If an I/O error ocurred. * */ protected BufferedRandomAccessFile(String name, String mode, int bufferSize) throws IOException{ this(new File(name), mode, bufferSize); } /** * Constructor. Uses the default value for the byte-buffer * size (512 bytes). * * @param name The name of the file associated with the buffer * * @param mode "r" for read, "rw" or "rw+" for read and write mode * ("rw+" opens the file for update whereas "rw" removes * it before. So the 2 modes are different only if the * file already exists). * * @exception java.io.IOException If an I/O error ocurred. * */ protected BufferedRandomAccessFile(String name, String mode ) throws IOException{ this(name, mode, 512); } /** * Reads a new buffer from the file. If there has been any * changes made since the buffer was read, the buffer is * first written to the file. * * @param off The offset where to move to. * * @exception java.io.IOException If an I/O error ocurred. * */ protected final void readNewBuffer(int off) throws IOException{ /* If the buffer have changed. We need to write it to * the file before reading a new buffer. */ if(byteBufferChanged){ flush(); } // Don't allow to seek beyond end of file if reading only if (isReadOnly && off >= theFile.length()) { throw new EOFException(); } // Set new offset offset = off; theFile.seek(offset); maxByte = theFile.read(byteBuffer,0,byteBuffer.length); pos=0; if(maxByte<byteBuffer.length){ // Not enough data in input file. isEOFInBuffer = true; if(maxByte==-1){ maxByte++; } }else{ isEOFInBuffer = false; } } /** * Closes the buffered random access file * * @exception java.io.IOException If an I/O error ocurred. * */ public void close() throws IOException{ /* If the buffer has been changed, it need to be saved before * closing */ flush(); byteBuffer = null; // Release the byte-buffer reference theFile.close(); } /** * Returns the current offset in the file * */ public int getPos(){ return (offset+pos); } /** * Returns the current length of the stream, in bytes, taking into * account any buffering. * * @return The length of the stream, in bytes. * * @exception java.io.IOException If an I/O error ocurred. * */ public int length() throws IOException{ int len; len = (int)theFile.length(); // If the position in the buffer is not past the end of the file, // the length of theFile is the length of the stream if( (offset+maxByte)<=len ){ return(len); } else{ // If not, the file is extended due to the buffering return (offset+maxByte); } } /** * Moves the current position to the given offset at which the * next read or write occurs. The offset is measured from the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -