1 /*
2 * $HeadURL: /cvsroot/insight/Insight/remote-protocol/src/com/mindtree/insight/remoteprotocol/PacketDataSizeOverLimitsException.java,v $
3 * $Date: 2005/08/09 18:39:17 $
4 * $Revision: 1.1 $
5 * $Author: m1001025 $
6 *
7 * Copyright (c) 2005 MindTree Consulting Ltd.
8 *
9 * This file is part of Insight Remote Protocol Library.
10 *
11 * Insight Remote Protocol Library is free software: you can redistribute it
12 * and/or modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, either version 3 of the License,
14 * or (at your option) any later version.
15 *
16 * Insight Remote Protocol Library is distributed in the hope that it will be
17 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * Insight Remote Protocol Library. If not, see <http://www.gnu.org/licenses/>.
23 */
24 package com.mindtree.techworks.insight.remoteprotocol.util;
25
26 import java.nio.ByteBuffer;
27 import java.nio.CharBuffer;
28 import java.nio.charset.CharacterCodingException;
29 import java.nio.charset.Charset;
30 import java.nio.charset.CharsetDecoder;
31 import java.nio.charset.CharsetEncoder;
32
33 /**
34 * Util class for byte manipulation.
35 *
36 * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
37 * @version $Revision: $ $Date: $
38 */
39 public class ByteUtils {
40
41 /**
42 * Reads a <code>short</code> value from a byte array.
43 *
44 * @param inputArr
45 * The byte array to read data from
46 * @param offset
47 * The offset to read data from
48 * @return The short read from the array
49 * @throws ArrayIndexOutOfBoundsException
50 * If data cannot be read from specified position
51 */
52 public static short readShortFromByteArray(byte[] inputArr, int offset) {
53
54 short returnShort = 0;
55
56 returnShort = (short) (returnShort | (inputArr[offset] & 0xFF));
57 returnShort = (short) (returnShort << 8 | (inputArr[offset + 1] & 0XFF));
58
59 return returnShort;
60 }
61
62 /**
63 * Reads an <code>int</code> value from a byte array.
64 *
65 * @param inputArr
66 * The byte array to read data from
67 * @param offset
68 * The offset to read data from
69 * @return The int read from the array
70 * @throws ArrayIndexOutOfBoundsException
71 * If data cannot be read from specified position
72 */
73 public static int readIntFromByteArray(byte[] inputArr, int offset) {
74
75 int returnInt = 0;
76
77 returnInt = returnInt | (inputArr[offset] & 0xFF);
78 returnInt = (returnInt << 8) | (inputArr[offset + 1] & 0xFF);
79 returnInt = (returnInt << 8) | (inputArr[offset + 2] & 0xFF);
80 returnInt = (returnInt << 8) | (inputArr[offset + 3] & 0xFF);
81
82 return returnInt;
83 }
84
85 /**
86 * Creates a byte array for a <code>short</code>. The returned array
87 * always has 2 elements.
88 *
89 * @param valueToConvert
90 * The short value to convert
91 * @return The byte array containing the two bytes of the short. The size is
92 * always 2.
93 */
94 public static byte[] getByteArrayForShort(short valueToConvert) {
95
96 byte[] returnVal = new byte[2];
97
98 putBytesInArrayForShort(valueToConvert, returnVal, 0);
99
100 return returnVal;
101 }
102
103 /**
104 * Creates a byte array for a <code>short</code>. The array values are
105 * placed in the input array <code>buf</code> from the position specified
106 * by the <code>offset</code>.
107 *
108 * @param valueToConvert
109 * The short value to convert
110 * @param buf
111 * The buffer to place the bytes in
112 * @param offset
113 * The offset to insert the values from
114 * @throws ArrayIndexOutOfBoundsException
115 * If the specified position in the array is not available.
116 */
117 public static void putBytesInArrayForShort(short valueToConvert,
118 byte[] buf, int offset) {
119
120 buf[offset] = (byte) ((valueToConvert >> 8) & 0x00FF);
121 buf[offset + 1] = (byte) (valueToConvert & 0x00FF);
122 }
123
124 /**
125 * Creates a byte array for an <code>int</code>. The returned array
126 * always has 4 elements.
127 *
128 * @param valueToConvert
129 * The integer value to convert
130 * @return The byte array containing the four bytes of the integer. The size
131 * is always 4.
132 */
133 public static byte[] getByteArrayForInt(int valueToConvert) {
134
135 byte[] returnVal = new byte[4];
136
137 putBytesInArrayForInt(valueToConvert, returnVal, 0);
138
139 return returnVal;
140 }
141
142 /**
143 * Creates a byte array for an <code>int</code>. The array values are
144 * placed in the input array <code>buf</code> from the position specified
145 * by the <code>offset</code>.
146 *
147 * @param valueToConvert
148 * The integer value to convert
149 * @param buf
150 * The buffer to place the bytes in
151 * @param offset
152 * The offset to insert the values from
153 * @throws ArrayIndexOutOfBoundsException
154 * If the specified position in the array is not available.
155 */
156 public static void putBytesInArrayForInt(int valueToConvert, byte[] buf,
157 int offset) {
158
159 buf[offset] = (byte) ((valueToConvert >> 24) & 0x00FF);
160 buf[offset + 1] = (byte) ((valueToConvert >> 16) & 0x00FF);
161 buf[offset + 2] = (byte) ((valueToConvert >> 8) & 0x00FF);
162 buf[offset + 3] = (byte) (valueToConvert & 0x00FF);
163 }
164
165 /**
166 * Creates a byte array representing the Input String. Every character is
167 * represented in two bytes. This method is same as calling
168 * <code>{@link #getByteArrayForCharacterArray(char[]) #getByteArrayForCharacterArray(char[])}</code>
169 * method with the character array representing the string.
170 *
171 * @param inputString
172 * The input string to convert
173 * @return A byte array for the string.
174 * @throws CharacterCodingException
175 * If there is an exception encoding the characters.
176 */
177 public static byte[] getByteArrayForString(String inputString)
178 throws CharacterCodingException {
179
180 return getByteArrayForCharacterArray(inputString.toCharArray());
181 }
182
183 /**
184 * Creates a byte array representing the input character array. The size of
185 * the array returned is twice that of the array input.
186 *
187 * @param inputArray
188 * The array to convert
189 * @return An array containing the bytes for the characters
190 * @throws CharacterCodingException
191 * If there is an exception encoding the characters.
192 */
193 public static byte[] getByteArrayForCharacterArray(char[] inputArray)
194 throws CharacterCodingException {
195
196 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
197 CharBuffer charBuffer = CharBuffer.wrap(inputArray);
198 ByteBuffer encodedChars = encoder.encode(charBuffer);
199
200 byte[] returnArray = new byte[encodedChars.limit()];
201 encodedChars.get(returnArray);
202
203 return returnArray;
204 }
205
206 /**
207 * Decodes a string value from the byte Array passed in.
208 *
209 * @param inputArr
210 * The input array
211 * @param offset
212 * The position to start reading from
213 * @param length
214 * The length to read upto
215 * @return The string read
216 * @throws CharacterCodingException
217 * If there is an exception decoding the characters.
218 */
219 public static String readStringFromByteArray(byte[] inputArr, int offset,
220 int length) throws CharacterCodingException {
221
222 CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
223 ByteBuffer inputBuffer = ByteBuffer.wrap(inputArr, offset, length);
224 CharBuffer result = decoder.decode(inputBuffer);
225
226 return result.toString();
227 }
228 }