📄 binarybufferedfile.java
字号:
len -= copy; //was not enough stuff in buffer, do some reads... if (len > 512) {//threshold exceeded, read straight into user // buffer final int bcnt = super.read(b, off, len); firstbyteoffset += (curptr + bcnt); curptr = 0; return (numread + bcnt); } else { //refull buffer and recurse try { refillBuffer(); } catch (EOFException e) { return numread; } return (numread + read(b, off, len)); } } public int read(byte b[]) throws IOException { return read(b, 0, b.length); } /** * 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 { byte foo[] = new byte[howmany]; int gotsofar = 0; try { while (gotsofar < howmany) { int err = read(foo, gotsofar, howmany - gotsofar); if (err == -1) { if (allowless) { /* * return a smaller array, so the caller can * tell how much they really got */ byte retval[] = new byte[gotsofar]; System.arraycopy(foo, 0, retval, 0, gotsofar); return retval; } else { //some kind of failure... if (gotsofar > 0) { throw new FormatException("EOF while reading"); } else { throw new EOFException(); } } } gotsofar += err; } } catch (IOException i) { throw new FormatException("IOException reading file: " + i.getMessage()); } return foo; } /** * 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 = read(); if (retv == -1) { throw new EOFException("Error in ReadChar, EOF reached"); } return (char) retv; } catch (IOException i) { throw new FormatException("IOException in ReadChar: " + i.getMessage()); } } /** * 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 assertSize(2); curptr += 2; bytesinbuffer -= 2; return MoreMath.BuildShort(buffer, curptr - 2, MSBFirst); } /** * Reads an array of shorts. * * @param vec the array to write the shorts into * @param offset the first array index to write to * @param len the number of shorts to read * @exception EOFException there were fewer bytes than needed in * the file * @exception FormatException rethrow of IOExceptions encountered * while reading the bytes for the array */ public void readShortArray(short vec[], int offset, int len) throws EOFException, FormatException { while (len > 0) { int shortsleft = bytesinbuffer / 2; if (shortsleft == 0) { assertSize(2); //force a buffer refill - throws // exception if it can't continue; } int reallyread = (len < shortsleft) ? len : shortsleft; if (MSBFirst) { for (int i = 0; i < reallyread; i++) { vec[offset++] = MoreMath.BuildShortBE(buffer, curptr); curptr += 2; } } else { for (int i = 0; i < reallyread; i++) { vec[offset++] = MoreMath.BuildShortLE(buffer, curptr); curptr += 2; } } len -= reallyread; bytesinbuffer -= (2 * reallyread); } } /** * 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 */ public int readInteger() throws EOFException, FormatException { //MSBFirst must be set when we are called assertSize(4); curptr += 4; bytesinbuffer -= 4; return MoreMath.BuildInteger(buffer, curptr - 4, MSBFirst); } /** * Reads an array of integers. * * @exception EOFException there were fewer bytes than needed in * the file * @exception FormatException rethrow of IOExceptions encountered * while reading the bytes for the array */ public void readIntegerArray(int vec[], int offset, int len) throws EOFException, FormatException { while (len > 0) { int intsleft = bytesinbuffer / 4; if (intsleft == 0) { assertSize(4); //force a buffer refill continue; } int reallyread = (len < intsleft) ? len : intsleft; int cursor = curptr; if (MSBFirst) { for (int i = 0; i < reallyread; i++) { vec[offset++] = MoreMath.BuildIntegerBE(buffer, cursor); cursor += 4; } } else { for (int i = 0; i < reallyread; i++) { vec[offset++] = MoreMath.BuildIntegerLE(buffer, cursor); cursor += 4; } } len -= reallyread; bytesinbuffer -= (4 * reallyread); curptr = cursor; } } /** * Reads an array of floats from the input. * * @param vec the vector to read into * @param offset the first float read goes into vec[offset] * @param len the number of floats to read from the stream * @exception EOFException not enough bytes were left in the file * @exception FormatException rethrow of IOExceptions encountered * while reading the bytes for the integer */ public void readFloatArray(float vec[], int offset, int len) throws EOFException, FormatException { while (len > 0) { int floatsleft = bytesinbuffer / 4; if (floatsleft == 0) { assertSize(4); //force a buffer refill continue; } int reallyread = (len < floatsleft) ? len : floatsleft; int cursor = curptr; if (MSBFirst) { for (int i = 0; i < reallyread; i++) { int floatasint = MoreMath.BuildIntegerBE(buffer, cursor); vec[offset++] = Float.intBitsToFloat(floatasint); cursor += 4; } } else { for (int i = 0; i < reallyread; i++) { int floatasint = MoreMath.BuildIntegerLE(buffer, cursor); vec[offset++] = Float.intBitsToFloat(floatasint); cursor += 4; } } len -= reallyread; bytesinbuffer -= (4 * reallyread); curptr = cursor; } } /** * 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 { assertSize(8); curptr += 8; bytesinbuffer -= 8; return MoreMath.BuildLong(buffer, curptr - 8, MSBFirst); } /** * 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 { String retstring; if (length < buffer.length) { assertSize(length); retstring = new String(buffer, curptr, length); curptr += length; bytesinbuffer -= length; } else { byte foo[] = readBytes(length, false); retstring = new String(foo, 0, length); } return retstring; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -