⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufferedrandomaccessfile.java

📁 jpeg2000算法实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * beginning of the stream.     *      * @param off The offset where to move to.     *     * @exception EOFException If in read-only and seeking beyond EOF.     *     * @exception java.io.IOException If an I/O error ocurred.     * */    public void seek(int off) throws IOException{	/* If the new offset is within the buffer, only the pos value needs	 * to be modified. Else, the buffer must be moved. */	if( (off>=offset)&&(off<(offset+byteBuffer.length)) ){            if (isReadOnly && isEOFInBuffer && off > offset+maxByte) {                // We are seeking beyond EOF in read-only mode!                throw new EOFException();            }	    pos = off-offset;	}	else{	    readNewBuffer(off);	}    }        /**     * Reads an unsigned byte of data from the stream. Prior to reading, the     * stream is realigned at the byte level.     *      * @return The byte read.     *     * @exception java.io.IOException If an I/O error ocurred.     *     * @exception java.io.EOFException If the end of file was reached     * */    public final int read() throws IOException, EOFException{	if(pos<maxByte){ // The byte can be read from the buffer	    // In Java, the bytes are always signed.            return (byteBuffer[pos++]&0xFF);	}	else if(isEOFInBuffer){ // EOF is reached            pos = maxByte+1; // Set position to EOF	    throw new EOFException();	}	else { // End of the buffer is reached	    readNewBuffer(offset+pos);	    return read();	}    }        /**     * Reads up to len bytes of data from this file into an array of     * bytes. This method reads repeatedly from the stream until all the bytes     * are read. This method blocks until all the bytes are read, the end of     * the stream is detected, or an exception is thrown.     *     * @param b The buffer into which the data is to be read. It must be long     * enough.     *     * @param off The index in 'b' where to place the first byte read.     *     * @param len The number of bytes to read.     *     * @exception EOFException If the end-of file was reached before     * getting all the necessary data.     *     * @exception IOException If an I/O error ocurred.     * */    public final void readFully(byte b[], int off, int len)        throws IOException {        int clen; // current length to read        while (len > 0) {            // There still is some data to read            if (pos<maxByte) { // We can read some data from buffer                clen = maxByte-pos;                if (clen > len) clen = len;                System.arraycopy(byteBuffer,pos,b,off,clen);                pos += clen;                off += clen;                len -= clen;            }            else if (isEOFInBuffer) {                pos = maxByte+1; // Set position to EOF                throw new EOFException();            }            else { // Buffer empty => get more data                readNewBuffer(offset+pos);            }        }    }    /**     * Writes a byte to the stream. Prior to writing, the stream is     * realigned at the byte level.     *      * @param b The byte to write. The lower 8 bits of <tt>b</tt> are     * written.     *     * @exception java.io.IOException If an I/O error ocurred.     * */    public final void write(int b) throws IOException{	// As long as pos is less than the length of the buffer we can write	// to the buffer. If the position is after the buffer a new buffer is	// needed	if(pos<byteBuffer.length){	    if(isReadOnly)		throw new IOException("File is read only");	    byteBuffer[pos]=(byte)b;	    if(pos>=maxByte){		maxByte=pos+1;	    }	    pos++;	    byteBufferChanged =true;	}	else{	    readNewBuffer(offset+pos);	    write(b);	}    }        /**     * Writes a byte to the stream. Prior to writing, the stream is     * realigned at the byte level.     *      * @param b The byte to write.     *     * @exception java.io.IOException If an I/O error ocurred.     * */    public final void write(byte b) throws IOException{	// As long as pos is less than the length of the buffer we can write	// to the buffer. If the position is after the buffer a new buffer is	// needed	if(pos<byteBuffer.length){	    if(isReadOnly)		throw new IOException("File is read only");	    byteBuffer[pos]=b;	    if(pos>=maxByte){		maxByte=pos+1;	    }	    pos++;	    byteBufferChanged =true;	}	else{	    readNewBuffer(offset+pos);	    write(b);	}    }        /**     * Writes aan array of bytes to the stream. Prior to writing, the stream is     * realigned at the byte level.     *      * @param b The array of bytes to write.      *     * @param offset The first byte in b to write      *     * @param length The number of bytes from b to write      *     * @exception java.io.IOException If an I/O error ocurred.     * */    public final void write(byte[] b, int offset, int length)         throws IOException{        int i,stop;        stop = offset+length;        if(stop > b.length)            throw new ArrayIndexOutOfBoundsException(b.length);        for(i=offset ; i<stop ; i++){            write(b[i]);        }    }        /**     * Writes the byte value of <tt>v</tt> (i.e., 8 least     * significant bits) to the output. Prior to writing, the output     * should be realigned at the byte level.     *      * <P>Signed or unsigned data can be written. To write a signed     * value just pass the <tt>byte</tt> value as an argument. To     * write unsigned data pass the <tt>int</tt> value as an argument     * (it will be automatically casted, and only the 8 least     * significant bits will be written).     *      * @param v The value to write to the output     *     * @exception java.io.IOException If an I/O error ocurred.     * */    public final void writeByte(int v) throws IOException{	write(v);    }        /**     * Any data that has been buffered must be written (including     * buffering at the bit level), and the stream should be realigned     * at the byte level.     *      * @exception java.io.IOException If an I/O error ocurred.     * */    public final void flush() throws IOException{        if(byteBufferChanged){	    theFile.seek(offset);	    theFile.write(byteBuffer,0,maxByte);	    byteBufferChanged = false;        }    }        /**     * Reads a signed byte (i.e., 8 bit) from the input. Prior to      * reading, the input should be realigned at the byte level.     *      * @return The next byte-aligned signed byte (8 bit) from the     * input.     *     * @exception java.io.EOFException If the end-of file was reached before     * getting all the necessary data.     *     * @exception java.io.IOException If an I/O error ocurred.     * */    public final byte readByte() throws EOFException, IOException {	if(pos<maxByte){ // The byte can be read from the buffer	    // In Java, the bytes are always signed.            return byteBuffer[pos++];	}	else if(isEOFInBuffer){ // EOF is reached            pos = maxByte+1; // Set position to EOF	    throw new EOFException();	}	else { // End of the buffer is reached	    readNewBuffer(offset+pos);	    return readByte();	}    }        /**     * Reads an unsigned byte (i.e., 8 bit) from the input. It is     * returned as an <tt>int</tt> since Java does not have an     * unsigned byte type. Prior to reading, the input should be     * realigned at the byte level.     *      * @return The next byte-aligned unsigned byte (8 bit) from the     * input, as an <tt>int</tt>.     *     * @exception java.io.EOFException If the end-of file was reached before     * getting all the necessary data.     *     * @exception java.io.IOException If an I/O error ocurred.     * */    public final int readUnsignedByte() throws EOFException, IOException{        return read();    }        /**     * Returns the endianess (i.e., byte ordering) of the implementing     * class. Note that an implementing class may implement only one     * type of endianness or both, which would be decided at creation     * time.     *      * @return Either <tt>EndianType.BIG_ENDIAN</tt> or     * <tt>EndianType.LITTLE_ENDIAN</tt>     *     * @see EndianType     * */    public int getByteOrdering(){	return byteOrdering;    }        /**     * Skips <tt>n</tt> bytes from the input. Prior to skipping, the     * input should be realigned at the byte level.     *      * @param n The number of bytes to skip     *     * @exception java.io.EOFException If the end-of file was reached before     * all the bytes could be skipped.     *     * @exception java.io.IOException If an I/O error ocurred.     * */    public int skipBytes(int n)throws EOFException, IOException{	if(n<0)	    throw new IllegalArgumentException("Can not skip negative number "+					       "of bytes");	if(n <= (maxByte-pos)){	    pos += n;	    return n;	}	else{	    seek(offset+pos+n);	    return n;	}    }        /**     * Returns a string of information about the file     * */    public String toString(){	return "BufferedRandomAccessFile: "+fileName+" ("+	    ((isReadOnly)?"read only":"read/write")+	    ")";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -