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 Schreibpuffers
    protected java.io.DataInputStream _dataInStream
    Gepufferter EingabeStream, wird bei Bedarf initialisiert und gelöscht
    protected java.io.DataOutputStream _dataOutStream
    Gepufferter AusgabeStream, wird bei Bedarf initialisiert und gelöscht
    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
    protected 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 Festplatte
    protected 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ück
    protected abstract java.io.DataOutputStream getDataOutStream()
    Gibt einen DataOutputStream zum Schreiben zurück
    long getFilePointer()
    Für RandomAccessFile-Kompatibilität
    abstract boolean isOpen()  
    long length()
    Für RandomAccessFile-Kompatibilität
    long position()  
    FileAccess position​(long newPosition)  
    int read()
    Deprecated.
    int read​(byte[] b)
    Methode analog zu RandomAccessFile.read(byte[]).
    int read​(byte[] b, int off, int len)
    Methode analog zu RandomAccessFile.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ät
    void setLength​(long len)
    Für RandomAccessFile-Kompatibilität
    long size()
    Gibt die Dateilänge zurück
    long 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 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
  • 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.IOException
      Gibt einen DataOutputStream zum Schreiben zurück
      Returns:
      DataOutputStream
      Throws:
      java.io.IOException
    • getDataInStream

      protected abstract java.io.DataInputStream getDataInStream() throws java.io.IOException
      Gibt einen DataInputStream zum Lesen zurück
      Returns:
      DataInputStream
      Throws:
      java.io.IOException
    • getChannel

      protected abstract java.nio.channels.FileChannel getChannel() throws java.io.IOException
      Gibt 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 interface java.nio.channels.Channel
    • close

      public void close() throws java.io.IOException
      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface java.nio.channels.Channel
      Specified by:
      close in interface java.io.Closeable
      Throws:
      java.io.IOException
    • readFully

      public void readFully​(byte[] b) throws java.io.IOException
      Specified by:
      readFully in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFully

      public void readFully​(byte[] b, int off, int len) throws java.io.IOException
      Specified by:
      readFully in interface java.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 Methode position(position() + n); return n;

      Diese Methode kann über das Dateiende hinausspringen, vgl. RandomAccessFile.seek(long).

      Specified by:
      skipBytes in interface java.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 Methode position(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 interface java.io.DataInput
      Throws:
      java.io.IOException
    • readByte

      public byte readByte() throws java.io.IOException
      Specified by:
      readByte in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readUnsignedByte

      public int readUnsignedByte() throws java.io.IOException
      Specified by:
      readUnsignedByte in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readShort

      public short readShort() throws java.io.IOException
      Specified by:
      readShort in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readUnsignedShort

      public int readUnsignedShort() throws java.io.IOException
      Specified by:
      readUnsignedShort in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readChar

      public char readChar() throws java.io.IOException
      Specified by:
      readChar in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readInt

      public int readInt() throws java.io.IOException
      Specified by:
      readInt in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readLong

      public long readLong() throws java.io.IOException
      Specified by:
      readLong in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFloat

      public float readFloat() throws java.io.IOException
      Specified by:
      readFloat in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readDouble

      public double readDouble() throws java.io.IOException
      Specified by:
      readDouble in interface java.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 interface java.io.DataInput
      Throws:
      java.lang.UnsupportedOperationException - immer
    • readUTF

      public java.lang.String readUTF() throws java.io.IOException
      Specified by:
      readUTF in interface java.io.DataInput
      Throws:
      java.io.IOException
    • write

      public void write​(int b) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • write

      public void write​(byte[] b) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • write

      public void write​(byte[] b, int off, int len) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeBoolean

      public void writeBoolean​(boolean v) throws java.io.IOException
      Specified by:
      writeBoolean in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeByte

      public void writeByte​(int v) throws java.io.IOException
      Specified by:
      writeByte in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeShort

      public void writeShort​(int v) throws java.io.IOException
      Specified by:
      writeShort in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeChar

      public void writeChar​(int v) throws java.io.IOException
      Specified by:
      writeChar in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeInt

      public void writeInt​(int v) throws java.io.IOException
      Specified by:
      writeInt in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeLong

      public void writeLong​(long v) throws java.io.IOException
      Specified by:
      writeLong in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeFloat

      public void writeFloat​(float v) throws java.io.IOException
      Specified by:
      writeFloat in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeDouble

      public void writeDouble​(double v) throws java.io.IOException
      Specified by:
      writeDouble in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeBytes

      public void writeBytes​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeBytes in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeChars

      public void writeChars​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeChars in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeUTF

      public void writeUTF​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeUTF in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • read

      public int read​(java.nio.ByteBuffer dst) throws java.io.IOException
      Specified by:
      read in interface java.nio.channels.ReadableByteChannel
      Specified by:
      read in interface java.nio.channels.SeekableByteChannel
      Throws:
      java.io.IOException
    • read

      public int read​(byte[] b) throws java.io.IOException
      Methode analog zu RandomAccessFile.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.IOException
      Methode analog zu RandomAccessFile.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 - Puffer
      off - Position im Puffer an die die Daten geschrieben werden
      len - 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.IOException
      Deprecated.
      Methode analog zu RandomAccessFile.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 interface java.nio.channels.SeekableByteChannel
      Specified by:
      write in interface java.nio.channels.WritableByteChannel
      Throws:
      java.io.IOException
    • position

      public long position()
      Specified by:
      position in interface java.nio.channels.SeekableByteChannel
      See Also:
      FileChannel.position()
    • position

      public FileAccess position​(long newPosition) throws java.io.IOException
      Specified by:
      position in interface java.nio.channels.SeekableByteChannel
      Throws:
      java.io.IOException
      See Also:
      FileChannel.position(long)
    • truncate

      public FileAccess truncate​(long size) throws java.io.IOException
      Specified by:
      truncate in interface java.nio.channels.SeekableByteChannel
      Throws:
      java.io.IOException
      See Also:
      FileChannel.truncate(long)
    • size

      public long size() throws java.io.IOException
      Gibt die Dateilänge zurück
      Specified by:
      size in interface java.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.IOException
      Fü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.IOException
      Fü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.IOException
      Fü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.IOException
      Schreibt 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 class java.lang.Object