1 19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 20 21 import java.io.*; 22 23 27 public class Converter { 28 29 30 31 private static final String ENCODING = "UTF-8"; 32 33 39 public static final void writeByte( 40 byte array[], IntHolder offset, byte data){ 41 offset.setValue(writeByte(array, offset.getValue(), data)); 42 } 43 44 50 public static final int writeByte(byte array[], int offset, byte data) { 51 array[offset++] = data; 52 return offset; 53 } 54 55 61 public static final void writeShort( 62 byte array[], IntHolder offset, short data){ 63 offset.setValue(writeShort(array, offset.getValue(), data)); 64 } 65 66 72 public static final int writeShort(byte array[], int offset, short data) { 73 array[offset++] = (byte)(data >> 8); 74 array[offset++] = (byte)data; 75 return offset; 76 } 77 78 84 public static final void writeInt( 85 byte array[], IntHolder offset, int data){ 86 offset.setValue(writeInt(array, offset.getValue(), data)); 87 } 88 89 95 public static final int writeInt(byte array[], int offset, int data) { 96 array[offset++] = (byte)(data >> 24); 97 array[offset++] = (byte)(data >> 16); 98 array[offset++] = (byte)(data >> 8); 99 array[offset++] = (byte)data; 100 return offset; 101 } 102 103 109 public static final void writeLong( 110 byte array[], IntHolder offset, long data){ 111 offset.setValue(writeLong(array, offset.getValue(), data)); 112 } 113 114 120 public static final int writeLong(byte array[], int offset, long data) { 121 array[offset++] = (byte)(data >>> 56); 122 array[offset++] = (byte)(data >>> 48); 123 array[offset++] = (byte)(data >>> 40); 124 array[offset++] = (byte)(data >>> 32); 125 array[offset++] = (byte)(data >>> 24); 126 array[offset++] = (byte)(data >>> 16); 127 array[offset++] = (byte)(data >>> 8); 128 array[offset++] = (byte)data; 129 return offset; 130 } 131 132 141 public static final void writeString( 142 byte array[], IntHolder offset, String data){ 143 offset.setValue(writeString(array, offset.getValue(), data)); 144 } 145 146 155 public static final int writeString(byte array[], int offset, String data) { 156 byte enc[] = convertStringToUTF8(data); 157 offset = writeShort(array, offset, (short)enc.length); 158 if (enc.length > 0) 159 System.arraycopy(enc, 0, array, offset, enc.length); 160 return offset + enc.length; 161 } 162 163 167 public static final byte[] convertStringToUTF8(String str) { 168 byte result[] = null; 169 try { 170 result = str.getBytes(ENCODING); 171 } catch (UnsupportedEncodingException ex) { 172 173 throw new RuntimeException ("UTF-8 not supported!"); 174 } 175 return result; 176 } 177 178 184 public static final byte readByte(byte array[], IntHolder offset) { 185 int offst = offset.getValue(); 186 byte data = readByte(array, offst); 187 offset.setValue(offst+1); 188 return data; 189 } 190 191 196 public static final byte readByte(byte array[], int offst) { 197 byte data = array[offst]; 198 return data; 199 } 200 201 207 public static final short readShort(byte array[], IntHolder offset) { 208 int offst = offset.getValue(); 209 short data = readShort(array, offst); 210 offset.setValue(offst+2); 211 return data; 212 } 213 214 219 public static final short readShort(byte array[], int offst) { 220 short data = (short)(((short)array[offst++]) & 0xFF); 221 data <<= 8; 222 data |= (((short)array[offst]) & 0xFF); 223 return data; 224 } 225 226 232 public static final int readInt(byte array[], IntHolder offset) { 233 int offst = offset.getValue(); 234 int data = readInt(array, offst); 235 offset.setValue(offst + 4); 236 return data; 237 } 238 239 244 public static final int readInt(byte array[], int offst) { 245 int data = (((int)array[offst++]) & 0xFF); 246 for (int i = 0; i < 3; i++) { 247 data <<= 8; 248 data |= (((int)array[offst++]) & 0xFF); 249 } 250 return data; 251 } 252 253 259 public static final long readLong(byte array[], IntHolder offset) { 260 int offst = offset.getValue(); 261 long data = readLong(array, offst); 262 offset.setValue(offst + 8); 263 return data; 264 } 265 266 271 public static final long readLong(byte array[], int offst) { 272 long data = (((long)array[offst++]) & 0xFF); 273 for (int i = 0; i < 7; i++) { 274 data <<= 8; 275 data |= (((long)array[offst++]) & 0xFF); 276 } 277 return data; 278 } 279 280 287 public static final String readString(byte array[], IntHolder offset) { 288 return readString(array, offset.getValue(), offset); 289 } 290 291 297 public static final String readString(byte array[], int offset) { 298 return readString(array, offset, null); 299 } 300 301 309 private static String readString( 310 byte array[], int offset, IntHolder newOffset) { 311 String str; 312 313 short arrSize = readShort(array, offset); 314 if (arrSize == 0) { 315 str = ""; 316 } 317 else { 318 byte enc[] = new byte[arrSize]; 319 System.arraycopy(array, offset+2, enc, 0, arrSize); 320 str = convertUTF8ToString(enc); 321 } 322 if (newOffset != null) 323 newOffset.setValue(offset + 2 + arrSize); 324 return str; 325 } 326 327 331 public static final String convertUTF8ToString(byte buffer[]) { 332 String result = null; 333 try { 334 result = new String (buffer, ENCODING); 335 } catch (UnsupportedEncodingException ex) { 336 337 throw new RuntimeException ("UTF-8 not supported!"); 338 } 339 return result; 340 } 341 342 } 343 344 | Popular Tags |