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