Home | History | Annotate | Download | only in fs
      1 /*
      2  * Copyright (C) 2003-2009 JNode.org
      3  *               2009,2010 Matthias Treydte <mt (at) waldheinz.de>
      4  *
      5  * This library is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU Lesser General Public License as published
      7  * by the Free Software Foundation; either version 2.1 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
     13  * License for more details.
     14  *
     15  * You should have received a copy of the GNU Lesser General Public License
     16  * along with this library; If not, write to the Free Software Foundation, Inc.,
     17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     18  */
     19 
     20 package de.waldheinz.fs;
     21 
     22 import java.io.IOException;
     23 import java.nio.ByteBuffer;
     24 
     25 /**
     26  * This is the abstraction used for a device that can hold a {@link FileSystem}.
     27  *
     28  * @author Ewout Prangsma &lt;epr at jnode.org&gt;
     29  * @author Matthias Treydte &lt;waldheinz at gmail.com&gt;
     30  */
     31 public interface BlockDevice {
     32 
     33     /**
     34      * Gets the total length of this device in bytes.
     35      *
     36      * @return the total number of bytes on this device
     37      * @throws IOException on error getting the size of this device
     38      */
     39     public abstract long getSize() throws IOException;
     40 
     41     /**
     42      * Read a block of data from this device.
     43      *
     44      * @param devOffset the byte offset where to read the data from
     45      * @param dest the destination buffer where to store the data read
     46      * @throws IOException on read error
     47      */
     48     public abstract void read(long devOffset, ByteBuffer dest)
     49             throws IOException;
     50 
     51     /**
     52      * Writes a block of data to this device.
     53      *
     54      * @param devOffset the byte offset where to store the data
     55      * @param src the source {@code ByteBuffer} to write to the device
     56      * @throws ReadOnlyException if this {@code BlockDevice} is read-only
     57      * @throws IOException on write error
     58      * @throws IllegalArgumentException if the {@code devOffset} is negative
     59      *      or the write would go beyond the end of the device
     60      * @see #isReadOnly()
     61      */
     62     public abstract void write(long devOffset, ByteBuffer src)
     63             throws ReadOnlyException, IOException,
     64             IllegalArgumentException;
     65 
     66     /**
     67      * Flushes data in caches to the actual storage.
     68      *
     69      * @throws IOException on write error
     70      */
     71     public abstract void flush() throws IOException;
     72 
     73     /**
     74      * Returns the size of a sector on this device.
     75      *
     76      * @return the sector size in bytes
     77      * @throws IOException on error determining the sector size
     78      */
     79     public int getSectorSize() throws IOException;
     80 
     81     /**
     82      * Closes this {@code BlockDevice}. No methods of this device may be
     83      * accesses after this method was called.
     84      *
     85      * @throws IOException on error closing this device
     86      * @see #isClosed()
     87      */
     88     public void close() throws IOException;
     89 
     90     /**
     91      * Checks if this device was already closed. No methods may be called
     92      * on a closed device (except this method).
     93      *
     94      * @return if this device is closed
     95      */
     96     public boolean isClosed();
     97 
     98     /**
     99      * Checks if this {@code BlockDevice} is read-only.
    100      *
    101      * @return if this {@code BlockDevice} is read-only
    102      */
    103     public boolean isReadOnly();
    104 
    105 }
    106