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 
     23 /**
     24  * Little endian (LSB first) conversion methods.
     25  *
     26  * @author Ewout Prangsma &lt;epr at users.sourceforge.net&gt;
     27  */
     28 final class LittleEndian {
     29 
     30     private LittleEndian() { /* no instances */ }
     31 
     32     /**
     33      * Gets an 8-bit unsigned integer from the given byte array at
     34      * the given offset.
     35      *
     36      * @param src the byte offset where to read the value from
     37      * @param offset the byte array to extract the value from
     38      * @return the integer that was read
     39      */
     40     public static int getUInt8(byte[] src, int offset) {
     41         return src[offset] & 0xFF;
     42     }
     43 
     44     /**
     45      * Gets a 16-bit unsigned integer from the given byte array at the given offset.
     46      *
     47      * @param src
     48      * @param offset
     49      */
     50     public static int getUInt16(byte[] src, int offset) {
     51         final int v0 = src[offset + 0] & 0xFF;
     52         final int v1 = src[offset + 1] & 0xFF;
     53         return ((v1 << 8) | v0);
     54     }
     55 
     56     /**
     57      * Gets a 32-bit unsigned integer from the given byte array at the given offset.
     58      *
     59      * @param src
     60      * @param offset
     61      */
     62     public static long getUInt32(byte[] src, int offset) {
     63         final long v0 = src[offset + 0] & 0xFF;
     64         final long v1 = src[offset + 1] & 0xFF;
     65         final long v2 = src[offset + 2] & 0xFF;
     66         final long v3 = src[offset + 3] & 0xFF;
     67         return ((v3 << 24) | (v2 << 16) | (v1 << 8) | v0);
     68     }
     69 
     70     /**
     71      * Sets an 8-bit integer in the given byte array at the given offset.
     72      */
     73     public static void setInt8(byte[] dst, int offset, int value) {
     74         dst[offset] = (byte) value;
     75     }
     76 
     77     /**
     78      * Sets a 16-bit integer in the given byte array at the given offset.
     79      */
     80     public static void setInt16(byte[] dst, int offset, int value) {
     81         dst[offset + 0] = (byte) (value & 0xFF);
     82         dst[offset + 1] = (byte) ((value >>> 8) & 0xFF);
     83     }
     84 
     85     /**
     86      * Sets a 32-bit integer in the given byte array at the given offset.
     87      */
     88     public static void setInt32(byte[] dst, int offset, long value)
     89             throws IllegalArgumentException {
     90 
     91         if (value > Integer.MAX_VALUE) {
     92             throw new IllegalArgumentException(
     93                     value + " can not be represented in a 32bit dword");
     94         }
     95 
     96         dst[offset + 0] = (byte) (value & 0xFF);
     97         dst[offset + 1] = (byte) ((value >>> 8) & 0xFF);
     98         dst[offset + 2] = (byte) ((value >>> 16) & 0xFF);
     99         dst[offset + 3] = (byte) ((value >>> 24) & 0xFF);
    100     }
    101 
    102 }
    103