Package de.bsvrz.dav.daf.util
Class FileAccess
java.lang.Object
de.bsvrz.dav.daf.util.FileAccess
- All Implemented Interfaces:
Closeable
,DataInput
,DataOutput
,AutoCloseable
,ByteChannel
,Channel
,ReadableByteChannel
,SeekableByteChannel
,WritableByteChannel
- Direct Known Subclasses:
BufferedRandomAccessFile
,CloseableRandomAccessFile
public abstract class FileAccess
extends Object
implements DataInput, DataOutput, SeekableByteChannel
Abstrakte Basisklasse für Klassen, die RandomAccessFile-ähnliche Funktionalität bieten
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final int
Größe des Lese und Schreibpuffersprotected DataInputStream
Gepufferter EingabeStream, wird bei Bedarf initialisiert und gelöschtprotected DataOutputStream
Gepufferter AusgabeStream, wird bei Bedarf initialisiert und gelöschtprotected long
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 final int
Standardpuffergröße -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
flush()
Schreibt den Schreibpuffer auf die Festplatteprotected void
protected void
protected abstract FileChannel
Gibt einen gültigen FileChannel zurück, mit dem die Klasse die Datei manipulieren kann.protected abstract DataInputStream
Gibt einen DataInputStream zum Lesen zurückprotected abstract DataOutputStream
Gibt einen DataOutputStream zum Schreiben zurücklong
Für RandomAccessFile-Kompatibilitätabstract boolean
isOpen()
long
length()
Für RandomAccessFile-Kompatibilitätlong
position()
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
(ByteBuffer dst) boolean
byte
readByte()
char
readChar()
double
float
void
readFully
(byte[] b) void
readFully
(byte[] b, int off, int len) int
readInt()
readLine()
Deprecated.long
readLong()
short
int
int
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.toString()
truncate
(long size) void
write
(byte[] b) void
write
(byte[] b, int off, int len) void
write
(int b) int
write
(ByteBuffer src) void
writeBoolean
(boolean v) void
writeByte
(int v) void
writeBytes
(String s) void
writeChar
(int v) void
writeChars
(String s) void
writeDouble
(double v) void
writeFloat
(float v) void
writeInt
(int v) void
writeLong
(long v) void
writeShort
(int v) void
-
Field Details
-
defaultBufferSize
protected static final int defaultBufferSizeStandardpuffergröße- See Also:
-
_bufferSize
protected final int _bufferSizeGröße des Lese und Schreibpuffers -
_dataInStream
Gepufferter EingabeStream, wird bei Bedarf initialisiert und gelöscht -
_dataOutStream
Gepufferter 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
- Throws:
IOException
-
getDataOutStream
Gibt einen DataOutputStream zum Schreiben zurück- Returns:
- DataOutputStream
- Throws:
IOException
-
getDataInStream
Gibt einen DataInputStream zum Lesen zurück- Returns:
- DataInputStream
- Throws:
IOException
-
getChannel
Gibt einen gültigen FileChannel zurück, mit dem die Klasse die Datei manipulieren kann.- Returns:
- einen gültigen FileChannel
- Throws:
IOException
-
isOpen
public abstract boolean isOpen() -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
skipBytes
Ü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 interfaceDataInput
- Parameters:
n
- Anzahl zu überspringender Bytes (kann negativ sein, dann wird rückwärts gesprungen)- Returns:
- n
- Throws:
IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
skip
Ü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:
IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
readBoolean
- Specified by:
readBoolean
in interfaceDataInput
- Throws:
IOException
-
readByte
- Specified by:
readByte
in interfaceDataInput
- Throws:
IOException
-
readUnsignedByte
- Specified by:
readUnsignedByte
in interfaceDataInput
- Throws:
IOException
-
readShort
- Specified by:
readShort
in interfaceDataInput
- Throws:
IOException
-
readUnsignedShort
- Specified by:
readUnsignedShort
in interfaceDataInput
- Throws:
IOException
-
readChar
- Specified by:
readChar
in interfaceDataInput
- Throws:
IOException
-
readInt
- Specified by:
readInt
in interfaceDataInput
- Throws:
IOException
-
readLong
- Specified by:
readLong
in interfaceDataInput
- Throws:
IOException
-
readFloat
- Specified by:
readFloat
in interfaceDataInput
- Throws:
IOException
-
readDouble
- Specified by:
readDouble
in interfaceDataInput
- Throws:
IOException
-
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 interfaceDataInput
- Throws:
UnsupportedOperationException
- immer
-
readUTF
- Specified by:
readUTF
in interfaceDataInput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
writeBoolean
- Specified by:
writeBoolean
in interfaceDataOutput
- Throws:
IOException
-
writeByte
- Specified by:
writeByte
in interfaceDataOutput
- Throws:
IOException
-
writeShort
- Specified by:
writeShort
in interfaceDataOutput
- Throws:
IOException
-
writeChar
- Specified by:
writeChar
in interfaceDataOutput
- Throws:
IOException
-
writeInt
- Specified by:
writeInt
in interfaceDataOutput
- Throws:
IOException
-
writeLong
- Specified by:
writeLong
in interfaceDataOutput
- Throws:
IOException
-
writeFloat
- Specified by:
writeFloat
in interfaceDataOutput
- Throws:
IOException
-
writeDouble
- Specified by:
writeDouble
in interfaceDataOutput
- Throws:
IOException
-
writeBytes
- Specified by:
writeBytes
in interfaceDataOutput
- Throws:
IOException
-
writeChars
- Specified by:
writeChars
in interfaceDataOutput
- Throws:
IOException
-
writeUTF
- Specified by:
writeUTF
in interfaceDataOutput
- Throws:
IOException
-
read
- Specified by:
read
in interfaceReadableByteChannel
- Specified by:
read
in interfaceSeekableByteChannel
- Throws:
IOException
-
read
Methode 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:
IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
read
Methode 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:
IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
read
Deprecated.Methode analog zuRandomAccessFile.read()
. Sollte nicht benutzt werden, da fehleranfällig bei Dateiende. Besser:readByte()
- Returns:
- Gelesenes byte oder -1 falls am Dateiende.
- Throws:
IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
write
- Specified by:
write
in interfaceSeekableByteChannel
- Specified by:
write
in interfaceWritableByteChannel
- Throws:
IOException
-
position
public long position()- Specified by:
position
in interfaceSeekableByteChannel
- See Also:
-
position
- Specified by:
position
in interfaceSeekableByteChannel
- Throws:
IOException
- See Also:
-
truncate
- Specified by:
truncate
in interfaceSeekableByteChannel
- Throws:
IOException
- See Also:
-
size
Gibt die Dateilänge zurück- Specified by:
size
in interfaceSeekableByteChannel
- Returns:
- Länge in Bytes
- Throws:
IOException
- See Also:
-
seek
Für RandomAccessFile-Kompatibilität- Parameters:
position
- Neue Position- Throws:
IOException
- See Also:
-
getFilePointer
public long getFilePointer()Für RandomAccessFile-Kompatibilität- Returns:
- Position
- See Also:
-
length
Für RandomAccessFile-Kompatibilität- Returns:
- Dateilänge
- Throws:
IOException
- See Also:
-
setLength
Für RandomAccessFile-Kompatibilität- Parameters:
len
- neue Dateilänge- Throws:
IOException
- See Also:
-
flush
Schreibt den Schreibpuffer auf die Festplatte- Throws:
IOException
- Eingabe-/Ausgabefehler beim Lesen oder Schreiben der Datei
-
toString
-