]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/BEShort.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / BEShort.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  * Big Endian short <-> byte array conversions\r
32  * Motorola order, Network order\r
33  *\r
34  * @author Toni Kalajainen\r
35  */\r
36 public class BEShort {\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[0] = (byte) (value & 0xff);\r
47                 array[1] = (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[0 + offset] = (byte) (value & 0xff);\r
62                 array[1 + 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[0] = (byte) (value & 0xff);\r
76                 array[1] = (byte) (value >> 8);\r
77         }       \r
78         /**\r
79          * read short value from byte array\r
80          * @param array the array\r
81          * @param offset offset\r
82          * @return the value\r
83          */\r
84         public static short toShort(byte array[], int offset)\r
85         {\r
86                 if (offset+2>array.length)\r
87                         throw new IndexOutOfBoundsException();          \r
88                 short value = (short)\r
89                 (\r
90                         ( ((short) array[0 + offset] & 0xFF) ) |\r
91                         ( ((short) array[1 + offset] & 0xFF) << 8)\r
92                 );\r
93                 return value;\r
94         }\r
95 \r
96         /**\r
97          * read short value from byte array\r
98          * @param array the array\r
99          * @return the value\r
100          */\r
101         public static short toShort(byte array[])\r
102         {\r
103                 if (2>array.length)\r
104                         throw new IndexOutOfBoundsException();          \r
105                 short value = (short)\r
106                 (\r
107                         ( ((short) array[0] & 0xFF) ) |\r
108                         ( ((short) array[1] & 0xFF) << 8)\r
109                 );\r
110                 return value;\r
111         }\r
112 \r
113         /**\r
114          * read short value from byte array\r
115          * @param array the array\r
116          * @param offset offset\r
117          * @return the value\r
118          */\r
119         public static int toInt(byte array[], int offset)\r
120         {\r
121                 if (offset+2>array.length)\r
122                         throw new IndexOutOfBoundsException();          \r
123                 short value = (short)\r
124                 (\r
125                         ( ((short) array[0 + offset] & 0xFF) ) |\r
126                         ( ((short) array[1 + offset] & 0xFF) << 8)\r
127                 );\r
128                 return value;\r
129         }\r
130         /**\r
131          * Test cases\r
132          * @param args\r
133          */\r
134         public static void main(String[] args) {\r
135                 System.out.println("min="+Short.MIN_VALUE+" max="+Short.MAX_VALUE);\r
136                 short value = (short) -513;\r
137                 byte array[] = toBytes(value);\r
138                 System.out.print(value);\r
139                 System.out.print(" = ");\r
140                 printByteArray(array);\r
141                 System.out.println();\r
142                 \r
143                 write(value, array, 0);\r
144                 System.out.print(value);\r
145                 System.out.print(" = ");\r
146                 printByteArray(array);\r
147                 System.out.println();\r
148                 \r
149                 write(value, array);\r
150                 System.out.print(value);\r
151                 System.out.print(" = ");\r
152                 printByteArray(array);\r
153                 System.out.println();\r
154                 \r
155                 value = toShort(array, 0);\r
156                 printByteArray(array);\r
157                 System.out.print(" = ");\r
158                 System.out.print(value);\r
159                 System.out.println();\r
160                                 \r
161                 value = toShort(array);\r
162                 printByteArray(array);\r
163                 System.out.print(" = ");\r
164                 System.out.print(value);\r
165                 System.out.println();\r
166         }\r
167         \r
168         public static void printByteArray(byte array[]) {\r
169                 for (short i=0; i<array.length; i++) {\r
170                         System.out.print(array[i] & 0xff);\r
171                         if (i<array.length-1) \r
172                                 System.out.print(",");\r
173                 }\r
174         }\r
175 }\r