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