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