1 46 50 package org.mr.core.util.byteable; 51 52 import java.io.IOException ; 53 import java.nio.ByteBuffer ; 54 55 56 57 66 public class ByteableOutputStream { 67 68 public static final int NULL_PLACE_HOLDER = -5; 70 ByteBuffer buff= null; 72 ByteBufferFactory myBufferPool = null; 73 public ByteableOutputStream(ByteBufferFactory pool ) { 74 myBufferPool = pool; 75 if(myBufferPool != null){ 77 buff = myBufferPool.getBuffer(myBufferPool.getSmallBufferSize()); 78 }else{ 79 buff = ByteBuffer.allocate(myBufferPool.getSmallBufferSize()); 80 } 81 82 } 83 84 89 public final void writeByteable(Byteable byteable) throws IOException { 90 if(byteable == null){ 91 writeASCIIString(ByteableRegistry.NULL_NAME); 92 }else{ 93 writeASCIIString(byteable.getByteableName()); 94 byteable.toBytes(this); 95 } 96 97 } 98 99 100 101 102 103 106 public final ByteBuffer getByteBuffer(){ 107 buff.flip(); 108 return buff; 109 } 110 111 112 124 public final void write(byte b) throws IOException { 125 increaseBufferSizeIfNeeded(1); 126 buff.put(b); 127 } 128 129 134 public final void increaseBufferSizeIfNeeded(int sizeNeeded){ 135 if(buff.remaining()< sizeNeeded){ 136 if(sizeNeeded < buff.capacity()){ 137 sizeNeeded = buff.capacity()*2; 138 }else{ 139 sizeNeeded = buff.capacity()+sizeNeeded; 140 } 141 ByteBuffer newBuff ; 142 if(myBufferPool != null){ 144 newBuff = myBufferPool.getBuffer(sizeNeeded); 145 }else{ 146 newBuff = ByteBuffer.allocate(sizeNeeded); 147 } 148 buff.flip(); 149 newBuff.put(buff); 150 myBufferPool.release(buff); 151 buff = newBuff; 152 } 153 } 154 155 160 public final void write(ByteBuffer otherBuff) throws IOException { 161 while(otherBuff.hasRemaining()){ 163 write(otherBuff.get()); 164 } 165 166 } 167 168 169 170 182 public final void write(byte b[], int off, int len) 183 throws IOException 184 { 185 int bytesNeeded =len - off; 186 increaseBufferSizeIfNeeded(bytesNeeded); 187 buff.put( b, off, len); 188 192 } 193 194 public final void write(byte b[]) 195 throws IOException 196 { 197 write(b , 0 , b.length); 198 199 } 200 201 202 final static byte trueByte = 1; 203 final static byte falseByte = 0; 204 216 public final void writeBoolean(boolean v) throws IOException { 217 if( v){ 218 write(trueByte); 219 }else{ 220 write(falseByte); 221 } 222 223 224 } 225 226 public void flush(){ 228 229 } 230 231 240 public final void writeByte(byte v) throws IOException { 241 write(v); 242 243 } 244 245 254 public final void writeShort(int v) throws IOException { 255 write((byte)((v >>> 8) & 0xFF)); 256 write((byte)((v >>> 0) & 0xFF)); 257 258 } 259 260 269 public final void writeChar(int v) throws IOException { 270 writeShort(v); 271 274 } 275 276 285 public final void writeInt(int v) throws IOException { 286 write((byte)((v >>> 24) & 0xFF)); 287 write((byte)((v >>> 16) & 0xFF)); 288 write((byte)((v >>> 8) & 0xFF)); 289 write((byte)((v >>> 0) & 0xFF)); 290 291 } 292 293 294 295 304 public final void writeLong(long v) throws IOException { 305 write((byte)(v >>> 56)); 306 write((byte)(v >>> 48)); 307 write((byte)(v >>> 40)); 308 write((byte)(v >>> 32)); 309 write((byte)(v >>> 24)); 310 write((byte)(v >>> 16)); 311 write((byte)(v >>> 8)); 312 write((byte)(v >>> 0)); 313 314 } 315 316 329 public final void writeFloat(float v) throws IOException { 330 writeInt(Float.floatToIntBits(v)); 331 } 332 333 346 public final void writeDouble(double v) throws IOException { 347 writeLong(Double.doubleToLongBits(v)); 348 } 349 350 361 public final void writeBytes(String s) throws IOException { 362 int len = s.length(); 363 for (int i = 0 ; i < len ; i++) { 364 write((byte)s.charAt(i)); 365 } 366 367 } 368 369 381 public final void writeChars(String s) throws IOException { 382 int len = s.length(); 383 for (int i = 0 ; i < len ; i++) { 384 int v = s.charAt(i); 385 writeChar(v); 386 } 389 390 } 391 392 393 byte[] asciiTemp = new byte[1000]; 394 399 public final void writeASCIIString(String s) throws IOException { 400 if (s==null){ 401 writeShort(ByteableOutputStream.NULL_PLACE_HOLDER); 402 return; 403 } 404 405 short length =(short) s.length(); 406 writeShort(length); 407 int asciiTempIndex = 0; 408 char v; 409 for(int index =0 ; index<length ; index++){ 410 v=s.charAt(index); 411 asciiTemp[asciiTempIndex++]= (byte)v; 413 } 414 write(asciiTemp , 0 ,asciiTempIndex ); 415 } 416 417 418 437 public final int writeUTF(String str) throws IOException { 438 if (str==null){ 439 writeInt(ByteableOutputStream.NULL_PLACE_HOLDER); 440 return 1; 441 } 442 443 int strlen = str.length(); 444 int utflen = 0; 445 char[] charr = new char[strlen]; 446 int c, count = 0; 447 448 str.getChars(0, strlen, charr, 0); 449 450 for (int i = 0; i < strlen; i++) { 451 c = charr[i]; 452 if ((c >= 0x0001) && (c <= 0x007F)) { 453 utflen++; 454 } else if (c > 0x07FF) { 455 utflen += 3; 456 } else { 457 utflen += 2; 458 } 459 } 460 461 464 byte[] bytearr = new byte[utflen]; 465 writeInt(utflen); 466 467 for (int i = 0; i < strlen; i++) { 468 c = charr[i]; 469 if ((c >= 0x0001) && (c <= 0x007F)) { 470 bytearr[count++] = (byte) c; 471 } else if (c > 0x07FF) { 472 bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); 473 bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); 474 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 475 } else { 476 bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); 477 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 478 } 479 } 480 write(bytearr , 0 ,count); 481 return utflen ; 482 } 483 484 485 public final void release() { 486 if(buff!= null){ 487 myBufferPool.release(buff); 488 buff = null; 489 } 490 }} 492 | Popular Tags |