Home | History | Annotate | Download | only in fat
      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.fat;
     21 
     22 import java.io.IOException;
     23 
     24 /**
     25  * <description>
     26  *
     27  * @author Ewout Prangsma &lt; epr at jnode.org&gt;
     28  * @author Fabien DUMINY
     29  * @author Matthias Treydte &lt;waldheinz at gmail.com&gt;
     30  */
     31 public class FatUtils {
     32 
     33     /**
     34      * Gets the offset (in bytes) of the fat with the given index
     35      *
     36      * @param bs
     37      * @param fatNr (0..)
     38      * @return long
     39      * @throws IOException
     40      */
     41     public static long getFatOffset(BootSector bs, int fatNr) {
     42         long sectSize = bs.getBytesPerSector();
     43         long sectsPerFat = bs.getSectorsPerFat();
     44         long resSects = bs.getNrReservedSectors();
     45 
     46         long offset = resSects * sectSize;
     47         long fatSize = sectsPerFat * sectSize;
     48 
     49         offset += fatNr * fatSize;
     50 
     51         return offset;
     52     }
     53 
     54     /**
     55      * Gets the offset (in bytes) of the root directory with the given index
     56      *
     57      * @param bs
     58      * @return long
     59      * @throws IOException
     60      */
     61     public static long getRootDirOffset(BootSector bs) {
     62         long sectSize = bs.getBytesPerSector();
     63         long sectsPerFat = bs.getSectorsPerFat();
     64         int fats = bs.getNrFats();
     65 
     66         long offset = getFatOffset(bs, 0);
     67 
     68         offset += fats * sectsPerFat * sectSize;
     69 
     70         return offset;
     71     }
     72 
     73     /**
     74      * Gets the offset of the data (file) area
     75      *
     76      * @param bs
     77      * @return long
     78      * @throws IOException
     79      */
     80     public static long getFilesOffset(BootSector bs) {
     81         long offset = getRootDirOffset(bs);
     82 
     83         offset += bs.getRootDirEntryCount() * 32;
     84 
     85         return offset;
     86     }
     87 
     88     private FatUtils() { /* no instances */ }
     89 
     90 }
     91