]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33
34 import org.simantics.utils.strings.FileNameUtils;
35
36
37 /**
38  * Byte array operations
39  * 
40  * @author Toni Kalajainen
41  */
42 public class ByteArrays {
43
44     /**
45      * Copy the contents of source array to dst array
46      * @param src
47      * @param dest
48      */
49     public static void move(byte[] src, byte[] dest)
50     {
51         System.arraycopy(src, 0, dest, 0, src.length);
52     }
53
54     public static byte[] mid(byte[] src, int pos, int len)
55     {
56         if (pos+len > src.length)
57             throw new IndexOutOfBoundsException();
58
59         byte result[] = new byte[len];
60         System.arraycopy(src, pos, result, 0, len);
61         return result;
62     }
63
64     /**
65      * copy byte array into another starting from the given position.
66      * @param src
67      * @param dest
68      * @param pos
69      * @param length
70      */
71     public static void move(byte[] src, byte[] dest, int pos, int length)
72     {
73         System.arraycopy(src, 0, dest, pos, length);
74     }
75
76     /**
77      * Merge two arrays into one
78      * @param src
79      * @param src2
80      * @return
81      */
82     public static byte[] merge(byte[] src, byte[] src2)
83     {
84         byte result[] = new byte[src.length+src2.length];
85         System.arraycopy(src, 0, result, 0, src.length);
86         System.arraycopy(src2, 0, result, src.length, src2.length);
87         return result;
88     }
89
90
91     /**
92      * Merge 3 arrays into one
93      * @param src
94      * @param src2
95      * @param src3
96      * @return
97      */
98     public static byte[] merge(byte[] src, byte[] src2, byte[] src3)
99     {
100         byte result[] = new byte[src.length+src2.length+src3.length];
101         System.arraycopy(src, 0, result, 0, src.length);
102         System.arraycopy(src2, 0, result, src.length, src2.length);
103         System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);
104         return result;
105     }
106
107
108     /**
109      * Merge 4 arrays into one
110      * @param src
111      * @param src2
112      * @param src3
113      * @param src4
114      * @return
115      */
116     public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4)
117     {
118         byte result[] = new byte[src.length+src2.length+src3.length+src4.length];
119         System.arraycopy(src, 0, result, 0, src.length);
120         System.arraycopy(src2, 0, result, src.length, src2.length);
121         System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);
122         System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);
123         return result;
124     }
125
126     /**
127      * Merge 5 arrays into one
128      * @param src
129      * @param src2
130      * @param src3
131      * @param src4
132      * @param src5
133      * @return
134      */
135     public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4, byte[] src5)
136     {
137         byte result[] = new byte[src.length+src2.length+src3.length+src4.length+src5.length];
138         System.arraycopy(src, 0, result, 0, src.length);
139         System.arraycopy(src2, 0, result, src.length, src2.length);
140         System.arraycopy(src3, 0, result, src.length+src2.length, src3.length);
141         System.arraycopy(src4, 0, result, src.length+src2.length+src3.length, src4.length);
142         System.arraycopy(src5, 0, result, src.length+src2.length+src3.length+src4.length, src5.length);
143         return result;
144     }
145
146     /**
147      * Swap Little-endian and Big-endian integer byte order
148      * @param i
149      * @return
150      */
151     public static int swap(int i) {
152         int byte0 = i & 0xff;
153         int byte1 = (i>>8) & 0xff;
154         int byte2 = (i>>16) & 0xff;
155         int byte3 = (i>>24) & 0xff;
156         // swap the byte order
157         return (byte0<<24) | (byte1<<16) | (byte2<<8) | byte3;
158     }
159
160     public static void saveToFile(byte data[], String filename)
161     throws IOException
162     {
163         // Create directories
164         String dir = FileNameUtils.extractFileDir(filename);
165         (new File(dir)).mkdirs();
166         FileOutputStream out = new FileOutputStream(filename);
167         out.write(data);
168         out.flush();
169         out.close();
170     }
171
172     /**
173      * Test cases
174      * 
175      * @param args
176      */
177     public static void main(String[] args) {
178         byte array1[] = BEInt.toBytes(5);
179         byte array2[] = BEInt.toBytes(10);
180         byte array[] = merge(array1, array2);
181         printByteArray(array);
182         System.out.println();
183     }
184
185     public static void printByteArray(byte array[]) {
186         for (int i=0; i<array.length; i++) {
187             System.out.print(array[i] & 0xff);
188             if (i<array.length-1)
189                 System.out.print(",");
190         }
191     }
192 }