+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * Created on Jan 21, 2005\r
+ * \r
+ * Copyright Toni Kalajainen\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.simantics.utils.bytes;\r
+\r
+import org.simantics.utils.strings.EString;\r
+\r
+/**\r
+ * Byte array <-> string conversions\r
+ * \r
+ * All conversions have length in the beginning (32-bit integer) and\r
+ * followed by actual chars \r
+ * \r
+ * the length is in Big Endian\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+public class BEString {\r
+\r
+ public static byte[] toBytes(String s) {\r
+ int size = s.length();\r
+ byte array[] = new byte[size + 4];\r
+ // write length\r
+ BEInt.write(size, array);\r
+ // write chars\r
+ for (int i=0; i<size; i++)\r
+ array[i+4] = (byte) (s.charAt(i) );\r
+ return array;\r
+ }\r
+ \r
+ /**\r
+ * Write string to byte array\r
+ * @param str the string\r
+ * @param array the byte array\r
+ * @param offset the offset\r
+ */\r
+ public static void write(String str, byte array[], int offset)\r
+ {\r
+ int size = str.length();\r
+ if (offset+4+size>array.length)\r
+ throw new IndexOutOfBoundsException(); \r
+ BEInt.write(size, array, offset);\r
+ for (int i=0; i<size; i++)\r
+ array[i+4+offset] = (byte) (str.charAt(i) ); \r
+ }\r
+ \r
+ /**\r
+ * Write string to byte array\r
+ * @param str the string\r
+ * @param array the byte array\r
+ * @param offset the offset\r
+ */\r
+ public static void write(String str, byte array[])\r
+ {\r
+ int size = str.length();\r
+ if (4+size>array.length)\r
+ throw new IndexOutOfBoundsException(); \r
+ BEInt.write(size, array);\r
+ for (int i=0; i<size; i++)\r
+ array[i+4] = (byte) (str.charAt(i) ); \r
+ }\r
+ \r
+ public static String toString(byte array[], int offset) {\r
+ if (offset+4>array.length)\r
+ throw new IndexOutOfBoundsException();\r
+ // read size\r
+ int size = BEInt.toInt(array, offset);\r
+ if (offset+4+size>array.length)\r
+ throw new IndexOutOfBoundsException();\r
+ // read chars\r
+ //return new String(array, 4+offset, size);\r
+ char chars[] = new char[size];\r
+ for (int i=0; i<size; i++)\r
+ chars[i] = (char) (array[i+4+offset] & 0xff); \r
+ return new String(chars);\r
+ }\r
+ \r
+ public static String toString(byte array[]) {\r
+ if (4>array.length)\r
+ throw new IndexOutOfBoundsException();\r
+ // read size\r
+ int size = BEInt.toInt(array);\r
+ if (4+size>array.length)\r
+ throw new IndexOutOfBoundsException();\r
+ // read chars\r
+ //return new String(array, 4, size);\r
+ char chars[] = new char[size];\r
+ for (int i=0; i<size; i++)\r
+ chars[i] = (char) (array[i+4] & 0xff); \r
+ return new String(chars);\r
+ }\r
+ \r
+ /**\r
+ * Big Endian hex\r
+ * @param value\r
+ * @param decimals\r
+ * @return\r
+ */\r
+ public static String intToHex(int value, int decimals) {\r
+ String result=""; \r
+ for (int i=0; i<decimals; i++) {\r
+ result += EString.HEX_VALUES[(value>>4) & 0xF];\r
+ result += EString.HEX_VALUES[value & 0xF];\r
+ value = value >> 8; \r
+ } \r
+ return result;\r
+ } \r
+ \r
+ public static void main(String[] args) {\r
+ String value = "STRING 01234"+(char)(128)+(char)(129)+(char)(255);\r
+ \r
+ int X = 500;\r
+ System.out.println(X+" = "+intToHex(X, 4)); \r
+ \r
+ byte array[] = toBytes(value); \r
+ System.out.print(value);\r
+ System.out.print(" = ");\r
+ printByteArray(array);\r
+ System.out.println();\r
+ \r
+ write(value, array);\r
+ System.out.print(value);\r
+ System.out.print(" = ");\r
+ printByteArray(array);\r
+ System.out.println();\r
+ \r
+ write(value, array, 0);\r
+ System.out.print(value);\r
+ System.out.print(" = ");\r
+ printByteArray(array);\r
+ System.out.println();\r
+ \r
+ value = toString(array, 0);\r
+ printByteArray(array);\r
+ System.out.print(" = ");\r
+ System.out.print(value);\r
+ System.out.println();\r
+ \r
+ value = toString(array);\r
+ printByteArray(array);\r
+ System.out.print(" = ");\r
+ System.out.print(value);\r
+ System.out.println();\r
+ \r
+ array = toBytes(value); \r
+ System.out.print(value);\r
+ System.out.print(" = ");\r
+ printByteArray(array);\r
+ System.out.println();\r
+ \r
+ \r
+ }\r
+ \r
+ public static void printByteArray(byte array[]) {\r
+ for (int i=0; i<array.length; i++) {\r
+ System.out.print(array[i] & 0xff);\r
+ if (i<array.length-1) \r
+ System.out.print(",");\r
+ }\r
+ } \r
+}\r