Package uk.ac.starlink.oldfits
Class MappedFile
java.lang.Object
uk.ac.starlink.oldfits.AbstractArrayDataIO
uk.ac.starlink.oldfits.MappedFile
- All Implemented Interfaces:
Closeable
,DataInput
,DataOutput
,AutoCloseable
,nom.tam.util.ArrayDataInput
,nom.tam.util.ArrayDataOutput
,nom.tam.util.FitsIO
,nom.tam.util.InputReader
,nom.tam.util.OutputWriter
,nom.tam.util.RandomAccess
,nom.tam.util.ReadWriteAccess
Provides mapped access to a data buffer using a single mapped byte buffer,
compatible with nom.tam.util classes.
This class implements the nom.tam.util ArrayDataInput, ArrayDataOutput and RandomAccess interfaces in the same way that nom.tam.util.BufferedFile does. Hence it can be used as a drop-in replacement for BufferedFile. Unlike BufferedFile however, it does mapped access to files (using java.nio.Buffer objects). This may be moderately more efficient for sequential access to a file, but is dramatically more efficient if very random access is required. This is because BufferedFile effectively always assumes that you are going to read sequentially, so that accessing a single datum distant from (or before) the last datum accessed always results in filling a whole buffer.
Limitations:
- Files larger than Integer.MAX_VALUE bytes may not currently be accessed
- Access to very large files may fail if virtual memory runs out
- Since:
- 30 Aug 2002
- Author:
- Mark Taylor (Starlink)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Exception indicating that a file is too long to map. -
Field Summary
Fields inherited from interface nom.tam.util.FitsIO
BITS_OF_1_BYTE, BITS_OF_2_BYTES, BITS_OF_3_BYTES, BITS_OF_4_BYTES, BITS_OF_5_BYTES, BITS_OF_6_BYTES, BITS_OF_7_BYTES, BYTE_1_OF_LONG_MASK, BYTE_2_OF_LONG_MASK, BYTE_3_OF_LONG_MASK, BYTE_4_OF_LONG_MASK, BYTE_MASK, BYTES_IN_BOOLEAN, BYTES_IN_BYTE, BYTES_IN_CHAR, BYTES_IN_DOUBLE, BYTES_IN_FLOAT, BYTES_IN_INTEGER, BYTES_IN_LONG, BYTES_IN_SHORT, DEFAULT_BUFFER_SIZE, HIGH_INTEGER_MASK, INTEGER_MASK, SHORT_MASK, SHORT_OF_LONG_MASK
-
Constructor Summary
ConstructorsConstructorDescriptionMappedFile
(String filename) Constructs a MappedFile object by mapping the whole of an existing file using read-only mode.MappedFile
(String filename, String mode) Constructs a MappedFile object by mapping the whole of an existing file with a given mode.MappedFile
(String filename, String mode, long start, int size) Constructs a MappedFile object by mapping part of an existing file with a given mode.MappedFile
(ByteBuffer buf) Constructs a MappedFile object from a byte buffer. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
flush()
protected byte
get()
Reads one byte from the current position.protected void
get
(byte[] buf, int offset, int length) Reads bytes into a buffer from the current position.long
long
length()
Returns the size of this buffer.void
mark
(int readlimit) boolean
protected void
put
(byte b) Writes a single byte at the current position.protected void
put
(byte[] buf, int offset, int length) Writes bytes from a buffer to the current position.protected long
Returns the number of bytes remaining between the current position and the end of the file.void
reset()
void
seek
(long offsetFromStart) long
skip
(long offset) void
skipAllBytes
(int toSkip) void
skipAllBytes
(long toSkip) int
skipBytes
(int toSkip) Methods inherited from class uk.ac.starlink.oldfits.AbstractArrayDataIO
read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, readArray, readBoolean, readByte, readChar, readDouble, readFloat, readFully, readFully, readInt, readLArray, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, writeArray, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface nom.tam.util.ArrayDataInput
read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, read, readArray, readArrayFully, readFully, readImage, readLArray
Methods inherited from interface nom.tam.util.ArrayDataOutput
write, write
Methods inherited from interface java.io.DataInput
readBoolean, readByte, readChar, readDouble, readFloat, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF
Methods inherited from interface nom.tam.util.InputReader
read, read
Methods inherited from interface nom.tam.util.OutputWriter
write, write
Methods inherited from interface nom.tam.util.RandomAccess
position, position
-
Constructor Details
-
MappedFile
Constructs a MappedFile object from a byte buffer.- Parameters:
buf
- byte buffer
-
MappedFile
Constructs a MappedFile object by mapping the whole of an existing file using read-only mode.- Parameters:
filename
- name of the file to map- Throws:
MappedFile.FileTooLongException
- if the file is too long to mapIOException
-
MappedFile
Constructs a MappedFile object by mapping the whole of an existing file with a given mode.- Parameters:
filename
- name of the file to mapmode
- mode- Throws:
MappedFile.FileTooLongException
- if the file is too long to mapIOException
-
MappedFile
Constructs a MappedFile object by mapping part of an existing file with a given mode.- Parameters:
filename
- name of the file to mapmode
- modestart
- offset of region to mapsize
- length of region to map- Throws:
IOException
-
-
Method Details
-
seek
- Specified by:
seek
in interfacenom.tam.util.RandomAccess
- Throws:
IOException
-
skip
public long skip(long offset) - Specified by:
skip
in interfacenom.tam.util.ArrayDataInput
-
getFilePointer
public long getFilePointer()- Specified by:
getFilePointer
in interfacenom.tam.util.RandomAccess
-
skipBytes
public int skipBytes(int toSkip) -
skipAllBytes
- Specified by:
skipAllBytes
in interfacenom.tam.util.ArrayDataInput
- Throws:
IOException
-
skipAllBytes
- Specified by:
skipAllBytes
in interfacenom.tam.util.ArrayDataInput
- Throws:
IOException
-
markSupported
public boolean markSupported()- Specified by:
markSupported
in interfacenom.tam.util.ArrayDataInput
-
mark
public void mark(int readlimit) - Specified by:
mark
in interfacenom.tam.util.ArrayDataInput
-
reset
- Specified by:
reset
in interfacenom.tam.util.ArrayDataInput
- Throws:
IOException
-
get
Description copied from class:AbstractArrayDataIO
Reads one byte from the current position.- Specified by:
get
in classAbstractArrayDataIO
- Returns:
- next byte
- Throws:
IOException
-
get
Description copied from class:AbstractArrayDataIO
Reads bytes into a buffer from the current position.- Specified by:
get
in classAbstractArrayDataIO
- Parameters:
buf
- destination bufferoffset
- offset of first byte inbuf
to be writtenlength
- maximum number of bytes to be written tobuf
- Throws:
IOException
-
put
Description copied from class:AbstractArrayDataIO
Writes a single byte at the current position.- Specified by:
put
in classAbstractArrayDataIO
- Parameters:
b
- output byte- Throws:
IOException
-
put
Description copied from class:AbstractArrayDataIO
Writes bytes from a buffer to the current position.- Specified by:
put
in classAbstractArrayDataIO
- Parameters:
buf
- source bufferoffset
- offset of first byte inbuf
to be readlength
- number of bytes frombuf
to be read- Throws:
IOException
-
length
public long length()Description copied from class:AbstractArrayDataIO
Returns the size of this buffer. May be -1 if not known/unlimited.- Specified by:
length
in interfacenom.tam.util.ReadWriteAccess
- Specified by:
length
in classAbstractArrayDataIO
- Returns:
- length or -1
-
remaining
protected long remaining()Description copied from class:AbstractArrayDataIO
Returns the number of bytes remaining between the current position and the end of the file. If there is no end to the file, it is permissible to returnLong.MAX_VALUE
;- Specified by:
remaining
in classAbstractArrayDataIO
- Returns:
- number of bytes left in file
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfacenom.tam.util.FitsIO
-
flush
public void flush()- Specified by:
flush
in interfacenom.tam.util.ArrayDataOutput
-