📄 lerandomaccessfile.java
字号:
package com.mindprod.ledatastream;
import java.io.*;
/**
* LERandomAccessFile.java copyright (c) 1998-2008 Roedy Green, Canadian Mind Products
*
* @noinspection WeakerAccess
*/
public final class LERandomAccessFile implements DataInput, DataOutput
{
// ------------------------------ FIELDS ------------------------------
/**
* undisplayed copyright notice.
*
* @noinspection UnusedDeclaration
*/
private static final String EMBEDDED_COPYRIGHT =
"copyright (c) 1999-2008 Roedy Green, Canadian Mind Products, http://mindprod.com";
/**
* to get at the big-endian methods of RandomAccessFile .
*
* @noinspection WeakerAccess
*/
protected RandomAccessFile raf;
/**
* work array for buffering input/output.
*
* @noinspection WeakerAccess
*/
protected byte work[];
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* constructor.
*
* @param file file to read/write.
* @param rw like {@link java.io.RandomAccessFile} where "r" for read "rw" for read and write, "rws" for
* read-write sync, and "rwd" for read-write dsync. Sync ensures the physical i/o has completed befor
* the method returns.
* @throws java.io.FileNotFoundException if open fails.
*/
public LERandomAccessFile( File file,
String rw ) throws FileNotFoundException
{
raf = new RandomAccessFile( file, rw );
work = new byte[8];
}
/**
* constructors.
*
* @param file name of file.
* @param rw string "r" or "rw" depending on read or read/write.
* @throws java.io.FileNotFoundException if open fails.
* @noinspection SameParameterValue
*/
public LERandomAccessFile( String file,
String rw ) throws FileNotFoundException
{
raf = new RandomAccessFile( file, rw );
work = new byte[8];
}
/**
* close the file.
*
* @throws IOException if close fails.
*/
public final void close() throws IOException
{
raf.close();
}
/**
* Get file descriptor.
*
* @return file descriptor (handle to open file)
* @throws IOException if get fails.
*/
public final FileDescriptor getFD() throws IOException
{
return raf.getFD();
}
/**
* get position of marker in the file.
*
* @return offset where we are in the file.
* @throws IOException if get fails.
*/
public final long getFilePointer() throws IOException
{
return raf.getFilePointer();
}
/**
* get length of the file.
*
* @return length in bytes, note value is a long.
* @throws IOException if get fails.
*/
public final long length() throws IOException
{
return raf.length();
}
/**
* ready one unsigned byte.
*
* @return unsigned byte read.
* @throws IOException if read fails.
*/
public final int read() throws IOException
{
return raf.read();
}
/**
* read an array of bytes.
*
* @param ba byte array to accept the bytes.
* @return how many bytes actually read.
* @throws IOException if read fails.
*/
public final int read( byte ba[] ) throws IOException
{
return raf.read( ba );
}
/**
* Read a byte array.
*
* @param ba byte array to accept teh bytes.
* @param off offset into the array to place the bytes, <b>not</b> offset in file.
* @param len how many bytes to read.
* @return how many bytes actually read.
* @throws IOException if read fails.
*/
public final int read( byte ba[], int off, int len ) throws IOException
{
return raf.read( ba, off, len );
}
/**
* OK, reads only only 1 byte boolean.
*
* @return true or false.
* @throws IOException if read fails.
*/
public final boolean readBoolean() throws IOException
{
return raf.readBoolean();
}
/**
* read byte.
*
* @return byte read.
* @throws IOException if read fails.
*/
public final byte readByte() throws IOException
{
return raf.readByte();
}
/**
* Read a char. like RandomAcessFile.readChar except little endian.
*
* @return char read.
* @throws IOException if read fails.
*/
public final char readChar() throws IOException
{
raf.readFully( work, 0, 2 );
return ( char ) ( ( work[ 1 ] & 0xff ) << 8 | ( work[ 0 ] & 0xff ) );
}
/**
* read a double. like RandomAcessFile.readDouble except little endian.
*
* @return the double read.
* @throws IOException if read fails.
*/
public final double readDouble() throws IOException
{
return Double.longBitsToDouble( readLong() );
}
/**
* read a float. like RandomAcessFile.readFloat except little endian.
*
* @return float read.
* @throws IOException if read fails.
*/
public final float readFloat() throws IOException
{
return Float.intBitsToFloat( readInt() );
}
/**
* Read a full array.
*
* @param ba the array to hold the results.
* @throws IOException if read fails.
*/
public final void readFully( byte ba[] ) throws IOException
{
raf.readFully( ba, 0, ba.length );
}
/**
* read an array of bytes until the count is satisfied.
*
* @param ba the array to hold the results.
* @param off offset.
* @param len count of bytes to read.
* @throws IOException if read fails.
*/
public final void readFully( byte ba[],
int off,
int len ) throws IOException
{
raf.readFully( ba, off, len );
}
/**
* read signed little endian 32-bit int.
*
* @return signed int
* @throws IOException if read fails.
* @see java.io.RandomAccessFile#readInt except little endian.
*/
public final int readInt() throws IOException
{
raf.readFully( work, 0, 4 );
return ( work[ 3 ] ) << 24
| ( work[ 2 ] & 0xff ) << 16
| ( work[ 1 ] & 0xff ) << 8
| ( work[ 0 ] & 0xff );
}
/**
* Read a line.
*
* @return line read.
* @throws IOException if read fails.
*/
public final String readLine() throws IOException
{
return raf.readLine();
}
/**
* Read a long, 64 bits.
*
* @return long read. like RandomAcessFile.readLong except little endian.
* @throws IOException if read fails.
*/
public final long readLong() throws IOException
{
raf.readFully( work, 0, 8 );
return ( long ) ( work[ 7 ] ) << 56
|
/* long cast necessary or shift done modulo 32 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -