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.SeekableByteChannelAbstrakte Basisklasse für Klassen, die RandomAccessFile-ähnliche Funktionalität bieten
-
-
Field Summary
Fields Modifier and Type Field Description protected int_bufferSizeGröße des Lese und Schreibpuffersprotected java.io.DataInputStream_dataInStreamGepufferter EingabeStream, wird bei Bedarf initialisiert und gelöschtprotected java.io.DataOutputStream_dataOutStreamGepufferter AusgabeStream, wird bei Bedarf initialisiert und gelöschtprotected 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 entsprichtprotected static intdefaultBufferSizeStandardpuffergröße
-
Constructor Summary
Constructors Constructor Description FileAccess(int bufferSize)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()voidflush()Schreibt den Schreibpuffer auf die Festplatteprotected voidflushInStream()protected voidflushOutStream()protected abstract java.nio.channels.FileChannelgetChannel()Gibt einen gültigen FileChannel zurück, mit dem die Klasse die Datei manipulieren kann.protected abstract java.io.DataInputStreamgetDataInStream()Gibt einen DataInputStream zum Lesen zurückprotected abstract java.io.DataOutputStreamgetDataOutStream()Gibt einen DataOutputStream zum Schreiben zurücklonggetFilePointer()Für RandomAccessFile-Kompatibilitätabstract booleanisOpen()longlength()Für RandomAccessFile-Kompatibilitätlongposition()FileAccessposition(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(java.nio.ByteBuffer dst)booleanreadBoolean()bytereadByte()charreadChar()doublereadDouble()floatreadFloat()voidreadFully(byte[] b)voidreadFully(byte[] b, int off, int len)intreadInt()java.lang.StringreadLine()Deprecated.longreadLong()shortreadShort()intreadUnsignedByte()intreadUnsignedShort()java.lang.StringreadUTF()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.java.lang.StringtoString()FileAccesstruncate(long size)voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)intwrite(java.nio.ByteBuffer src)voidwriteBoolean(boolean v)voidwriteByte(int v)voidwriteBytes(java.lang.String s)voidwriteChar(int v)voidwriteChars(java.lang.String s)voidwriteDouble(double v)voidwriteFloat(float v)voidwriteInt(int v)voidwriteLong(long v)voidwriteShort(int v)voidwriteUTF(java.lang.String s)
-
-
-
Field Detail
-
defaultBufferSize
protected static final int defaultBufferSize
Standardpuffergröße- See Also:
- Constant Field Values
-
_bufferSize
protected final int _bufferSize
Größe des Lese und Schreibpuffers
-
_dataInStream
protected java.io.DataInputStream _dataInStream
Gepufferter EingabeStream, wird bei Bedarf initialisiert und gelöscht
-
_dataOutStream
protected java.io.DataOutputStream _dataOutStream
Gepufferter AusgabeStream, wird bei Bedarf initialisiert und gelöscht
-
_position
protected 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 entspricht
-
-
Method Detail
-
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:
isOpenin interfacejava.nio.channels.Channel
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b) throws java.io.IOException- Specified by:
readFullyin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws java.io.IOException- Specified by:
readFullyin 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:
skipBytesin 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:
readBooleanin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readByte
public byte readByte() throws java.io.IOException- Specified by:
readBytein interfacejava.io.DataInput- Throws:
java.io.IOException
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOException- Specified by:
readUnsignedBytein interfacejava.io.DataInput- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOException- Specified by:
readShortin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOException- Specified by:
readUnsignedShortin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readChar
public char readChar() throws java.io.IOException- Specified by:
readCharin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOException- Specified by:
readIntin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readLong
public long readLong() throws java.io.IOException- Specified by:
readLongin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOException- Specified by:
readFloatin interfacejava.io.DataInput- Throws:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOException- Specified by:
readDoublein 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:
readLinein interfacejava.io.DataInput- Throws:
java.lang.UnsupportedOperationException- immer
-
readUTF
public java.lang.String readUTF() throws java.io.IOException- Specified by:
readUTFin interfacejava.io.DataInput- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException- Specified by:
writeBooleanin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException- Specified by:
writeBytein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException- Specified by:
writeShortin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException- Specified by:
writeCharin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException- Specified by:
writeIntin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException- Specified by:
writeLongin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException- Specified by:
writeFloatin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException- Specified by:
writeDoublein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException- Specified by:
writeBytesin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException- Specified by:
writeCharsin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String s) throws java.io.IOException- Specified by:
writeUTFin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOException- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- Specified by:
readin 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:
writein interfacejava.nio.channels.SeekableByteChannel- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Throws:
java.io.IOException
-
position
public long position()
- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- See Also:
FileChannel.position()
-
position
public FileAccess position(long newPosition) throws java.io.IOException
- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException- See Also:
FileChannel.position(long)
-
truncate
public FileAccess truncate(long size) throws java.io.IOException
- Specified by:
truncatein 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:
sizein 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:
toStringin classjava.lang.Object
-
-