]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEString.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / LEString.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 Little Endian\r
39  *\r
40  * @author Toni Kalajainen\r
41  */\r
42 public class LEString {\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                 LEInt.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                 LEInt.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                 LEInt.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 = LEInt.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 = LEInt.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         public static void main(String[] args) {\r
118                 String value = "STRING 01234"+(char)(128)+(char)(129)+(char)(255);\r
119                 \r
120                 int X = 500;\r
121                 System.out.println(X+" = "+intToHex(X, 4));             \r
122                 \r
123                 byte array[] = toBytes(value);                  \r
124                 System.out.print(value);\r
125                 System.out.print(" = ");\r
126                 printByteArray(array);\r
127                 System.out.println();\r
128                 \r
129                 write(value, array);\r
130                 System.out.print(value);\r
131                 System.out.print(" = ");\r
132                 printByteArray(array);\r
133                 System.out.println();\r
134                 \r
135                 write(value, array, 0);\r
136                 System.out.print(value);\r
137                 System.out.print(" = ");\r
138                 printByteArray(array);\r
139                 System.out.println();\r
140                 \r
141                 value = toString(array, 0);\r
142                 printByteArray(array);\r
143                 System.out.print(" = ");\r
144                 System.out.print(value);\r
145                 System.out.println();\r
146                                 \r
147                 value = toString(array);\r
148                 printByteArray(array);\r
149                 System.out.print(" = ");\r
150                 System.out.print(value);\r
151                 System.out.println();\r
152                 \r
153                 array = toBytes(value);                 \r
154                 System.out.print(value);\r
155                 System.out.print(" = ");\r
156                 printByteArray(array);\r
157                 System.out.println();                                                   \r
158         }\r
159         \r
160         /**\r
161          * Little Endian hex\r
162          * @param value\r
163          * @param decimals\r
164          * @return\r
165          */\r
166         public static String intToHex(int value, int decimals) {\r
167                 String result="";               \r
168                 for (int i=0; i<decimals; i++) {\r
169                         result = \r
170                                 EString.HEX_VALUES[value & 0xF] + result;\r
171                         result =                        \r
172                                 EString.HEX_VALUES[(value>>4) & 0xF] + result;\r
173                         value >>= 8;                    \r
174                 }               \r
175                 return result;\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