]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/BELong.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / BELong.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 long <-> byte array conversions\r
32  * Motorola order, Network order\r
33  *\r
34  * @author Toni Kalajainen\r
35  */\r
36 public class BELong {\r
37         \r
38         /**\r
39          * Convert long to byte array\r
40          * @param l long value\r
41          * @return byte array\r
42          */\r
43         public static byte[] toBytes(long value)\r
44         {\r
45                 byte array[] = new byte[8];\r
46                 array[0] = (byte) (value & 0xff);\r
47                 array[1] = (byte) ((value >> 8) & 0xff);\r
48                 array[2] = (byte) ((value >> 16) & 0xff);\r
49                 array[3] = (byte) ((value >> 24) & 0xff);\r
50                 array[4] = (byte) ((value >> 32) & 0xff);\r
51                 array[5] = (byte) ((value >> 40) & 0xff);\r
52                 array[6] = (byte) ((value >> 48) & 0xff);\r
53                 array[7] = (byte) ((value >> 56) & 0xff);\r
54                 return array;\r
55         }\r
56         \r
57         /**\r
58          * Write long value to byte array\r
59          * @param value the long value\r
60          * @param array the byte array\r
61          * @param offset the offset\r
62          */\r
63         public static void write(long value, byte array[], int offset)\r
64         {\r
65                 if (offset+8>array.length)\r
66                         throw new IndexOutOfBoundsException();          \r
67                 array[0 + offset] = (byte) (value & 0xff);\r
68                 array[1 + offset] = (byte) (value >> 8);\r
69                 array[2 + offset] = (byte) (value >> 16);\r
70                 array[3 + offset] = (byte) (value >> 24);\r
71                 array[4 + offset] = (byte) (value >> 32);\r
72                 array[5 + offset] = (byte) (value >> 40);\r
73                 array[6 + offset] = (byte) (value >> 48);\r
74                 array[7 + offset] = (byte) (value >> 56);\r
75         }\r
76         \r
77         /**\r
78          * Write long value to byte array\r
79          * @param value the long value\r
80          * @param array the byte array\r
81          * @param offset the offset\r
82          */\r
83         public static void write(long value, byte array[])\r
84         {\r
85                 if (array.length<8)\r
86                         throw new IndexOutOfBoundsException();          \r
87                 array[0] = (byte) (value & 0xff);\r
88                 array[1] = (byte) (value >> 8);\r
89                 array[2] = (byte) (value >> 16);\r
90                 array[3] = (byte) (value >> 24);\r
91                 array[4] = (byte) (value >> 32);\r
92                 array[5] = (byte) (value >> 40);\r
93                 array[6] = (byte) (value >> 48);\r
94                 array[7] = (byte) (value >> 56);\r
95         }       \r
96         /**\r
97          * read long value from byte array\r
98          * @param array the array\r
99          * @param offset offset\r
100          * @return the value\r
101          */\r
102         public static long toLong(byte array[], int offset)\r
103         {\r
104                 if (offset+8>array.length)\r
105                         throw new IndexOutOfBoundsException();          \r
106                 long value = \r
107                         ( ((long) array[0 + offset] & 0xFF) ) |\r
108                         ( ((long) array[1 + offset] & 0xFF) << 8) |\r
109                         ( ((long) array[2 + offset] & 0xFF) << 16) | \r
110                         ( ((long) array[3 + offset] & 0xFF) << 24) | \r
111                         ( ((long) array[4 + offset] & 0xFF) << 32) | \r
112                         ( ((long) array[5 + offset] & 0xFF) << 40) | \r
113                         ( ((long) array[6 + offset] & 0xFF) << 48) | \r
114                         ( ((long) array[7 + offset] & 0xFF) << 56);\r
115                 return value;\r
116         }\r
117         /**\r
118          * read long value from byte array\r
119          * @param array the array\r
120          * @return the value\r
121          */\r
122         public static long toLong(byte array[])\r
123         {\r
124                 if (8>array.length)\r
125                         throw new IndexOutOfBoundsException();          \r
126                 long value = \r
127                         ( ((long) array[0] & 0xFF) ) |\r
128                         ( ((long) array[1] & 0xFF) << 8) |\r
129                         ( ((long) array[2] & 0xFF) << 16) | \r
130                         ( ((long) array[3] & 0xFF) << 24) | \r
131                         ( ((long) array[4] & 0xFF) << 32) | \r
132                         ( ((long) array[5] & 0xFF) << 40) | \r
133                         ( ((long) array[6] & 0xFF) << 48) | \r
134                         ( ((long) array[7] & 0xFF) << 56);\r
135                 return value;\r
136         }\r
137 \r
138         /**\r
139          * Test cases\r
140          * @param args\r
141          */\r
142         public static void main(String[] args) {\r
143                 System.out.println("min="+Long.MIN_VALUE+" max="+Long.MAX_VALUE);\r
144                 long value = -1290000000;\r
145                 byte array[] = toBytes(value);\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, 0);\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 = toLong(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 = toLong(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         \r
171         public static void printByteArray(byte array[]) {\r
172                 for (int i=0; i<array.length; i++) {\r
173                         System.out.print(array[i] & 0xff);\r
174                         if (i<array.length-1) \r
175                                 System.out.print(",");\r
176                 }\r
177         }\r
178 }\r