📄 binaryfile.java
字号:
return inputReader.skipBytes(n); } /** * Get the index of the next character to be read * * @return the index * @exception IOException Any IO errors that occur in accessing the * underlying file */ public long getFilePointer() throws IOException { return inputReader.getFilePointer(); } /** * Set the index of the next character to be read. * * @param pos the position to seek to. * @exception IOException Any IO Errors that occur in seeking the underlying * file. */ public void seek(long pos) throws IOException { inputReader.seek(pos); } /** * The length of the InputReader source. */ public long length() throws IOException { return inputReader.length(); } /** * Return how many bytes left to be read in the file. * * @return the number of bytes remaining to be read (counted in bytes) * @exception IOException Any IO errors encountered in accessing the file */ public long available() throws IOException { return inputReader.available(); } /** * Closes the underlying file, but with a chance for re-opening if accessed * again. * * @exception IOException Any IO errors encountered in accessing the file */ public void close() throws IOException { if (inputReader != null) { inputReader.close(); openCount--; } } /** * Closes underlying file, get rid of resources and knowledge of file. To be * called when you don't need the file any more. * * @throws IOException */ public void dispose() throws IOException { close(); inputReader = null; } /** * Read from the file. * * @return one byte from the file. -1 for EOF * @exception IOException Any IO errors encountered in reading from the file */ public int read() throws IOException { return inputReader.read(); } /** * 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 { return inputReader.read(b, off, len); } /** * Read from the file. * * @param b the byte array to read into. Equivelent to * <code>read(b, 0, b.length)</code> * @return the number of bytes read * @exception IOException Any IO errors encountered in reading from the file * @see java.io.RandomAccessFile#read(byte[]) */ public int read(byte b[]) throws IOException { return inputReader.read(b); } /** * Read from the file. * * @param howmany the number of bytes to read * @param allowless if we can return fewer bytes than requested * @return the array of bytes read. * @exception FormatException Any IO Exceptions, plus an end-of-file * encountered after reading some, but now enough, bytes when * allowless was <code>false</code> * @exception EOFException Encountered an end-of-file while allowless was * <code>false</code>, but NO bytes had been read. */ public byte[] readBytes(int howmany, boolean allowless) throws EOFException, FormatException { return inputReader.readBytes(howmany, allowless); } /** * Reads and returns a single byte, cast to a char * * @return the byte read from the file, cast to a char * @exception EOFException the end-of-file has been reached, so no chars * where available * @exception FormatException a rethrown IOException */ public char readChar() throws EOFException, FormatException { try { int retv = inputReader.read(); if (retv == -1) { throw new EOFException("Error in ReadChar, EOF reached"); } return (char) retv; } catch (IOException i) { throw new FormatException("readChar IOException: " + i.getMessage()); } } /** * Read a byte from the file, return an unsigned integer. * * @return one byte from the file. -1 for EOF causes EOFException * @exception IOException Any IO errors encountered in reading from the file */ public int readUnsigned() throws IOException, EOFException { byte b = (byte) read(); if (b == -1) { throw new EOFException(); } return MoreMath.signedToInt(b); } /** * Reads and returns a short. * * @return the 2 bytes merged into a short, according to the current byte * ordering * @exception EOFException there were less than 2 bytes left in the file * @exception FormatException rethrow of IOExceptions encountered while * reading the bytes for the short * @see #read(byte[]) */ public short readShort() throws EOFException, FormatException { // MSBFirst must be set when we are called return MoreMath.BuildShort(readBytes(2, false), MSBFirst); } /** * Code for reading shorts that are two-byte integers, high order first, and * negatives are signed magnitude. Users may have to switch the bytes and * convert negatives to the complement they use. This can be done by putting * the low order byte first, then turning off bit 15 (the high order bit), * and then multiplying by -1." Basically they are encoded as positive * numbers, but bit 15 is set to 1. * * @return * @throws EOFException * @throws FormatException */ public short readShortData() throws EOFException, FormatException { // read in the two bytes byte[] bytevec = readBytes(2, false); // check for negative values - bit 7 of byte 0 if (bytevec[0] < 0) { // mask bit 7 bytevec[0] &= 0x7f; // create the short and multiply the result by -1 return ((short) (MoreMath.BuildShort(bytevec, true) * -1)); } return MoreMath.BuildShort(bytevec, true); } /** * Reads and returns a integer from 2 bytes. * * @return the 2 bytes merged into a short, according to the current byte * ordering, and then unsigned to int. * @exception EOFException there were less than 2 bytes left in the file * @exception FormatException rethrow of IOExceptions encountered while * reading the bytes for the short * @see #read(byte[]) */ public int readUnsignedShort() throws EOFException, FormatException { // MSBFirst must be set when we are called return MoreMath.signedToInt(readShort()); } /** * Reads and returns a long * * @return the 4 bytes merged into a long, according to the current byte * ordering * @exception EOFException there were less than 4 bytes left in the file * @exception FormatException rethrow of IOExceptions encountered while * reading the bytes for the integer * @see #read(byte[]) */ public int readInteger() throws EOFException, FormatException { // MSBFirst must be set when we are called return MoreMath.BuildInteger(readBytes(4, false), MSBFirst); } public void readIntegerArray(int vec[], int offset, int len) throws EOFException, FormatException { for (int i = 0; i < len; i++) { vec[offset++] = readInteger(); } } /** * Reads and returns a long * * @return the 8 bytes merged into a long, according to the current byte * ordering * @exception EOFException there were less than 8 bytes left in the file * @exception FormatException rethrow of IOExceptions encountered while * reading the bytes for the long * @see #read(byte[]) */ public long readLong() throws EOFException, FormatException { return MoreMath.BuildLong(readBytes(8, false), MSBFirst); } /** * Reads and returns a float * * @return the 4 bytes merged into a float, according to the current byte * ordering * @exception EOFException there were less than 4 bytes left in the file * @exception FormatException rethrow of IOExceptions encountered while * reading the bytes for the float * @see #read(byte[]) */ public float readFloat() throws EOFException, FormatException { return Float.intBitsToFloat(readInteger()); } public void readFloatArray(float vec[], int offset, int len) throws EOFException, FormatException { for (int i = 0; i < len; i++) { vec[offset++] = readFloat(); } } /** * Reads and returns a double * * @return the 8 bytes merged into a double, according to the current byte * ordering * @exception EOFException there were less than 8 bytes left in the file * @exception FormatException rethrow of IOExceptions encountered while * reading the bytes for the short * @see #read(byte[]) */ public double readDouble() throws EOFException, FormatException { return Double.longBitsToDouble(readLong()); } /** * Reads <code>length</code> bytes and returns a string composed of the * bytes cast to chars * * @param length the number of bytes to read into the string * @return the composed string * @exception EOFException there were less than <code>length</code> bytes * left in the file * @exception FormatException rethrow of IOExceptions encountered while * reading the bytes for the short */ public String readFixedLengthString(int length) throws EOFException, FormatException { byte foo[] = readBytes(length, false); return new String(foo, 0, length); } /** * Read a bytes and throw an InvalidCharException if it doesn't match * <code>expected</code> * * @param expected what the next char is claimed to be * @exception EOFException there wasn't a byte, so we can't check for a * match * @exception InvalidCharException throws when the character read doesn't * match <code>expected</code> The .c member of the thrown * exception is the actual char read * @exception FormatException some other error from reading the file */ public void assertChar(char expected) throws EOFException, FormatException { char c = readChar(); if (c != expected) { throw new InvalidCharException("AssertChar: expected " + expected + " got " + c, c); } } /** * Reads a string until the specified delimiter or EOF is encountered * * @param delim the end-of-string delimiter * @return the string that was read * @exception FormatException rethrow of IOExceptions from the read methods */ public String readToDelimiter(char delim) throws FormatException { StringBuffer buildretval = new StringBuffer(); char tmp; try { while ((tmp = readChar()) != delim) buildretval.append(tmp); } catch (EOFException e) { // allowable } catch (FormatException fe) { if (buildretval.length() == 0) { throw fe; } } return buildretval.toString(); } /** * Makes sure that the file has been closed. * * @exception Throwable what it throws. */ public void finalize() throws Throwable { close(); classCount--; } /** * Maintains a list of objects that can be closed so that other files can be * opened. */ private static Vector closableList = new Vector(); /** * Add an object that can be closed if needed. Duplicates are allowed. Only * holds a WeakReference, so that the object can still be garbage-collected. * * @param it the object that can be closed */ public static synchronized void addClosable(Closable it) { closableList.addElement(new WeakReference(it)); } /** * Remove an object from the closable list. * * @param it the object to remove */ public static synchronized void removeClosable(Closable it) { for (int i = 0; i < closableList.size(); i++) { Object o = ((WeakReference) closableList.elementAt(i)).get(); if ((o == it) || (o == null)) { closableList.removeElementAt(i); i--; // in case its in the list more than once } } } public static synchronized void closeClosable() { System.out.println("closeClosable " + closableList.size()); for (int i = 0; i < closableList.size(); i++) { Closable c = (Closable) ((WeakReference) closableList.elementAt(i)).get(); if ((c == null) || !c.close(false)) { closableList.removeElementAt(i); i--; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -