1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.office.endianness;
22
23 /**
24 * This class provides methods for reading information from a byte array in an
25 * endian-neutral way.
26 */
27 public abstract class EndianDecoder {
28
29 /**
30 * Reads 2 bytes of information from given offset(i.e. zero based index)
31 * into the specified byte array, decodes them and return the char value.
32 *
33 * @param bytes the byte array to decode an <code>int</code> from
34 * @param offset the offset(zero based index) into the byte array to
35 * @return the char value of the 2 bytes at the specified position
36 */
37 public char decode2ByteChar( final byte[] bytes, final int offset ) {
38 return decodeChar( bytes[ offset ], bytes[ offset + 1 ] );
39 }
40
41 /**
42 * Reads 2 bytes of information from given offset(i.e. zero based index)
43 * into the specified byte array, decodes them and return the integer
44 * value.
45 *
46 * @param bytes the byte array to decode an <code>int</code> from
47 * @param offset the offset(zero based index) into the byte array to
48 * @return the integer value of the 2 bytes at the specified position
49 */
50 public int decode2ByteInt( final byte[] bytes, final int offset ) {
51 return decodeInt( bytes[ offset ], bytes[ offset + 1 ] );
52 }
53
54 /**
55 * Reads 4 bytes of information from given offset(i.e. zero based index)
56 * into the specified byte array, decodes them and return the integer
57 * value.
58 *
59 * @param bytes the byte array to decode an <code>int</code> from
60 * @param offset the offset(zero based index) into the byte array to
61 * @return the integer value of the 4 bytes at the specified position
62 */
63 public int decode4ByteInt( final byte[] bytes, final int offset ) {
64 return decodeInt( bytes[ offset ], bytes[ offset + 1 ], bytes[ offset + 2 ], bytes[ offset + 3 ] );
65 }
66
67 /**
68 * Reads 8 bytes of information from given offset(i.e. zero based index)
69 * into the specified byte array, decodes them and return the long value.
70 *
71 * @param bytes the byte array to decode an <code>int</code> from
72 * @param offset the offset(zero based index) into the byte array to
73 * @return the long value of the 8 bytes at the specified position
74 */
75 public abstract long decode8ByteLong( final byte[] bytes, final int offset );
76
77 /**
78 * Returns the char value resulting from decoding the specified bytes as
79 * a char.
80 *
81 * @param byte1 the first byte in the stream
82 * @param byte2 the second byte in the stream
83 * @return the char value resulting from decoding the given bytes
84 */
85 public char decodeChar( final int byte1, final int byte2 ) {
86 return (char) this.decodeInt( byte1, byte2 );
87 }
88
89 /**
90 * Returns the integer value resulting from decoding the specified bytes as
91 * an integer.
92 *
93 * @param byte1 the first byte in the stream
94 * @param byte2 the second byte in the stream
95 * @return the integer value resulting from decoding the given bytes
96 */
97 public abstract int decodeInt( final byte byte1, final byte byte2 );
98
99 /**
100 * Returns the integer value resulting from decoding the specified bytes as
101 * an integer. Only the least significant 8 bits of the specified integers
102 * are used, the other 24 bits are ignored.
103 *
104 * @param byte1 the first byte in the stream
105 * @param byte2 the second byte in the stream
106 * @return the integer value resulting from decoding the given bytes
107 */
108 public int decodeInt( final int byte1, final int byte2 ) {
109 return this.decodeInt( (byte) byte1, (byte) byte2 );
110 }
111
112 /**
113 * Returns the integer value resulting from decoding the specified bytes as
114 * an integer.
115 *
116 * @param b1 the first byte in the stream
117 * @param b2 the second byte in the stream
118 * @param b3 the third byte in the stream
119 * @param b4 the fourth byte in the stream
120 * @return the integer value resulting from decoding the given bytes
121 */
122 public abstract int decodeInt( final byte b1, final byte b2, final byte b3, final byte b4 );
123
124 /**
125 * Returns the integer value resulting from decoding the specified bytes as
126 * an integer.
127 *
128 * @param b1 the first byte in the stream
129 * @param b2 the second byte in the stream
130 * @param b3 the third byte in the stream
131 * @param b4 the fourth byte in the stream
132 * @return the integer value resulting from decoding the given bytes
133 */
134 public int decodeInt( final int b1, final int b2, final int b3, final int b4 ) {
135 return this.decodeInt( (byte) b1, (byte) b2, (byte) b3, (byte) b4 );
136 }
137
138 }