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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
12 * Created on Jan 21, 2005
\r
14 * Copyright Toni Kalajainen
\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
20 * http://www.apache.org/licenses/LICENSE-2.0
\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
28 package org.simantics.utils.bytes;
\r
30 import java.io.File;
\r
31 import java.io.FileOutputStream;
\r
32 import java.io.IOException;
\r
34 import org.simantics.utils.strings.FileNameUtils;
\r
38 * Byte array operations
\r
40 * @author Toni Kalajainen
\r
42 public class ByteArrays {
\r
45 * Copy the contents of source array to dst array
\r
49 public static void move(byte[] src, byte[] dest)
\r
51 System.arraycopy(src, 0, dest, 0, src.length);
\r
54 public static byte[] mid(byte[] src, int pos, int len)
\r
56 if (pos+len > src.length)
\r
57 throw new IndexOutOfBoundsException();
\r
59 byte result[] = new byte[len];
\r
60 System.arraycopy(src, pos, result, 0, len);
\r
65 * copy byte array into another starting from the given position.
\r
71 public static void move(byte[] src, byte[] dest, int pos, int length)
\r
73 System.arraycopy(src, 0, dest, pos, length);
\r
77 * Merge two arrays into one
\r
82 public static byte[] merge(byte[] src, byte[] src2)
\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
92 * Merge 3 arrays into one
\r
98 public static byte[] merge(byte[] src, byte[] src2, byte[] src3)
\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
109 * Merge 4 arrays into one
\r
116 public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4)
\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
127 * Merge 5 arrays into one
\r
135 public static byte[] merge(byte[] src, byte[] src2, byte[] src3, byte[] src4, byte[] src5)
\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
147 * Swap Little-endian and Big-endian integer byte order
\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
160 public static void saveToFile(byte data[], String filename)
\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
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
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