Package de.bsvrz.dav.daf.util
Class FileAccess
java.lang.Object
de.bsvrz.dav.daf.util.FileAccess
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataInput
,java.io.DataOutput
,java.lang.AutoCloseable
,java.nio.channels.ByteChannel
,java.nio.channels.Channel
,java.nio.channels.ReadableByteChannel
,java.nio.channels.SeekableByteChannel
,java.nio.channels.WritableByteChannel
- Direct Known Subclasses:
BufferedRandomAccessFile
,CloseableRandomAccessFile
public abstract class FileAccess
extends java.lang.Object
implements java.io.DataInput, java.io.DataOutput, java.nio.channels.SeekableByteChannel
Abstrakte Basisklasse für Klassen, die RandomAccessFile-ähnliche Funktionalität bieten
-
Field Summary
Fields Modifier and Type Field Description protected int
_bufferSize
Größe des Lese und Schreibpuffersprotected java.io.DataInputStream
_dataInStream
Gepufferter EingabeStream, wird bei Bedarf initialisiert und gelöschtprotected java.io.DataOutputStream
_dataOutStream
Gepufferter AusgabeStream, wird bei Bedarf initialisiert und gelöschtprotected long
_position
Aktuelle Dateiposition aus Anwendersicht, muss hier gemerkt und selbst berechnet werden, weil die Position des FileChannels durch die Pufferung beim Lesen und Schreiben nicht notwendigerweise der aktuellen logischen Position entsprichtprotected static int
defaultBufferSize
Standardpuffergröße -
Constructor Summary
Constructors Constructor Description FileAccess(int bufferSize)
-
Method Summary
Modifier and Type Method Description void
close()
void
flush()
Schreibt den Schreibpuffer auf die Festplatteprotected void
flushInStream()
protected void
flushOutStream()
protected abstract java.nio.channels.FileChannel
getChannel()
Gibt einen gültigen FileChannel zurück, mit dem die Klasse die Datei manipulieren kann.protected abstract java.io.DataInputStream
getDataInStream()
Gibt einen DataInputStream zum Lesen zurückprotected abstract java.io.DataOutputStream
getDataOutStream()
Gibt einen DataOutputStream zum Schreiben zurücklong
getFilePointer()
Für RandomAccessFile-Kompatibilitätabstract boolean
isOpen()
long
length()
Für RandomAccessFile-Kompatibilitätlong
position()
FileAccess
position(long newPosition)
int
read()
Deprecated.int
read(byte[] b)
Methode analog zuRandomAccessFile.read(byte[])
.int
read(byte[] b, int off, int len)
Methode analog zuRandomAccessFile.read(byte[], int, int)
.int
read(java.nio.ByteBuffer dst)
boolean
readBoolean()
byte
readByte()
char
readChar()
double
readDouble()
float
readFloat()
void
readFully(byte[] b)
void
readFully(byte[] b, int off, int len)
int
readInt()
java.lang.String
readLine()
Deprecated.long
readLong()
short
readShort()
int
readUnsignedByte()
int
readUnsignedShort()
java.lang.String
readUTF()
void
seek(long position)
Für RandomAccessFile-Kompatibilitätvoid
setLength(long len)
Für RandomAccessFile-Kompatibilitätlong
size()
Gibt die Dateilänge zurücklong
skip(long n)
Überspringt genau n Bytes.int
skipBytes(int n)
Überspringt n genau Bytes.java.lang.String
toString()
FileAccess
truncate(long size)
void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
int
write(java.nio.ByteBuffer src)
void
writeBoolean(boolean v)
void
writeByte(int v)
void
writeBytes(java.lang.String s)
void
writeChar(int v)
void
writeChars(java.lang.String s)
void
writeDouble(double v)
void
writeFloat(float v)
void
writeInt(int v)
void
writeLong(long v)
void
writeShort(int v)
void
writeUTF(java.lang.String s)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
defaultBufferSize
protected static final int defaultBufferSizeStandardpuffergröße- See Also:
- Constant Field Values
-
_bufferSize
protected final int _bufferSizeGröße des Lese und Schreibpuffers -
_dataInStream
protected java.io.DataInputStream _dataInStreamGepufferter EingabeStream, wird bei Bedarf initialisiert und gelöscht -
_dataOutStream
protected java.io.DataOutputStream _dataOutStreamGepufferter AusgabeStream, wird bei Bedarf initialisiert und gelöscht -
_position
protected long _positionAktuelle Dateiposition aus Anwendersicht, muss hier gemerkt und selbst berechnet werden, weil die Position des FileChannels durch die Pufferung beim Lesen und Schreiben nicht notwendigerweise der aktuellen logischen Position entspricht
-
-
Constructor Details
-
FileAccess
public FileAccess(int bufferSize)
-
-
Method Details
-
flushInStream
protected void flushInStream() -
flushOutStream
protected void flushOutStream() throws java.io.IOException- Throws:
java.io.IOException
-
getDataOutStream
protected abstract java.io.DataOutputStream getDataOutStream() throws java.io.IOExceptionGibt einen DataOutputStream zum Schreiben zurück- Returns:
- DataOutputStream
- Throws:
java.io.IOException
-
getDataInStream
protected abstract java.io.DataInputStream getDataInStream() throws java.io.IOExceptionGibt einen DataInputStream zum Lesen zurück- Returns:
- DataInputStream
- Throws:
java.io.IOException
-
getChannel
protected abstract java.nio.channels.FileChannel getChannel() throws java.io.IOExceptionGibt einen gültigen FileChannel zurück, mit dem die Klasse die Datei manipulieren kann.- Returns:
- einen gültigen FileChannel
- Throws:
java.io.IOException
-
isOpen
public abstract boolean isOpen()- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
close
public void close() throws java.io.IOException- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b) throws java.io.IOException- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws java.io.IOException- Specified by:
readFully
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
skipBytes
public int skipBytes(int n) throws java.io.IOExceptionÜberspringt n genau Bytes. Anders als DataInput definiert wird immer genau die übergebene Zahl an bytes übersprungen, d.h. die Methode gibt immer den Parameter n zurück. Daher entspricht diese Methodeposition(position() + n); return n;
Diese Methode kann über das Dateiende hinausspringen, vgl.
RandomAccessFile.seek(long)
.- Specified by:
skipBytes
in interfacejava.io.DataInput
- Parameters:
n
- Anzahl zu überspringender Bytes (kann negativ sein, dann wird rückwärts gesprungen)- Returns:
- n
- Throws:
java.io.IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
skip
public long skip(long n) throws java.io.IOExceptionÜberspringt genau n Bytes. Daher entspricht diese Methodeposition(position() + n); return n;
Diese Methode kann über das Dateiende hinausspringen, vgl.
RandomAccessFile.seek(long)
.- Parameters:
n
- Anzahl zu überspringender Bytes (kann negativ sein, dann wird rückwärts gesprungen)- Returns:
- Der Parameter n (zur Kompatibilität mit FileChannel)
- Throws:
java.io.IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
readBoolean
public boolean readBoolean() throws java.io.IOException- Specified by:
readBoolean
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readByte
public byte readByte() throws java.io.IOException- Specified by:
readByte
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOException- Specified by:
readUnsignedByte
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOException- Specified by:
readShort
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOException- Specified by:
readUnsignedShort
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readChar
public char readChar() throws java.io.IOException- Specified by:
readChar
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOException- Specified by:
readInt
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readLong
public long readLong() throws java.io.IOException- Specified by:
readLong
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOException- Specified by:
readFloat
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOException- Specified by:
readDouble
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
readLine
@Deprecated public java.lang.String readLine()Deprecated.Das Lesen einer einzelnen Zeile wird von dieser Klasse nicht unterstützt, da sie für binäre Daten gedacht ist.- Specified by:
readLine
in interfacejava.io.DataInput
- Throws:
java.lang.UnsupportedOperationException
- immer
-
readUTF
public java.lang.String readUTF() throws java.io.IOException- Specified by:
readUTF
in interfacejava.io.DataInput
- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Specified by:
write
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException- Specified by:
writeBoolean
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException- Specified by:
writeByte
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException- Specified by:
writeShort
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException- Specified by:
writeChar
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException- Specified by:
writeInt
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException- Specified by:
writeLong
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException- Specified by:
writeFloat
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException- Specified by:
writeDouble
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException- Specified by:
writeBytes
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException- Specified by:
writeChars
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String s) throws java.io.IOException- Specified by:
writeUTF
in interfacejava.io.DataOutput
- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOException- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Specified by:
read
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOExceptionMethode analog zuRandomAccessFile.read(byte[])
. Sollte nicht benutzt werden, sie fehleranfällig ist falls nicht der ganze Puffer gelesen wird. Besser:readFully(byte[])
- Parameters:
b
- Puffer- Returns:
- Anzahl gelesener bytes
- Throws:
java.io.IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionMethode analog zuRandomAccessFile.read(byte[], int, int)
. Sollte nicht benutzt werden, sie fehleranfällig ist falls nicht der ganze Puffer gelesen wird. Besser:readFully(byte[], int, int)
- Parameters:
b
- Pufferoff
- Position im Puffer an die die Daten geschrieben werdenlen
- Maximalanzahl zu lesender Bytes- Returns:
- Anzahl gelesener bytes
- Throws:
java.io.IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
read
@Deprecated public int read() throws java.io.IOExceptionDeprecated.Methode analog zuRandomAccessFile.read()
. Sollte nicht benutzt werden, da fehleranfällig bei Dateiende. Besser:readByte()
- Returns:
- Gelesenes byte oder -1 falls am Dateiende.
- Throws:
java.io.IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOException- Specified by:
write
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.IOException
-
position
public long position()- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- See Also:
FileChannel.position()
-
position
- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
- See Also:
FileChannel.position(long)
-
truncate
- Specified by:
truncate
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
- See Also:
FileChannel.truncate(long)
-
size
public long size() throws java.io.IOExceptionGibt die Dateilänge zurück- Specified by:
size
in interfacejava.nio.channels.SeekableByteChannel
- Returns:
- Länge in Bytes
- Throws:
java.io.IOException
- See Also:
FileChannel.size()
-
seek
public void seek(long position) throws java.io.IOExceptionFür RandomAccessFile-Kompatibilität- Parameters:
position
- Neue Position- Throws:
java.io.IOException
- See Also:
position(long)
,RandomAccessFile.seek(long)
-
getFilePointer
public long getFilePointer()Für RandomAccessFile-Kompatibilität- Returns:
- Position
- See Also:
position()
,RandomAccessFile.getFilePointer()
-
length
public long length() throws java.io.IOExceptionFür RandomAccessFile-Kompatibilität- Returns:
- Dateilänge
- Throws:
java.io.IOException
- See Also:
size()
,RandomAccessFile.length()
-
setLength
public void setLength(long len) throws java.io.IOExceptionFür RandomAccessFile-Kompatibilität- Parameters:
len
- neue Dateilänge- Throws:
java.io.IOException
- See Also:
RandomAccessFile.setLength(long)
-
flush
public void flush() throws java.io.IOExceptionSchreibt den Schreibpuffer auf die Festplatte- Throws:
java.io.IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-