]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/bytes/ByteArrays.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / ByteArrays.java
index 1f98f9da54e310f98333f8adce07eefe1e4e50b4..2776ef292b1c976ad8b9e3bd8809002a9ed999ea 100644 (file)
-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+/*
+ * Created on Jan 21, 2005
+ * 
+ * Copyright Toni Kalajainen
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.simantics.utils.bytes;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.simantics.utils.strings.FileNameUtils;
+
+
+/**
+ * Byte array operations
+ * 
+ * @author Toni Kalajainen
+ */
+public class ByteArrays {
+
+    /**
+     * Copy the contents of source array to dst array
+     * @param src
+     * @param dest
+     */
+    public static void move(byte[] src, byte[] dest)
+    {
+        System.arraycopy(src, 0, dest, 0, src.length);
+    }
+
+    public static byte[] mid(byte[] src, int pos, int len)
+    {
+        if (pos+len > src.length)
+            throw new IndexOutOfBoundsException();
+
+        byte result[] = new byte[len];
+        System.arraycopy(src, pos, result, 0, len);
+        return result;
+    }
+
+    /**
+     * copy byte array into another starting from the given position.
+     * @param src
+     * @param dest
+     * @param pos
+     * @param length
+     */
+    public static void move(byte[] src, byte[] dest, int pos, int length)
+    {
+        System.arraycopy(src, 0, dest, pos, length);
+    }
+
+    /**
+     * Merge two arrays into one
+     * @param src
+     * @param src2
+     * @return
+     */
+    public static byte[] merge(byte[] src, byte[] src2)
+    {
+        byte result[] = new byte[src.length+src2.length];
+        System.arraycopy(src, 0, result, 0, src.length);
+        System.arraycopy(src2, 0, result, src.length, src2.length);
+        return result;
+    }
+
+
+    /**
+     * Merge 3 arrays into one
+     * @param src
+     * @param src2
+     * @param src3
+     * @return
+     */
+    public static byte[] merge(byte[] src, byte[] src2, byte[] src3)
+    {
+        byte result[] = new byte[src.length+src2.length+src3.length];
+        System.arraycopy(src, 0, result, 0, src.length);
+        System.arraycopy(src2, 0, result, src.length, src2.length);
+        System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);
+        return result;
+    }
+
+
+    /**
+     * Merge 4 arrays into one
+     * @param src
+     * @param src2
+     * @param src3
+     * @param src4
+     * @return
+     */
+    public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4)
+    {
+        byte result[] = new byte[src.length+src2.length+src3.length+src4.length];
+        System.arraycopy(src, 0, result, 0, src.length);
+        System.arraycopy(src2, 0, result, src.length, src2.length);
+        System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);
+        System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);
+        return result;
+    }
+
+    /**
+     * Merge 5 arrays into one
+     * @param src
+     * @param src2
+     * @param src3
+     * @param src4
+     * @param src5
+     * @return
+     */
+    public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4, byte[] src5)
+    {
+        byte result[] = new byte[src.length+src2.length+src3.length+src4.length+src5.length];
+        System.arraycopy(src, 0, result, 0, src.length);
+        System.arraycopy(src2, 0, result, src.length, src2.length);
+        System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);
+        System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);
+        System.arraycopy(src5, 0, result, src.length+src2.length+src3.length+src4.length, src5.length);
+        return result;
+    }
+
+    /**
+     * Swap Little-endian and Big-endian integer byte order
+     * @param i
+     * @return
+     */
+    public static int swap(int i) {
+        int byte0 = i & 0xff;
+        int byte1 = (i>>8) & 0xff;
+        int byte2 = (i>>16) & 0xff;
+        int byte3 = (i>>24) & 0xff;
+        // swap the byte order
+        return (byte0<<24) | (byte1<<16) | (byte2<<8) | byte3;
+    }
+
+    public static void saveToFile(byte data[], String filename)
+    throws IOException
+    {
+        // Create directories
+        String dir = FileNameUtils.extractFileDir(filename);
+        (new File(dir)).mkdirs();
+        FileOutputStream out = new FileOutputStream(filename);
+        out.write(data);
+        out.flush();
+        out.close();
+    }
+
+    /**
+     * Test cases
+     * 
+     * @param args
+     */
+    public static void main(String[] args) {
+        byte array1[] = BEInt.toBytes(5);
+        byte array2[] = BEInt.toBytes(10);
+        byte array[] = merge(array1, array2);
+        printByteArray(array);
+        System.out.println();
+    }
+
+    public static void printByteArray(byte array[]) {
+        for (int i=0; i<array.length; i++) {
+            System.out.print(array[i] & 0xff);
+            if (i<array.length-1)
+                System.out.print(",");
+        }
+    }
+}