View Javadoc

1   package edu.iris.dmc.seedcodec;
2   
3   /***
4    * Generic class providing static methods for converting between integer numbers
5    * and byte arrays.
6    *
7    * @author Philip Crotwell
8    * @author Robert Casey
9    * @version 6/6/2002
10   */
11  
12  public class Utility  {
13  
14  	/***
15  	 * Concatenate two bytes to a short integer value.  Accepts a high and low byte to be
16  	 * converted to a 16-bit integer.  if swapBytes is true, then <b>b</b> becomes
17  	 * the high order byte.
18  	 * @param a high order byte
19  	 * @param b low order byte
20  	 * @param swapBytes reverse the roles of the first two parameters
21  	 * @return short integer representation of the concatenated bytes
22  	 */
23  	public static short bytesToShort(byte a, byte b, boolean swapBytes) {
24  		if (swapBytes) {
25  			return (short)((a & 0xff) + (b & 0xff) << 8);
26  		} else {
27  			return (short)(((a & 0xff) << 8) + (b & 0xff));
28  		}
29  	}
30  
31  
32  	// convert signed bytes to an integer value
33  
34  	/***
35  	 * Convert a single byte to a 32-bit int, with sign extension.
36  	 * @param a signed byte value
37  	 * @return 32-bit integer
38  	 */
39  	public static int bytesToInt(byte a) {
40  		return (int)a;  // whatever the high-order bit is set to is extended into integer 32-bit space
41  	}
42  
43  	/***
44  	 * Concatenate two bytes to a 32-bit int value.  <b>a</b> is the high order
45  	 * byte in the resulting int representation, unless swapBytes is true, in
46  	 * which <b>b</b> is the high order byte.
47  	 * @param a high order byte
48  	 * @param b low order byte
49  	 * @param swapBytes byte order swap flag
50  	 * @return 32-bit integer
51  	 */
52  	public static int bytesToInt(byte a, byte b, boolean swapBytes) {
53  		// again, high order bit is expressed left into 32-bit form
54  		if (swapBytes) {
55  			return (a & 0xff) + ((int)b << 8);
56  		} else {
57  			return ((int)a << 8) + (b & 0xff);
58  		}
59  	}
60  
61  	/***
62  	 * Concatenate three bytes to a 32-bit int value.  Byte order is <b>a,b,c</b>
63  	 * unless swapBytes is true, in which case the order is <b>c,b,a</b>.
64  	 * @param a highest order byte
65  	 * @param b second-highest order byte
66  	 * @param c lowest order byte
67  	 * @param swapBytes byte order swap flag
68  	 * @return 32-bit integer
69  	 */
70  	public static int bytesToInt(byte a, byte b, byte c, boolean swapBytes) {
71  		if (swapBytes) {
72  			return (a & 0xff ) + ((b & 0xff) << 8 ) + ((int)c  << 16);
73  		} else {
74  			return ((int)a  << 16 ) + ((b & 0xff) << 8 ) + (c & 0xff);
75  		}
76  	}
77  
78  
79  	/***
80  	 * Concatenate four bytes to a 32-bit int value.  Byte order is <b>a,b,c,d</b>
81  	 * unless swapBytes is true, in which case the order is <b>d,c,b,a</b>.
82  	 * <i>Note:</i> This method will accept unsigned and signed byte
83  	 * representations, since high bit extension is not a concern here.
84  	 * Java does not support unsigned integers, so the maximum value is not as
85  	 * high as would be the case with an unsigned integer.  To hold an unsigned
86  	 * 32-bit value, use uBytesToLong().
87  	 * @param a highest order byte
88  	 * @param b second-highest order byte
89  	 * @param c second-lowest order byte
90  	 * @param d lowest order byte
91  	 * @param swapBytes byte order swap flag
92  	 * @return 32-bit integer
93  	 * @see edu.iris.Fissures.seed.util.Utility#uBytesToLong(byte,byte,byte,byte,boolean)
94  	 */
95  	public static int bytesToInt(byte a, byte b, byte c, byte d, boolean swapBytes) {
96  		if (swapBytes) {
97  			return ((a & 0xff) ) +
98  				((b & 0xff) << 8 ) +
99  				((c & 0xff) << 16 ) +
100 				((d & 0xff) << 24);
101 		} else {
102 			return ((a & 0xff) << 24 ) +
103 				((b & 0xff) << 16 ) +
104 				((c & 0xff) << 8 ) +
105 				((d & 0xff) );
106 		}    
107 	}
108 
109 
110 	// convert unsigned byte representations to an integer value
111 
112 	/***
113 	 * Treat byte value as an unsigned value and convert to a 32-bit int value.
114 	 * @param a unsigned byte value
115 	 * @return positive 32-bit integer
116 	 */
117 	public static int uBytesToInt(byte a) {
118 		// we "and" with 0xff in order to get the sign correct (pos)
119 		// this extends zeroes left into 32-bit	space
120 		return a & 0xff;
121 	}
122 
123 	/***
124 	 * Conatenate two unsigned byte values into a 32-bit integer.
125 	 * @param a high order unsigned byte
126 	 * @param b low order unsigned byte
127 	 * @param swapBytes if true, <b>b</b> becomes the high order byte
128 	 * @return positive 32-bit integer
129 	 */
130 	public static int uBytesToInt(byte a, byte b, boolean swapBytes) {
131 		// we "and" with 0xff to get the sign correct (pos)
132 		if (swapBytes) {
133 			return (a & 0xff) + ((b & 0xff) << 8);
134 		} else {
135 			return ((a & 0xff) << 8) + (b & 0xff);
136 		}
137 	}
138 
139 	/***
140 	 * Conacatenate four unsigned byte values into a long integer.
141 	 * This method puts out a long value because a large unsigned 32-bit value would
142 	 * exceed the capacity of an int, which is considered signed in Java.
143 	 * @param a highest-order byte
144 	 * @param b second-highest order byte
145 	 * @param c second-lowest order byte
146 	 * @param d lowest order byte
147 	 * @param swapBytes if true, byte order is <b>d,c,b,a</b>, else order is
148 	 * <b>a,b,c,d</b>
149 	 * @return positive long integer
150 	 */
151 	public static long uBytesToLong(byte a, byte b, byte c, byte d, boolean swapBytes) {
152 		if (swapBytes) {
153 			return ((a & 0xffL) ) +
154 				((b & 0xffL) << 8 ) +
155 				((c & 0xffL) << 16 ) +
156 				((d & 0xffL) << 24);
157 		} else {
158 			return ((a & 0xffL) << 24 ) +
159 				((b & 0xffL) << 16 ) +
160 				((c & 0xffL) << 8 ) +
161 				((d & 0xffL) );
162 		}    
163 	}
164 
165 	/***
166 	 * Convert a long value to a 4-byte array.
167 	 * @param a long integer
168 	 * @return byte[4] array
169 	 */
170 	public static byte[] longToIntBytes(long a) {
171 		byte[] returnByteArray = new byte[4]; //int is 4 bytes
172 		returnByteArray[0] = (byte)((a & 0xff000000)>>24);
173 		returnByteArray[1] = (byte)((a & 0x00ff0000)>>16);
174 		returnByteArray[2] = (byte)((a & 0x0000ff00)>>8);
175 		returnByteArray[3] = (byte)((a & 0x000000ff));
176 		return returnByteArray;
177 	}
178 
179 	/***
180 	 * Convert an int value to a 2-byte array.
181 	 * @param a int value
182 	 * @return byte[2] array
183 	 */
184 	public static byte[] intToShortBytes(int a) {
185 		byte[] returnByteArray = new byte[2];  //short is 2 bytes
186 		returnByteArray[0] = (byte)((a & 0x0000ff00)>>8);
187 		returnByteArray[1] = (byte)((a & 0x000000ff));
188 		return returnByteArray;
189 	}
190 
191 
192 	// miscellaneous utilities
193 
194 	/***
195 	 * Return a byte array of length <b>requiredBytes</b> that contains the
196 	 * contents of <b>source</b> and is padded on the end with <b>paddingByte</b>.
197 	 * If <b>requiredBytes</b> is less than or equal to the length of
198 	 * <b>source</b>, then <b>source</b> will simply be returned.
199 	 * @param source byte array to have <b>paddingByte</b>(s) appended to
200 	 * @param requiredBytes the length in bytes of the returned byte array
201 	 * @param paddingByte the byte value that will be appended to the array to
202 	 * fill out the required byte size of the return array
203 	 * @return byte array of size <b>requiredBytes</b>
204 	 */
205 	public static byte[]  pad(byte[] source,int requiredBytes, byte paddingByte) {
206 		if (source.length >= requiredBytes) {
207 			return source;
208 		} else {
209 			byte[] returnByteArray = new byte[requiredBytes];
210 			System.arraycopy(source, 0, returnByteArray, 0, source.length);
211 			for(int i = source.length; i<requiredBytes; i++)
212 			{
213 				returnByteArray[i] = (byte)paddingByte;
214 			}
215 			return returnByteArray;
216 		}
217 	}
218 
219 	/***
220 	 * Return a byte array which is a subset of bytes from <b>source</b>
221 	 * beginning with index <b>start</b> and stopping just before index
222 	 * <b>end</b>.
223 	 * @param source source byte array
224 	 * @param start starting index, inclusive
225 	 * @param end ending index, exclusive
226 	 * @return byte array of length <b>start</b>-<b>end</b>
227 	 */
228 	public static byte[] format(byte[] source, int start, int end) {
229 		byte[] returnByteArray = new byte[start-end+1];
230 		int j = 0;
231 		for(int i = start; i < end; i++,j++) {
232 			returnByteArray[j] = source[i];
233 		}
234 		return returnByteArray;
235 	}
236 
237 
238 	/***
239 	 * Test method.
240 	 * @param args not used.
241 	 */
242 	public static void main (String[] args)
243 	{
244 		int a = 256;
245 		byte a1 = (byte)((a & 0xff000000)>>24);
246 		byte a2 = (byte)((a & 0x00ff0000)>>16);
247 		byte a3 = (byte)((a & 0x0000ff00)>>8);
248 		byte a4 = (byte) ((a & 0x000000ff));
249 		System.out.println("first byte is " + a1);
250 		System.out.println("2 byte is " + a2);
251 		System.out.println("3 byte is " + a3);
252 		System.out.println("4  byte is " + a4);
253 		byte[] source = new byte[5];
254 		for(int i=0; i< 5; i++)
255 			source[i] = (byte)10;
256 		byte[] output = Utility.pad(source, 5, (byte)32);
257 		for(int k=output.length-1; k > -1; k--)
258 		{
259 			System.out.println("byte" + k +" " + output[k]);
260 		}
261 	}
262 
263 }