]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 /*\r
12  * Created on Jan 21, 2005\r
13  * \r
14  * Copyright Toni Kalajainen\r
15  * \r
16  * Licensed under the Apache License, Version 2.0 (the "License");\r
17  * you may not use this file except in compliance with the License.\r
18  * You may obtain a copy of the License at\r
19  *\r
20  *     http://www.apache.org/licenses/LICENSE-2.0\r
21  *\r
22  * Unless required by applicable law or agreed to in writing, software\r
23  * distributed under the License is distributed on an "AS IS" BASIS,\r
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
25  * See the License for the specific language governing permissions and\r
26  * limitations under the License.\r
27  */\r
28 package org.simantics.utils.bytes;\r
29 \r
30 import java.io.File;\r
31 import java.io.FileOutputStream;\r
32 import java.io.IOException;\r
33 \r
34 import org.simantics.utils.strings.FileNameUtils;\r
35 \r
36 \r
37 /**\r
38  * Byte array operations\r
39  * \r
40  * @author Toni Kalajainen\r
41  */\r
42 public class ByteArrays {\r
43 \r
44     /**\r
45      * Copy the contents of source array to dst array\r
46      * @param src\r
47      * @param dest\r
48      */\r
49     public static void move(byte[] src, byte[] dest)\r
50     {\r
51         System.arraycopy(src, 0, dest, 0, src.length);\r
52     }\r
53 \r
54     public static byte[] mid(byte[] src, int pos, int len)\r
55     {\r
56         if (pos+len > src.length)\r
57             throw new IndexOutOfBoundsException();\r
58 \r
59         byte result[] = new byte[len];\r
60         System.arraycopy(src, pos, result, 0, len);\r
61         return result;\r
62     }\r
63 \r
64     /**\r
65      * copy byte array into another starting from the given position.\r
66      * @param src\r
67      * @param dest\r
68      * @param pos\r
69      * @param length\r
70      */\r
71     public static void move(byte[] src, byte[] dest, int pos, int length)\r
72     {\r
73         System.arraycopy(src, 0, dest, pos, length);\r
74     }\r
75 \r
76     /**\r
77      * Merge two arrays into one\r
78      * @param src\r
79      * @param src2\r
80      * @return\r
81      */\r
82     public static byte[] merge(byte[] src, byte[] src2)\r
83     {\r
84         byte result[] = new byte[src.length+src2.length];\r
85         System.arraycopy(src, 0, result, 0, src.length);\r
86         System.arraycopy(src2, 0, result, src.length, src2.length);\r
87         return result;\r
88     }\r
89 \r
90 \r
91     /**\r
92      * Merge 3 arrays into one\r
93      * @param src\r
94      * @param src2\r
95      * @param src3\r
96      * @return\r
97      */\r
98     public static byte[] merge(byte[] src, byte[] src2, byte[] src3)\r
99     {\r
100         byte result[] = new byte[src.length+src2.length+src3.length];\r
101         System.arraycopy(src, 0, result, 0, src.length);\r
102         System.arraycopy(src2, 0, result, src.length, src2.length);\r
103         System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);\r
104         return result;\r
105     }\r
106 \r
107 \r
108     /**\r
109      * Merge 4 arrays into one\r
110      * @param src\r
111      * @param src2\r
112      * @param src3\r
113      * @param src4\r
114      * @return\r
115      */\r
116     public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4)\r
117     {\r
118         byte result[] = new byte[src.length+src2.length+src3.length+src4.length];\r
119         System.arraycopy(src, 0, result, 0, src.length);\r
120         System.arraycopy(src2, 0, result, src.length, src2.length);\r
121         System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);\r
122         System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);\r
123         return result;\r
124     }\r
125 \r
126     /**\r
127      * Merge 5 arrays into one\r
128      * @param src\r
129      * @param src2\r
130      * @param src3\r
131      * @param src4\r
132      * @param src5\r
133      * @return\r
134      */\r
135     public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4, byte[] src5)\r
136     {\r
137         byte result[] = new byte[src.length+src2.length+src3.length+src4.length+src5.length];\r
138         System.arraycopy(src, 0, result, 0, src.length);\r
139         System.arraycopy(src2, 0, result, src.length, src2.length);\r
140         System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);\r
141         System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);\r
142         System.arraycopy(src5, 0, result, src.length+src2.length+src3.length+src4.length, src5.length);\r
143         return result;\r
144     }\r
145 \r
146     /**\r
147      * Swap Little-endian and Big-endian integer byte order\r
148      * @param i\r
149      * @return\r
150      */\r
151     public static int swap(int i) {\r
152         int byte0 = i & 0xff;\r
153         int byte1 = (i>>8) & 0xff;\r
154         int byte2 = (i>>16) & 0xff;\r
155         int byte3 = (i>>24) & 0xff;\r
156         // swap the byte order\r
157         return (byte0<<24) | (byte1<<16) | (byte2<<8) | byte3;\r
158     }\r
159 \r
160     public static void saveToFile(byte data[], String filename)\r
161     throws IOException\r
162     {\r
163         // Create directories\r
164         String dir = FileNameUtils.extractFileDir(filename);\r
165         (new File(dir)).mkdirs();\r
166         FileOutputStream out = new FileOutputStream(filename);\r
167         out.write(data);\r
168         out.flush();\r
169         out.close();\r
170     }\r
171 \r
172     /**\r
173      * Test cases\r
174      * \r
175      * @param args\r
176      */\r
177     public static void main(String[] args) {\r
178         byte array1[] = BEInt.toBytes(5);\r
179         byte array2[] = BEInt.toBytes(10);\r
180         byte array[] = merge(array1, array2);\r
181         printByteArray(array);\r
182         System.out.println();\r
183     }\r
184 \r
185     public static void printByteArray(byte array[]) {\r
186         for (int i=0; i<array.length; i++) {\r
187             System.out.print(array[i] & 0xff);\r
188             if (i<array.length-1)\r
189                 System.out.print(",");\r
190         }\r
191     }\r
192 }\r