]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEShort.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / LEShort.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 /*\r
12  * Created on Jan 21, 2005\r
13  * \r
14  * Copyright Toni Kalajainen\r
15  * \r
16  * Licensed under the Apache License, Version 2.0 (the "License");\r
17  * you may not use this file except in compliance with the License.\r
18  * You may obtain a copy of the License at\r
19  *\r
20  *     http://www.apache.org/licenses/LICENSE-2.0\r
21  *\r
22  * Unless required by applicable law or agreed to in writing, software\r
23  * distributed under the License is distributed on an "AS IS" BASIS,\r
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
25  * See the License for the specific language governing permissions and\r
26  * limitations under the License.\r
27  */\r
28 package org.simantics.utils.bytes;\r
29 \r
30 /**\r
31  * Little Endian short <-> byte array conversions\r
32  * Intel order\r
33  *\r
34  * @author Toni Kalajainen\r
35  */\r
36 public class LEShort {\r
37         \r
38         /**\r
39          * Convert short to byte array\r
40          * @param l short value\r
41          * @return byte array\r
42          */\r
43         public static byte[] toBytes(short value)\r
44         {\r
45                 byte array[] = new byte[2];\r
46                 array[1] = (byte) (value & 0xff);\r
47                 array[0] = (byte) ((value >> 8) & 0xff);\r
48                 return array;\r
49         }\r
50         \r
51         /**\r
52          * Write short value to byte array\r
53          * @param value the short value\r
54          * @param array the byte array\r
55          * @param offset the offset\r
56          */\r
57         public static void write(short value, byte array[], int offset)\r
58         {\r
59                 if (offset+2>array.length)\r
60                         throw new IndexOutOfBoundsException();          \r
61                 array[1 + offset] = (byte) (value & 0xff);\r
62                 array[0 + offset] = (byte) (value >> 8);\r
63         }\r
64         \r
65         /**\r
66          * Write short value to byte array\r
67          * @param value the short value\r
68          * @param array the byte array\r
69          * @param offset the offset\r
70          */\r
71         public static void write(short value, byte array[])\r
72         {\r
73                 if (array.length<2)\r
74                         throw new IndexOutOfBoundsException();          \r
75                 array[1] = (byte) (value & 0xff);\r
76                 array[0] = (byte) (value >> 8);\r
77         }       \r
78         \r
79         /**\r
80          * read short value from byte array\r
81          * @param array the array\r
82          * @param offset offset\r
83          * @return the value\r
84          */\r
85         public static short toShort(byte array[], int offset)\r
86         {\r
87                 if (offset+2>array.length)\r
88                         throw new IndexOutOfBoundsException();          \r
89                 short value = (short)\r
90                 (\r
91                         ( ((short) array[1 + offset] & 0xFF) ) |\r
92                         ( ((short) array[0 + offset] & 0xFF) << 8)\r
93                 );\r
94                 return value;\r
95         }\r
96 \r
97         /**\r
98          * read short value from byte array\r
99          * @param array the array\r
100          * @param offset offset\r
101          * @return the value\r
102          */\r
103         public static int toInt(byte array[], int offset)\r
104         {\r
105                 if (offset+2>array.length)\r
106                         throw new IndexOutOfBoundsException();          \r
107                 short value = (short)\r
108                 (\r
109                         ( ((short) array[1 + offset] & 0xFF) ) |\r
110                         ( ((short) array[0 + offset] & 0xFF) << 8)\r
111                 );\r
112                 return value;\r
113         }\r
114         \r
115         /**\r
116          * read short value from byte array\r
117          * @param array the array\r
118          * @return the value\r
119          */\r
120         public static short toShort(byte array[])\r
121         {\r
122                 if (2>array.length)\r
123                         throw new IndexOutOfBoundsException();          \r
124                 short value = (short)\r
125                 (\r
126                         ( ((short) array[1] & 0xFF) ) |\r
127                         ( ((short) array[0] & 0xFF) << 8)\r
128                 );\r
129                 return value;\r
130         }\r
131         \r
132         /**\r
133          * Test cases\r
134          * @param args\r
135          */\r
136         public static void main(String[] args) {\r
137                 System.out.println("min="+Short.MIN_VALUE+" max="+Short.MAX_VALUE);\r
138                 short value = (short) -513;\r
139                 byte array[] = toBytes(value);\r
140                 System.out.print(value);\r
141                 System.out.print(" = ");\r
142                 printByteArray(array);\r
143                 System.out.println();\r
144                 \r
145                 write(value, array, 0);\r
146                 System.out.print(value);\r
147                 System.out.print(" = ");\r
148                 printByteArray(array);\r
149                 System.out.println();\r
150                 \r
151                 write(value, array);\r
152                 System.out.print(value);\r
153                 System.out.print(" = ");\r
154                 printByteArray(array);\r
155                 System.out.println();\r
156                 \r
157                 value = toShort(array, 0);\r
158                 printByteArray(array);\r
159                 System.out.print(" = ");\r
160                 System.out.print(value);\r
161                 System.out.println();\r
162                                 \r
163                 value = toShort(array);\r
164                 printByteArray(array);\r
165                 System.out.print(" = ");\r
166                 System.out.print(value);\r
167                 System.out.println();\r
168         }\r
169         \r
170         public static void printByteArray(byte array[]) {\r
171                 for (short i=0; i<array.length; i++) {\r
172                         System.out.print(array[i] & 0xff);\r
173                         if (i<array.length-1) \r
174                                 System.out.print(",");\r
175                 }\r
176         }\r
177 }\r