241 * @return bytes encoded into modified UTF-8 242 */ 243 private static byte[] stringToBytes(String string) { 244 try { 245 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 246 DataOutputStream ss = new DataOutputStream(bos); 247 ss.writeUTF(string); 248 byte[] content = bos.toByteArray(); 249 // first 2 items are length; 250 if(content.length <= 2) { 251 return new byte[0]; 252 } 253 return Arrays.copyOfRange(content, 2, content.length); 254 } catch (IOException ex) { 255 throw new RuntimeException(ex); 256 } 257 } 258 259 @Override 260 public String toString() { 261 ByteBuffer buffer = ByteBuffer.allocate(bytes.length+2); 262 buffer.order(ByteOrder.BIG_ENDIAN); 263 buffer.putShort((short)bytes.length); 264 buffer.put(bytes); 265 ByteArrayInputStream stream = new ByteArrayInputStream(buffer.array()); 266 DataInputStream in = new DataInputStream(stream); 267 try { 268 return in.readUTF(); 269 } catch (IOException ex) { 270 throw new RuntimeException(ex); 271 } 272 } 273 274 @Override 275 public char charAt(int index) { 276 int ch = byteAt(index); 277 278 return (ch & 0x80) == 0 ? (char)ch : '\0'; 279 } 280 281 @Override 282 public CharSequence subSequence(int start, int end) { 283 return (CharSequence)substring(start, end - start); 284 } | 241 * @return bytes encoded into modified UTF-8 242 */ 243 private static byte[] stringToBytes(String string) { 244 try { 245 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 246 DataOutputStream ss = new DataOutputStream(bos); 247 ss.writeUTF(string); 248 byte[] content = bos.toByteArray(); 249 // first 2 items are length; 250 if(content.length <= 2) { 251 return new byte[0]; 252 } 253 return Arrays.copyOfRange(content, 2, content.length); 254 } catch (IOException ex) { 255 throw new RuntimeException(ex); 256 } 257 } 258 259 @Override 260 public String toString() { 261 ByteBuffer buffer = ByteBuffer.allocate(count+2); 262 buffer.order(ByteOrder.BIG_ENDIAN); 263 buffer.putShort((short)count); 264 buffer.put(bytes, offset, count); 265 ByteArrayInputStream stream = new ByteArrayInputStream(buffer.array()); 266 DataInputStream in = new DataInputStream(stream); 267 try { 268 return in.readUTF(); 269 } catch (IOException ex) { 270 throw new RuntimeException(ex); 271 } 272 } 273 274 @Override 275 public char charAt(int index) { 276 int ch = byteAt(index); 277 278 return (ch & 0x80) == 0 ? (char)ch : '\0'; 279 } 280 281 @Override 282 public CharSequence subSequence(int start, int end) { 283 return (CharSequence)substring(start, end - start); 284 } |