1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
* Copyright (C) 2003-2009 JNode.org
* 2009,2010 Matthias Treydte <mt@waldheinz.de>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; If not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package de.waldheinz.fs.fat;
/**
* Little endian (LSB first) conversion methods.
*
* @author Ewout Prangsma <epr at users.sourceforge.net>
*/
final class LittleEndian {
private LittleEndian() { /* no instances */ }
/**
* Gets an 8-bit unsigned integer from the given byte array at
* the given offset.
*
* @param src the byte offset where to read the value from
* @param offset the byte array to extract the value from
* @return the integer that was read
*/
public static int getUInt8(byte[] src, int offset) {
return src[offset] & 0xFF;
}
/**
* Gets a 16-bit unsigned integer from the given byte array at the given offset.
*
* @param src
* @param offset
*/
public static int getUInt16(byte[] src, int offset) {
final int v0 = src[offset + 0] & 0xFF;
final int v1 = src[offset + 1] & 0xFF;
return ((v1 << 8) | v0);
}
/**
* Gets a 32-bit unsigned integer from the given byte array at the given offset.
*
* @param src
* @param offset
*/
public static long getUInt32(byte[] src, int offset) {
final long v0 = src[offset + 0] & 0xFF;
final long v1 = src[offset + 1] & 0xFF;
final long v2 = src[offset + 2] & 0xFF;
final long v3 = src[offset + 3] & 0xFF;
return ((v3 << 24) | (v2 << 16) | (v1 << 8) | v0);
}
/**
* Sets an 8-bit integer in the given byte array at the given offset.
*/
public static void setInt8(byte[] dst, int offset, int value) {
dst[offset] = (byte) value;
}
/**
* Sets a 16-bit integer in the given byte array at the given offset.
*/
public static void setInt16(byte[] dst, int offset, int value) {
dst[offset + 0] = (byte) (value & 0xFF);
dst[offset + 1] = (byte) ((value >>> 8) & 0xFF);
}
/**
* Sets a 32-bit integer in the given byte array at the given offset.
*/
public static void setInt32(byte[] dst, int offset, long value)
throws IllegalArgumentException {
if (value > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
value + " can not be represented in a 32bit dword");
}
dst[offset + 0] = (byte) (value & 0xFF);
dst[offset + 1] = (byte) ((value >>> 8) & 0xFF);
dst[offset + 2] = (byte) ((value >>> 16) & 0xFF);
dst[offset + 3] = (byte) ((value >>> 24) & 0xFF);
}
}
|