1 7 8 package java.io; 9 10 20 public 21 class DataOutputStream extends FilterOutputStream implements DataOutput { 22 26 protected int written; 27 28 31 private byte[] bytearr = null; 32 33 42 public DataOutputStream(OutputStream out) { 43 super(out); 44 } 45 46 50 private void incCount(int value) { 51 int temp = written + value; 52 if (temp < 0) { 53 temp = Integer.MAX_VALUE; 54 } 55 written = temp; 56 } 57 58 70 public synchronized void write(int b) throws IOException { 71 out.write(b); 72 incCount(1); 73 } 74 75 87 public synchronized void write(byte b[], int off, int len) 88 throws IOException 89 { 90 out.write(b, off, len); 91 incCount(len); 92 } 93 94 105 public void flush() throws IOException { 106 out.flush(); 107 } 108 109 121 public final void writeBoolean(boolean v) throws IOException { 122 out.write(v ? 1 : 0); 123 incCount(1); 124 } 125 126 135 public final void writeByte(int v) throws IOException { 136 out.write(v); 137 incCount(1); 138 } 139 140 149 public final void writeShort(int v) throws IOException { 150 out.write((v >>> 8) & 0xFF); 151 out.write((v >>> 0) & 0xFF); 152 incCount(2); 153 } 154 155 164 public final void writeChar(int v) throws IOException { 165 out.write((v >>> 8) & 0xFF); 166 out.write((v >>> 0) & 0xFF); 167 incCount(2); 168 } 169 170 179 public final void writeInt(int v) throws IOException { 180 out.write((v >>> 24) & 0xFF); 181 out.write((v >>> 16) & 0xFF); 182 out.write((v >>> 8) & 0xFF); 183 out.write((v >>> 0) & 0xFF); 184 incCount(4); 185 } 186 187 private byte writeBuffer[] = new byte[8]; 188 189 198 public final void writeLong(long v) throws IOException { 199 writeBuffer[0] = (byte)(v >>> 56); 200 writeBuffer[1] = (byte)(v >>> 48); 201 writeBuffer[2] = (byte)(v >>> 40); 202 writeBuffer[3] = (byte)(v >>> 32); 203 writeBuffer[4] = (byte)(v >>> 24); 204 writeBuffer[5] = (byte)(v >>> 16); 205 writeBuffer[6] = (byte)(v >>> 8); 206 writeBuffer[7] = (byte)(v >>> 0); 207 out.write(writeBuffer, 0, 8); 208 incCount(8); 209 } 210 211 224 public final void writeFloat(float v) throws IOException { 225 writeInt(Float.floatToIntBits(v)); 226 } 227 228 241 public final void writeDouble(double v) throws IOException { 242 writeLong(Double.doubleToLongBits(v)); 243 } 244 245 256 public final void writeBytes(String s) throws IOException { 257 int len = s.length(); 258 for (int i = 0 ; i < len ; i++) { 259 out.write((byte)s.charAt(i)); 260 } 261 incCount(len); 262 } 263 264 276 public final void writeChars(String s) throws IOException { 277 int len = s.length(); 278 for (int i = 0 ; i < len ; i++) { 279 int v = s.charAt(i); 280 out.write((v >>> 8) & 0xFF); 281 out.write((v >>> 0) & 0xFF); 282 } 283 incCount(len * 2); 284 } 285 286 305 public final void writeUTF(String str) throws IOException { 306 writeUTF(str, this); 307 } 308 309 329 static int writeUTF(String str, DataOutput out) throws IOException { 330 int strlen = str.length(); 331 int utflen = 0; 332 int c, count = 0; 333 334 335 for (int i = 0; i < strlen; i++) { 336 c = str.charAt(i); 337 if ((c >= 0x0001) && (c <= 0x007F)) { 338 utflen++; 339 } else if (c > 0x07FF) { 340 utflen += 3; 341 } else { 342 utflen += 2; 343 } 344 } 345 346 if (utflen > 65535) 347 throw new UTFDataFormatException ( 348 "encoded string too long: " + utflen + " bytes"); 349 350 byte[] bytearr = null; 351 if (out instanceof DataOutputStream ) { 352 DataOutputStream dos = (DataOutputStream )out; 353 if(dos.bytearr == null || (dos.bytearr.length < (utflen+2))) 354 dos.bytearr = new byte[(utflen*2) + 2]; 355 bytearr = dos.bytearr; 356 } else { 357 bytearr = new byte[utflen+2]; 358 } 359 360 bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); 361 bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); 362 363 int i=0; 364 for (i=0; i<strlen; i++) { 365 c = str.charAt(i); 366 if (!((c >= 0x0001) && (c <= 0x007F))) break; 367 bytearr[count++] = (byte) c; 368 } 369 370 for (;i < strlen; i++){ 371 c = str.charAt(i); 372 if ((c >= 0x0001) && (c <= 0x007F)) { 373 bytearr[count++] = (byte) c; 374 375 } else if (c > 0x07FF) { 376 bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); 377 bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); 378 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 379 } else { 380 bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); 381 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 382 } 383 } 384 out.write(bytearr, 0, utflen+2); 385 return utflen + 2; 386 } 387 388 396 public final int size() { 397 return written; 398 } 399 } 400 | Popular Tags |