]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEShort.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / LEShort.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEShort.java b/bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEShort.java
new file mode 100644 (file)
index 0000000..e69ea3d
--- /dev/null
@@ -0,0 +1,177 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * Created on Jan 21, 2005\r
+ * \r
+ * Copyright Toni Kalajainen\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.simantics.utils.bytes;\r
+\r
+/**\r
+ * Little Endian short <-> byte array conversions\r
+ * Intel order\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+public class LEShort {\r
+       \r
+       /**\r
+        * Convert short to byte array\r
+        * @param l short value\r
+        * @return byte array\r
+        */\r
+       public static byte[] toBytes(short value)\r
+       {\r
+               byte array[] = new byte[2];\r
+               array[1] = (byte) (value & 0xff);\r
+               array[0] = (byte) ((value >> 8) & 0xff);\r
+               return array;\r
+       }\r
+       \r
+       /**\r
+        * Write short value to byte array\r
+        * @param value the short value\r
+        * @param array the byte array\r
+        * @param offset the offset\r
+        */\r
+       public static void write(short value, byte array[], int offset)\r
+       {\r
+               if (offset+2>array.length)\r
+                       throw new IndexOutOfBoundsException();          \r
+               array[1 + offset] = (byte) (value & 0xff);\r
+               array[0 + offset] = (byte) (value >> 8);\r
+       }\r
+       \r
+       /**\r
+        * Write short value to byte array\r
+        * @param value the short value\r
+        * @param array the byte array\r
+        * @param offset the offset\r
+        */\r
+       public static void write(short value, byte array[])\r
+       {\r
+               if (array.length<2)\r
+                       throw new IndexOutOfBoundsException();          \r
+               array[1] = (byte) (value & 0xff);\r
+               array[0] = (byte) (value >> 8);\r
+       }       \r
+       \r
+       /**\r
+        * read short value from byte array\r
+        * @param array the array\r
+        * @param offset offset\r
+        * @return the value\r
+        */\r
+       public static short toShort(byte array[], int offset)\r
+       {\r
+               if (offset+2>array.length)\r
+                       throw new IndexOutOfBoundsException();          \r
+               short value = (short)\r
+               (\r
+                       ( ((short) array[1 + offset] & 0xFF) ) |\r
+                       ( ((short) array[0 + offset] & 0xFF) << 8)\r
+               );\r
+               return value;\r
+       }\r
+\r
+       /**\r
+        * read short value from byte array\r
+        * @param array the array\r
+        * @param offset offset\r
+        * @return the value\r
+        */\r
+       public static int toInt(byte array[], int offset)\r
+       {\r
+               if (offset+2>array.length)\r
+                       throw new IndexOutOfBoundsException();          \r
+               short value = (short)\r
+               (\r
+                       ( ((short) array[1 + offset] & 0xFF) ) |\r
+                       ( ((short) array[0 + offset] & 0xFF) << 8)\r
+               );\r
+               return value;\r
+       }\r
+       \r
+       /**\r
+        * read short value from byte array\r
+        * @param array the array\r
+        * @return the value\r
+        */\r
+       public static short toShort(byte array[])\r
+       {\r
+               if (2>array.length)\r
+                       throw new IndexOutOfBoundsException();          \r
+               short value = (short)\r
+               (\r
+                       ( ((short) array[1] & 0xFF) ) |\r
+                       ( ((short) array[0] & 0xFF) << 8)\r
+               );\r
+               return value;\r
+       }\r
+       \r
+       /**\r
+        * Test cases\r
+        * @param args\r
+        */\r
+       public static void main(String[] args) {\r
+               System.out.println("min="+Short.MIN_VALUE+" max="+Short.MAX_VALUE);\r
+               short value = (short) -513;\r
+               byte array[] = toBytes(value);\r
+               System.out.print(value);\r
+               System.out.print(" = ");\r
+               printByteArray(array);\r
+               System.out.println();\r
+               \r
+               write(value, array, 0);\r
+               System.out.print(value);\r
+               System.out.print(" = ");\r
+               printByteArray(array);\r
+               System.out.println();\r
+               \r
+               write(value, array);\r
+               System.out.print(value);\r
+               System.out.print(" = ");\r
+               printByteArray(array);\r
+               System.out.println();\r
+               \r
+               value = toShort(array, 0);\r
+               printByteArray(array);\r
+               System.out.print(" = ");\r
+               System.out.print(value);\r
+               System.out.println();\r
+                               \r
+               value = toShort(array);\r
+               printByteArray(array);\r
+               System.out.print(" = ");\r
+               System.out.print(value);\r
+               System.out.println();\r
+       }\r
+       \r
+       public static void printByteArray(byte array[]) {\r
+               for (short i=0; i<array.length; i++) {\r
+                       System.out.print(array[i] & 0xff);\r
+                       if (i<array.length-1) \r
+                               System.out.print(",");\r
+               }\r
+       }\r
+}\r