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