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