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 intGröße des Lese und Schreibpuffersprotected DataInputStreamGepufferter EingabeStream, wird bei Bedarf initialisiert und gelöschtprotected DataOutputStreamGepufferter AusgabeStream, wird bei Bedarf initialisiert und gelöschtprotected longAktuelle 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 intStandardpuffergröße -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidflush()Schreibt den Schreibpuffer auf die Festplatteprotected voidprotected voidprotected abstract FileChannelGibt einen gültigen FileChannel zurück, mit dem die Klasse die Datei manipulieren kann.protected abstract DataInputStreamGibt einen DataInputStream zum Lesen zurückprotected abstract DataOutputStreamGibt einen DataOutputStream zum Schreiben zurücklongFür RandomAccessFile-Kompatibilitätabstract booleanisOpen()longlength()Für RandomAccessFile-Kompatibilitätlongposition()position(long newPosition) intread()Deprecated.intread(byte[] b) Methode analog zuRandomAccessFile.read(byte[]).intread(byte[] b, int off, int len) Methode analog zuRandomAccessFile.read(byte[], int, int).intread(ByteBuffer dst) booleanbytereadByte()charreadChar()doublefloatvoidreadFully(byte[] b) voidreadFully(byte[] b, int off, int len) intreadInt()readLine()Deprecated.longreadLong()shortintintreadUTF()voidseek(long position) Für RandomAccessFile-KompatibilitätvoidsetLength(long len) Für RandomAccessFile-Kompatibilitätlongsize()Gibt die Dateilänge zurücklongskip(long n) Überspringt genau n Bytes.intskipBytes(int n) Überspringt n genau Bytes.toString()truncate(long size) voidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidwrite(int b) intwrite(ByteBuffer src) voidwriteBoolean(boolean v) voidwriteByte(int v) voidwriteBytes(String s) voidwriteChar(int v) voidwriteChars(String s) voidwriteDouble(double v) voidwriteFloat(float v) voidwriteInt(int v) voidwriteLong(long v) voidwriteShort(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:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Throws:
IOException
-
readFully
- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readFully
- Specified by:
readFullyin 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:
skipBytesin 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:
readBooleanin interfaceDataInput- Throws:
IOException
-
readByte
- Specified by:
readBytein interfaceDataInput- Throws:
IOException
-
readUnsignedByte
- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
IOException
-
readShort
- Specified by:
readShortin interfaceDataInput- Throws:
IOException
-
readUnsignedShort
- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
IOException
-
readChar
- Specified by:
readCharin interfaceDataInput- Throws:
IOException
-
readInt
- Specified by:
readIntin interfaceDataInput- Throws:
IOException
-
readLong
- Specified by:
readLongin interfaceDataInput- Throws:
IOException
-
readFloat
- Specified by:
readFloatin interfaceDataInput- Throws:
IOException
-
readDouble
- Specified by:
readDoublein 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:
readLinein interfaceDataInput- Throws:
UnsupportedOperationException- immer
-
readUTF
- Specified by:
readUTFin interfaceDataInput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
writeBoolean
- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeByte
- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeShort
- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeChar
- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeInt
- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeFloat
- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeDouble
- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeBytes
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChars
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
writeUTF
- Specified by:
writeUTFin interfaceDataOutput- Throws:
IOException
-
read
- Specified by:
readin interfaceReadableByteChannel- Specified by:
readin 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:
writein interfaceSeekableByteChannel- Specified by:
writein interfaceWritableByteChannel- Throws:
IOException
-
position
public long position()- Specified by:
positionin interfaceSeekableByteChannel- See Also:
-
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
IOException- See Also:
-
truncate
- Specified by:
truncatein interfaceSeekableByteChannel- Throws:
IOException- See Also:
-
size
Gibt die Dateilänge zurück- Specified by:
sizein 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
-