]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/BEString.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / BEString.java
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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 /*\r
12  * Created on Jan 21, 2005\r
13  * \r
14  * Copyright Toni Kalajainen\r
15  * \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
19  *\r
20  *     http://www.apache.org/licenses/LICENSE-2.0\r
21  *\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
27  */\r
28 package org.simantics.utils.bytes;\r
29 \r
30 import org.simantics.utils.strings.EString;\r
31 \r
32 /**\r
33  * Byte array <-> string conversions\r
34  * \r
35  * All conversions have length in the beginning (32-bit integer) and\r
36  * followed by actual chars \r
37  * \r
38  * the length is in Big Endian\r
39  *\r
40  * @author Toni Kalajainen\r
41  */\r
42 public class BEString {\r
43 \r
44         public static byte[] toBytes(String s) {\r
45                 int size = s.length();\r
46                 byte array[] = new byte[size + 4];\r
47                 // write length\r
48                 BEInt.write(size, array);\r
49                 // write chars\r
50                 for (int i=0; i<size; i++)\r
51                         array[i+4] = (byte) (s.charAt(i) );\r
52                 return array;\r
53         }\r
54         \r
55         /**\r
56          * Write string to byte array\r
57          * @param str the string\r
58          * @param array the byte array\r
59          * @param offset the offset\r
60          */\r
61         public static void write(String str, byte array[], int offset)\r
62         {\r
63                 int size = str.length();\r
64                 if (offset+4+size>array.length)\r
65                         throw new IndexOutOfBoundsException();          \r
66                 BEInt.write(size, array, offset);\r
67                 for (int i=0; i<size; i++)\r
68                         array[i+4+offset] = (byte) (str.charAt(i) );                    \r
69         }\r
70         \r
71         /**\r
72          * Write string to byte array\r
73          * @param str the string\r
74          * @param array the byte array\r
75          * @param offset the offset\r
76          */\r
77         public static void write(String str, byte array[])\r
78         {\r
79                 int size = str.length();\r
80                 if (4+size>array.length)\r
81                         throw new IndexOutOfBoundsException();          \r
82                 BEInt.write(size, array);\r
83                 for (int i=0; i<size; i++)\r
84                         array[i+4] = (byte) (str.charAt(i) );                   \r
85         }\r
86         \r
87         public static String toString(byte array[], int offset) {\r
88                 if (offset+4>array.length)\r
89                         throw new IndexOutOfBoundsException();\r
90                 // read size\r
91                 int size = BEInt.toInt(array, offset);\r
92                 if (offset+4+size>array.length)\r
93                         throw new IndexOutOfBoundsException();\r
94                 // read chars\r
95                 //return new String(array, 4+offset, size);\r
96                 char chars[] = new char[size];\r
97                 for (int i=0; i<size; i++)\r
98                         chars[i] = (char) (array[i+4+offset] & 0xff);   \r
99                 return new String(chars);\r
100         }\r
101         \r
102         public static String toString(byte array[]) {\r
103                 if (4>array.length)\r
104                         throw new IndexOutOfBoundsException();\r
105                 // read size\r
106                 int size = BEInt.toInt(array);\r
107                 if (4+size>array.length)\r
108                         throw new IndexOutOfBoundsException();\r
109                 // read chars\r
110                 //return new String(array, 4, size);\r
111                 char chars[] = new char[size];\r
112                 for (int i=0; i<size; i++)\r
113                         chars[i] = (char) (array[i+4] & 0xff);  \r
114                 return new String(chars);\r
115         }\r
116         \r
117         /**\r
118          * Big Endian hex\r
119          * @param value\r
120          * @param decimals\r
121          * @return\r
122          */\r
123         public static String intToHex(int value, int decimals) {\r
124                 String result="";               \r
125                 for (int i=0; i<decimals; i++) {\r
126                         result += EString.HEX_VALUES[(value>>4) & 0xF];\r
127                         result += EString.HEX_VALUES[value & 0xF];\r
128                         value = value >> 8;                     \r
129                 }               \r
130                 return result;\r
131         }       \r
132         \r
133         public static void main(String[] args) {\r
134                 String value = "STRING 01234"+(char)(128)+(char)(129)+(char)(255);\r
135                 \r
136                 int X = 500;\r
137                 System.out.println(X+" = "+intToHex(X, 4));             \r
138                 \r
139                 byte array[] = toBytes(value);                  \r
140                 System.out.print(value);\r
141                 System.out.print(" = ");\r
142                 printByteArray(array);\r
143                 System.out.println();\r
144                 \r
145                 write(value, array);\r
146                 System.out.print(value);\r
147                 System.out.print(" = ");\r
148                 printByteArray(array);\r
149                 System.out.println();\r
150                 \r
151                 write(value, array, 0);\r
152                 System.out.print(value);\r
153                 System.out.print(" = ");\r
154                 printByteArray(array);\r
155                 System.out.println();\r
156                 \r
157                 value = toString(array, 0);\r
158                 printByteArray(array);\r
159                 System.out.print(" = ");\r
160                 System.out.print(value);\r
161                 System.out.println();\r
162                                 \r
163                 value = toString(array);\r
164                 printByteArray(array);\r
165                 System.out.print(" = ");\r
166                 System.out.print(value);\r
167                 System.out.println();\r
168                 \r
169                 array = toBytes(value);                 \r
170                 System.out.print(value);\r
171                 System.out.print(" = ");\r
172                 printByteArray(array);\r
173                 System.out.println();\r
174                 \r
175                                                 \r
176         }\r
177         \r
178         public static void printByteArray(byte array[]) {\r
179                 for (int i=0; i<array.length; i++) {\r
180                         System.out.print(array[i] & 0xff);\r
181                         if (i<array.length-1) \r
182                                 System.out.print(",");\r
183                 }\r
184         }       \r
185 }\r