X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2Fbytes%2FByteArrays.java;fp=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2Fbytes%2FByteArrays.java;h=1f98f9da54e310f98333f8adce07eefe1e4e50b4;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/bytes/ByteArrays.java b/bundles/org.simantics.utils/src/org/simantics/utils/bytes/ByteArrays.java new file mode 100644 index 000000000..1f98f9da5 --- /dev/null +++ b/bundles/org.simantics.utils/src/org/simantics/utils/bytes/ByteArrays.java @@ -0,0 +1,192 @@ +/******************************************************************************* + * 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