]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/bytes/ByteArrays.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / ByteArrays.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/bytes/ByteArrays.java b/bundles/org.simantics.utils/src/org/simantics/utils/bytes/ByteArrays.java
new file mode 100644 (file)
index 0000000..1f98f9d
--- /dev/null
@@ -0,0 +1,192 @@
+/*******************************************************************************\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
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+import org.simantics.utils.strings.FileNameUtils;\r
+\r
+\r
+/**\r
+ * Byte array operations\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class ByteArrays {\r
+\r
+    /**\r
+     * Copy the contents of source array to dst array\r
+     * @param src\r
+     * @param dest\r
+     */\r
+    public static void move(byte[] src, byte[] dest)\r
+    {\r
+        System.arraycopy(src, 0, dest, 0, src.length);\r
+    }\r
+\r
+    public static byte[] mid(byte[] src, int pos, int len)\r
+    {\r
+        if (pos+len > src.length)\r
+            throw new IndexOutOfBoundsException();\r
+\r
+        byte result[] = new byte[len];\r
+        System.arraycopy(src, pos, result, 0, len);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * copy byte array into another starting from the given position.\r
+     * @param src\r
+     * @param dest\r
+     * @param pos\r
+     * @param length\r
+     */\r
+    public static void move(byte[] src, byte[] dest, int pos, int length)\r
+    {\r
+        System.arraycopy(src, 0, dest, pos, length);\r
+    }\r
+\r
+    /**\r
+     * Merge two arrays into one\r
+     * @param src\r
+     * @param src2\r
+     * @return\r
+     */\r
+    public static byte[] merge(byte[] src, byte[] src2)\r
+    {\r
+        byte result[] = new byte[src.length+src2.length];\r
+        System.arraycopy(src, 0, result, 0, src.length);\r
+        System.arraycopy(src2, 0, result, src.length, src2.length);\r
+        return result;\r
+    }\r
+\r
+\r
+    /**\r
+     * Merge 3 arrays into one\r
+     * @param src\r
+     * @param src2\r
+     * @param src3\r
+     * @return\r
+     */\r
+    public static byte[] merge(byte[] src, byte[] src2, byte[] src3)\r
+    {\r
+        byte result[] = new byte[src.length+src2.length+src3.length];\r
+        System.arraycopy(src, 0, result, 0, src.length);\r
+        System.arraycopy(src2, 0, result, src.length, src2.length);\r
+        System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);\r
+        return result;\r
+    }\r
+\r
+\r
+    /**\r
+     * Merge 4 arrays into one\r
+     * @param src\r
+     * @param src2\r
+     * @param src3\r
+     * @param src4\r
+     * @return\r
+     */\r
+    public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4)\r
+    {\r
+        byte result[] = new byte[src.length+src2.length+src3.length+src4.length];\r
+        System.arraycopy(src, 0, result, 0, src.length);\r
+        System.arraycopy(src2, 0, result, src.length, src2.length);\r
+        System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);\r
+        System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Merge 5 arrays into one\r
+     * @param src\r
+     * @param src2\r
+     * @param src3\r
+     * @param src4\r
+     * @param src5\r
+     * @return\r
+     */\r
+    public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4, byte[] src5)\r
+    {\r
+        byte result[] = new byte[src.length+src2.length+src3.length+src4.length+src5.length];\r
+        System.arraycopy(src, 0, result, 0, src.length);\r
+        System.arraycopy(src2, 0, result, src.length, src2.length);\r
+        System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);\r
+        System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);\r
+        System.arraycopy(src5, 0, result, src.length+src2.length+src3.length+src4.length, src5.length);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * Swap Little-endian and Big-endian integer byte order\r
+     * @param i\r
+     * @return\r
+     */\r
+    public static int swap(int i) {\r
+        int byte0 = i & 0xff;\r
+        int byte1 = (i>>8) & 0xff;\r
+        int byte2 = (i>>16) & 0xff;\r
+        int byte3 = (i>>24) & 0xff;\r
+        // swap the byte order\r
+        return (byte0<<24) | (byte1<<16) | (byte2<<8) | byte3;\r
+    }\r
+\r
+    public static void saveToFile(byte data[], String filename)\r
+    throws IOException\r
+    {\r
+        // Create directories\r
+        String dir = FileNameUtils.extractFileDir(filename);\r
+        (new File(dir)).mkdirs();\r
+        FileOutputStream out = new FileOutputStream(filename);\r
+        out.write(data);\r
+        out.flush();\r
+        out.close();\r
+    }\r
+\r
+    /**\r
+     * Test cases\r
+     * \r
+     * @param args\r
+     */\r
+    public static void main(String[] args) {\r
+        byte array1[] = BEInt.toBytes(5);\r
+        byte array2[] = BEInt.toBytes(10);\r
+        byte array[] = merge(array1, array2);\r
+        printByteArray(array);\r
+        System.out.println();\r
+    }\r
+\r
+    public static void printByteArray(byte array[]) {\r
+        for (int 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