]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/BEShort.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / BEShort.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 short <-> byte array conversions
32  * Motorola order, Network order
33  *
34  * @author Toni Kalajainen
35  */
36 public class BEShort {
37         
38         /**
39          * Convert short to byte array
40          * @param l short value
41          * @return byte array
42          */
43         public static byte[] toBytes(short value)
44         {
45                 byte array[] = new byte[2];
46                 array[0] = (byte) (value & 0xff);
47                 array[1] = (byte) ((value >> 8) & 0xff);
48                 return array;
49         }
50         
51         /**
52          * Write short value to byte array
53          * @param value the short value
54          * @param array the byte array
55          * @param offset the offset
56          */
57         public static void write(short value, byte array[], int offset)
58         {
59                 if (offset+2>array.length)
60                         throw new IndexOutOfBoundsException();          
61                 array[0 + offset] = (byte) (value & 0xff);
62                 array[1 + offset] = (byte) (value >> 8);
63         }
64         
65         /**
66          * Write short value to byte array
67          * @param value the short value
68          * @param array the byte array
69          * @param offset the offset
70          */
71         public static void write(short value, byte array[])
72         {
73                 if (array.length<2)
74                         throw new IndexOutOfBoundsException();          
75                 array[0] = (byte) (value & 0xff);
76                 array[1] = (byte) (value >> 8);
77         }       
78         /**
79          * read short value from byte array
80          * @param array the array
81          * @param offset offset
82          * @return the value
83          */
84         public static short toShort(byte array[], int offset)
85         {
86                 if (offset+2>array.length)
87                         throw new IndexOutOfBoundsException();          
88                 short value = (short)
89                 (
90                         ( ((short) array[0 + offset] & 0xFF) ) |
91                         ( ((short) array[1 + offset] & 0xFF) << 8)
92                 );
93                 return value;
94         }
95
96         /**
97          * read short value from byte array
98          * @param array the array
99          * @return the value
100          */
101         public static short toShort(byte array[])
102         {
103                 if (2>array.length)
104                         throw new IndexOutOfBoundsException();          
105                 short value = (short)
106                 (
107                         ( ((short) array[0] & 0xFF) ) |
108                         ( ((short) array[1] & 0xFF) << 8)
109                 );
110                 return value;
111         }
112
113         /**
114          * read short value from byte array
115          * @param array the array
116          * @param offset offset
117          * @return the value
118          */
119         public static int toInt(byte array[], int offset)
120         {
121                 if (offset+2>array.length)
122                         throw new IndexOutOfBoundsException();          
123                 short value = (short)
124                 (
125                         ( ((short) array[0 + offset] & 0xFF) ) |
126                         ( ((short) array[1 + offset] & 0xFF) << 8)
127                 );
128                 return value;
129         }
130         /**
131          * Test cases
132          * @param args
133          */
134         public static void main(String[] args) {
135                 System.out.println("min="+Short.MIN_VALUE+" max="+Short.MAX_VALUE);
136                 short value = (short) -513;
137                 byte array[] = toBytes(value);
138                 System.out.print(value);
139                 System.out.print(" = ");
140                 printByteArray(array);
141                 System.out.println();
142                 
143                 write(value, array, 0);
144                 System.out.print(value);
145                 System.out.print(" = ");
146                 printByteArray(array);
147                 System.out.println();
148                 
149                 write(value, array);
150                 System.out.print(value);
151                 System.out.print(" = ");
152                 printByteArray(array);
153                 System.out.println();
154                 
155                 value = toShort(array, 0);
156                 printByteArray(array);
157                 System.out.print(" = ");
158                 System.out.print(value);
159                 System.out.println();
160                                 
161                 value = toShort(array);
162                 printByteArray(array);
163                 System.out.print(" = ");
164                 System.out.print(value);
165                 System.out.println();
166         }
167         
168         public static void printByteArray(byte array[]) {
169                 for (short i=0; i<array.length; i++) {
170                         System.out.print(array[i] & 0xff);
171                         if (i<array.length-1) 
172                                 System.out.print(",");
173                 }
174         }
175 }