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

📄 binarybufferedfile.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// **********************************************************************// // <copyright>// //  BBN Technologies//  10 Moulton Street//  Cambridge, MA 02138//  (617) 873-8000// //  Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/io/BinaryBufferedFile.java,v $// $RCSfile: BinaryBufferedFile.java,v $// $Revision: 1.2.2.1 $// $Date: 2004/10/14 18:27:00 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.io;import com.bbn.openmap.MoreMath;import java.io.EOFException;import java.io.File;import java.io.IOException;/** * This class extends the BinaryFile class, doing buffered reads on * the underlying input file. The buffer size is not modifiable after * construction, and the buffer management isn't the greatest. */public class BinaryBufferedFile extends BinaryFile {    /** Where reads get buffered */    private byte buffer[];    /** Where current reads will come from */    private int curptr = 0;    /** how many valid bytes are in the buffer */    private int bytesinbuffer = 0;    /** the byte offset of the first byte in the buffer */    private long firstbyteoffset = 0;    /**     * Constructs a BinaryBufferedFile with a File and a buffer size     *      * @param f the input file     * @param buffersize the size to use for buffering reads     * @exception IOException pass-through errors from opening a     *            BinaryFile with f     * @see com.bbn.openmap.io.BinaryFile     */    public BinaryBufferedFile(File f, int buffersize) throws IOException {        super(f);        buffer = new byte[buffersize];    }    /**     * Constructs a BinaryBufferedFile with a File     *      * @param f the input file     * @exception IOException pass-through errors from opening a     *            BinaryFile with f     * @see com.bbn.openmap.io.BinaryFile     */    public BinaryBufferedFile(File f) throws IOException {        this(f, 4096);    }    /**     * Constructs a BinaryBufferedFile with a filename and a     * buffersize     *      * @param name the name/path of the input file     * @param buffersize the size to use for buffering reads     * @exception IOException pass-through errors from opening a     *            BinaryFile with f     * @see com.bbn.openmap.io.BinaryFile     */    public BinaryBufferedFile(String name, int buffersize) throws IOException {        super(name);        buffer = new byte[buffersize];    }    /**     * Constructs a BinaryBufferedFile with a filename     *      * @param name the name/path of the input file     * @exception IOException pass-through errors from opening a     *            BinaryFile with f     * @see com.bbn.openmap.io.BinaryFile     */    public BinaryBufferedFile(String name) throws IOException {        this(name, 4096);    }    /**     * A simple factory method that lets you try to create something     * without having to really deal with failure. Returns a     * BinaryFile if successful, null if not.     */    public static BinaryBufferedFile create(String name) {        return create(name, 4096);    }    /**     * A simple factory method that lets you try to create something     * without having to really deal with failure. Returns a     * BinaryFile if successful, null if not.     */    public static BinaryBufferedFile create(String name, int buffersize) {        BinaryBufferedFile bf = null;        try {            bf = new BinaryBufferedFile(name, buffersize);        } catch (IOException ioe) {        }        return bf;    }    /**     * Set the input reader used by the BinaryFile. Make sure it's     * intialized properly. Assumes that the pointer is at the     * beginning of the file.     */    public void setInputReader(InputReader reader) {        super.setInputReader(reader);        firstbyteoffset = 0;        bytesinbuffer = 0;        curptr = 0;    }    /**     * Throws away whatever data is in the buffer, and refills it.     *      * @exception IOException IO errors encountered while refilling     *            the buffer     * @exception EOFException no data was left in the file     */    private void refillBuffer() throws IOException, EOFException {        firstbyteoffset += (curptr + bytesinbuffer);        int err = super.read(buffer, 0, buffer.length);        curptr = 0;        if (err == -1)            throw new EOFException();        bytesinbuffer = err;    }    /**     * Forces the buffer to have at least some minimum number of bytes     * in it.     *      * @param minlength the minimum number of bytes to have in the     *        buffer     * @exception FormatException couldn't get enough bytes (or IO     *            Exception)     * @exception EOFException couldn't get any bytes     */    private void assertSize(int minlength) throws FormatException, EOFException {        try {            if (bytesinbuffer < minlength) {                if (curptr != 0) {                    firstbyteoffset += curptr;                    System.arraycopy(buffer, curptr, buffer, 0, bytesinbuffer);                    curptr = 0;                }                int err = super.read(buffer, bytesinbuffer, buffer.length                        - bytesinbuffer);                if (err == -1) {                    if (available() <= 0) {                        throw new EOFException("BinaryBufferedFile, no bytes at all, trying to read "                                + minlength);                    } else {                        throw new FormatException("BinaryBufferedFile: failed to read "                                + minlength                                + " bytes, with "                                + bytesinbuffer                                + " bytes in the buffer and "                                + available()                                + " bytes available, have read "                                + curptr                                + " bytes.");                    }                }                bytesinbuffer += err;                assertSize(minlength);            }        } catch (EOFException e) {            throw e;        } catch (IOException i) {            throw new FormatException("assertSize IOException: "                    + i.getMessage());        }    }    public long skipBytes(long n) throws IOException {        if (n < bytesinbuffer) {            bytesinbuffer -= n;            curptr += n;            return n;        }        final long oldbinb = bytesinbuffer;        bytesinbuffer = 0;        curptr = 0;        final int skipcnt = (int) super.skipBytes(n - oldbinb);        firstbyteoffset += skipcnt;        return (oldbinb + skipcnt);    }    public long getFilePointer() throws IOException {        return (firstbyteoffset + curptr);    }    public void seek(long pos) throws IOException {        final long relpos = pos - firstbyteoffset;        if ((relpos >= 0) && (relpos < (curptr + bytesinbuffer))) {            final int relcur = (int) relpos - curptr;            if (relcur != 0) {                bytesinbuffer -= relcur;                curptr = (int) relpos;            } //else we're already at the right place        } else {            super.seek(pos);            firstbyteoffset = pos;            bytesinbuffer = 0;            curptr = 0;        }    }    public long length() throws IOException {        return super.length();    }    public long available() throws IOException {        return (length() - firstbyteoffset - curptr);    }    /**     * closes the underlying file input, and releases some resources     * of the class. Calling any other members after this one will     * return bogus results.     *      * @exception IOException IO errors envountered in closing the     *            file     */    public void close() throws IOException {        buffer = null;        super.close();    }    public int read() throws IOException {        try {            if (bytesinbuffer == 0)                refillBuffer();        } catch (EOFException e) {            return -1;        }        bytesinbuffer--;        return MoreMath.signedToInt(buffer[curptr++]);    }    /**     * Read from the file     *      * @param b The byte array to read into     * @param off the first array position to read into     * @param len the number of bytes to read     * @return the number of bytes read     * @exception IOException Any IO errors encountered in reading     *            from the file     */    public int read(byte b[], int off, int len) throws IOException {        int numread = 0;        int copy;        if (len < bytesinbuffer)            copy = len;        else            copy = bytesinbuffer;        numread += copy;        bytesinbuffer -= copy;        System.arraycopy(buffer, curptr, b, off, copy);        curptr += copy;        off += copy;        if (len == copy)            return numread;

⌨️ 快捷键说明

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