322 |
} |
} |
323 |
|
|
324 |
/** |
/** |
|
* This method reads 8 unsigned bits into a Java <code>int</code> value |
|
|
* from the |
|
|
* stream. The value returned is in the range of 0 to 255. |
|
|
* <p> |
|
|
* This method can read an unsigned byte written by an object implementing |
|
|
* the <code>writeUnsignedByte()</code> method in the |
|
|
* <code>DataOutput</code> interface. |
|
|
* |
|
|
* @return The unsigned bytes value read as a Java <code>int</code> |
|
|
* |
|
|
* @exception EOFException If end of file is reached before reading the value |
|
|
* @exception IOException If any other error occurs |
|
|
* |
|
|
* @see DataOutput |
|
|
*/ |
|
|
public final int readUnsignedByte () throws IOException |
|
|
{ |
|
|
int byte_read = read (); |
|
|
|
|
|
if (byte_read == -1) |
|
|
throw new EOFException ("Unexpected end of stream"); |
|
|
|
|
|
return byte_read & 0xFF; |
|
|
} |
|
|
|
|
|
/** |
|
325 |
* This method reads a Java <code>char</code> value from an input stream. |
* This method reads a Java <code>char</code> value from an input stream. |
326 |
* It operates by reading two bytes from the stream and converting them to |
* It operates by reading two bytes from the stream and converting them to |
327 |
* a single 16-bit Java <code>char</code> The two bytes are stored most |
* a single 16-bit Java <code>char</code> The two bytes are stored most |
354 |
} |
} |
355 |
|
|
356 |
/** |
/** |
357 |
* This method reads a signed 16-bit value into a Java in from the stream. |
* This method reads a Java double value from an input stream. It operates |
358 |
* It operates by reading two bytes from the stream and converting them to |
* by first reading a <code>logn</code> value from the stream by calling the |
359 |
* a single 16-bit Java <code>short</code> The two bytes are stored most |
* <code>readLong()</code> method in this interface, then |
360 |
* significant byte first (i.e., "big endian") regardless of the native |
* converts that <code>long</code> |
361 |
* host byte ordering. |
* to a <code>double</code> using the <code>longBitsToDouble</code> |
362 |
* <p> |
* method in the class <code>java.lang.Double</code> |
|
* As an example, if <code>byte1</code> and code{byte2</code> |
|
|
* represent the first |
|
|
* and second byte read from the stream respectively, they will be |
|
|
* transformed to a <code>short</code> in the following manner: |
|
|
* <p> |
|
|
* <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> |
|
|
* <p> |
|
|
* The value returned is in the range of -32768 to 32767. |
|
|
* <p> |
|
|
* This method can read a <code>short</code> written by an object |
|
|
* implementing the |
|
|
* <code>writeShort()</code> method in the <code>DataOutput</code> interface. |
|
|
* |
|
|
* @return The <code>short</code> value read |
|
|
* |
|
|
* @exception EOFException If end of file is reached before reading the value |
|
|
* @exception IOException If any other error occurs |
|
|
* |
|
|
* @see DataOutput |
|
|
*/ |
|
|
public final synchronized short readShort () throws IOException |
|
|
{ |
|
|
readFully (buf, 0, 2); |
|
|
|
|
|
return (short) ((buf[0] << 8) | (buf[1] & 0xff)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* This method reads 16 unsigned bits into a Java int value from the stream. |
|
|
* It operates by reading two bytes from the stream and converting them to |
|
|
* a single Java <code>int</code> The two bytes are stored most |
|
|
* significant byte first (i.e., "big endian") regardless of the native |
|
|
* host byte ordering. |
|
|
* <p> |
|
|
* As an example, if <code>byte1</code> and <code>byte2</code> |
|
|
* represent the first |
|
|
* and second byte read from the stream respectively, they will be |
|
|
* transformed to an <code>int</code> in the following manner: |
|
|
* <p> |
|
|
* <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code> |
|
|
* <p> |
|
|
* The value returned is in the range of 0 to 65535. |
|
|
* <p> |
|
|
* This method can read an unsigned short written by an object implementing |
|
|
* the <code>writeUnsignedShort()</code> method in the |
|
|
* <code>DataOutput</code> interface. |
|
|
* |
|
|
* @return The unsigned short value read as a Java <code>int</code> |
|
|
* |
|
|
* @exception EOFException If end of file is reached before reading the value |
|
|
* @exception IOException If any other error occurs |
|
|
*/ |
|
|
public final synchronized int readUnsignedShort() |
|
|
throws IOException |
|
|
{ |
|
|
readFully(buf, 0, 2); |
|
|
|
|
|
return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* This method reads a Java <code>int</code> value from an input stream |
|
|
* It operates by reading four bytes from the stream and converting them to |
|
|
* a single Java <code>int</code> The bytes are stored most |
|
|
* significant byte first (i.e., "big endian") regardless of the native |
|
|
* host byte ordering. |
|
|
* <p> |
|
|
* As an example, if <code>byte1</code> through <code>byte4</code> |
|
|
* represent the first |
|
|
* four bytes read from the stream, they will be |
|
|
* transformed to an <code>int</code> in the following manner: |
|
|
* <p> |
|
|
* <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + |
|
|
* ((byte3 & 0xFF) << 8) + (byte4 & 0xFF)))</code> |
|
|
* <p> |
|
|
* The value returned is in the range of 0 to 65535. |
|
|
* <p> |
|
|
* This method can read an <code>int</code> written by an object |
|
|
* implementing the |
|
|
* <code>writeInt()</code> method in the <code>DataOutput</code> interface. |
|
|
* |
|
|
* @return The <code>int</code> value read |
|
|
* |
|
|
* @exception EOFException If end of file is reached before reading the int |
|
|
* @exception IOException If any other error occurs |
|
|
* |
|
|
* @see DataOutput |
|
|
*/ |
|
|
public final synchronized int readInt() throws IOException |
|
|
{ |
|
|
readFully(buf, 0, 4); |
|
|
|
|
|
return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | |
|
|
((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* This method reads a Java long value from an input stream |
|
|
* It operates by reading eight bytes from the stream and converting them to |
|
|
* a single Java <code>long</code> The bytes are stored most |
|
|
* significant byte first (i.e., "big endian") regardless of the native |
|
|
* host byte ordering. |
|
|
* <p> |
|
|
* As an example, if <code>byte1</code> through <code>byte8</code> |
|
|
* represent the first |
|
|
* eight bytes read from the stream, they will be |
|
|
* transformed to an <code>long</code> in the following manner: |
|
|
* <p> |
|
|
* <code> |
|
|
* (long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) + |
|
|
* (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) + |
|
|
* (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) + |
|
|
* (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code> |
|
|
* <p> |
|
|
* The value returned is in the range of 0 to 65535. |
|
363 |
* <p> |
* <p> |
364 |
* This method can read an <code>long</code> written by an object |
* This method can read a <code>double</code> written by an object |
365 |
* implementing the |
* implementing the |
366 |
* <code>writeLong()</code> method in the <code>DataOutput</code> interface. |
* <code>writeDouble()</code> method in the <code>DataOutput</code> |
367 |
|
* interface. |
368 |
* |
* |
369 |
* @return The <code>long</code> value read |
* @return The <code>double</code> value read |
370 |
* |
* |
371 |
* @exception EOFException If end of file is reached before reading the long |
* @exception EOFException If end of file is reached before reading |
372 |
|
* the double |
373 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
374 |
* |
* |
375 |
|
* @see java.lang.Double |
376 |
* @see DataOutput |
* @see DataOutput |
377 |
*/ |
*/ |
378 |
public final synchronized long readLong() throws IOException |
public final double readDouble () throws IOException |
379 |
{ |
{ |
380 |
readFully(buf, 0, 8); |
long val = readLong(); |
381 |
|
|
382 |
return (((long)(buf[0] & 0xff) << 56) | |
return Double.longBitsToDouble(val); |
|
((long)(buf[1] & 0xff) << 48) | |
|
|
((long)(buf[2] & 0xff) << 40) | |
|
|
((long)(buf[3] & 0xff) << 32) | |
|
|
((long)(buf[4] & 0xff) << 24) | |
|
|
((long)(buf[5] & 0xff) << 16) | |
|
|
((long)(buf[6] & 0xff) << 8) | |
|
|
((long)(buf[7] & 0xff))); |
|
383 |
} |
} |
384 |
|
|
385 |
/** |
/** |
386 |
* This method reads a Java float value from an input stream. It operates |
* This method reads a Java float value from an input stream. It operates |
387 |
* by first reading an <code>int</code> value from the stream by calling the |
* by first reading an <code>int</code> value from the stream by calling the |
402 |
* @see java.lang.Float |
* @see java.lang.Float |
403 |
* @see DataOutput |
* @see DataOutput |
404 |
*/ |
*/ |
405 |
public final float readFloat() throws IOException |
public final float readFloat () throws IOException |
406 |
{ |
{ |
407 |
int val = readInt(); |
int val = readInt(); |
408 |
|
|
421 |
* buffer |
* buffer |
422 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
423 |
*/ |
*/ |
424 |
public final void readFully (byte[] buf) throws IOException |
public final void readFully (byte[] buffer) throws IOException |
425 |
{ |
{ |
426 |
readFully (buf, 0, buf.length); |
readFully (buffer, 0, buffer.length); |
427 |
} |
} |
428 |
|
|
429 |
/** |
/** |
443 |
* the buffer |
* the buffer |
444 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
445 |
*/ |
*/ |
446 |
public synchronized final void readFully (byte[] buf, int offset, int len) |
public synchronized final void readFully (byte[] buffer, int offset, int len) |
447 |
throws IOException |
throws IOException |
448 |
{ |
{ |
449 |
int total_read = 0; |
int total_read = 0; |
450 |
|
|
451 |
while (total_read < len) |
while (total_read < len) |
452 |
{ |
{ |
453 |
int bytes_read = read(buf, offset + total_read, len - total_read); |
int bytes_read = read (buffer, offset + total_read, len - total_read); |
454 |
if (bytes_read == -1) |
if (bytes_read == -1) |
455 |
throw new EOFException("Unexpected end of stream"); |
throw new EOFException("Unexpected end of stream"); |
456 |
|
|
459 |
} |
} |
460 |
|
|
461 |
/** |
/** |
462 |
* This method reads a Java double value from an input stream. It operates |
* This method reads a Java <code>int</code> value from an input stream |
463 |
* by first reading a <code>logn</code> value from the stream by calling the |
* It operates by reading four bytes from the stream and converting them to |
464 |
* <code>readLong()</code> method in this interface, then |
* a single Java <code>int</code> The bytes are stored most |
465 |
* converts that <code>long</code> |
* significant byte first (i.e., "big endian") regardless of the native |
466 |
* to a <code>double</code> using the <code>longBitsToDouble</code> |
* host byte ordering. |
|
* method in the class <code>java.lang.Double</code> |
|
467 |
* <p> |
* <p> |
468 |
* This method can read a <code>double</code> written by an object |
* As an example, if <code>byte1</code> through <code>byte4</code> |
469 |
|
* represent the first |
470 |
|
* four bytes read from the stream, they will be |
471 |
|
* transformed to an <code>int</code> in the following manner: |
472 |
|
* <p> |
473 |
|
* <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + |
474 |
|
* ((byte3 & 0xFF) << 8) + (byte4 & 0xFF)))</code> |
475 |
|
* <p> |
476 |
|
* The value returned is in the range of 0 to 65535. |
477 |
|
* <p> |
478 |
|
* This method can read an <code>int</code> written by an object |
479 |
* implementing the |
* implementing the |
480 |
* <code>writeDouble()</code> method in the <code>DataOutput</code> |
* <code>writeInt()</code> method in the <code>DataOutput</code> interface. |
|
* interface. |
|
481 |
* |
* |
482 |
* @return The <code>double</code> value read |
* @return The <code>int</code> value read |
483 |
* |
* |
484 |
* @exception EOFException If end of file is reached before reading |
* @exception EOFException If end of file is reached before reading the int |
|
* the double |
|
485 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
486 |
* |
* |
|
* @see java.lang.Double |
|
487 |
* @see DataOutput |
* @see DataOutput |
488 |
*/ |
*/ |
489 |
public final double readDouble() throws IOException |
public final synchronized int readInt() throws IOException |
490 |
{ |
{ |
491 |
long val = readLong(); |
readFully(buf, 0, 4); |
492 |
|
|
493 |
return Double.longBitsToDouble(val); |
return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | |
494 |
|
((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); |
495 |
} |
} |
496 |
|
|
497 |
/** |
/** |
521 |
* |
* |
522 |
* @deprecated |
* @deprecated |
523 |
*/ |
*/ |
524 |
public synchronized final String readLine() throws IOException |
public synchronized final String readLine () throws IOException |
525 |
{ |
{ |
526 |
StringBuffer sb = new StringBuffer(""); |
StringBuffer sb = new StringBuffer (""); |
527 |
|
|
528 |
for (;;) |
for (;;) |
529 |
{ |
{ |
530 |
int byte_read = read(); |
int byte_read = read (); |
531 |
|
|
532 |
if (byte_read == -1) |
if (byte_read == -1) |
533 |
return(sb.toString()); |
return sb.toString(); |
534 |
|
|
535 |
char c = (char)byte_read; |
char c = (char) byte_read; |
536 |
|
|
537 |
if (c == '\r') |
if (c == '\r') |
538 |
{ |
{ |
539 |
byte_read = read(); |
byte_read = read(); |
540 |
if (((char)byte_read) != '\n') |
if (((char)byte_read) != '\n') |
541 |
seek(getFilePointer() - 1); |
seek (getFilePointer() - 1); |
542 |
|
|
543 |
return(sb.toString()); |
return sb.toString(); |
544 |
} |
} |
545 |
|
|
546 |
if (c == '\n') |
if (c == '\n') |
547 |
return(sb.toString()); |
return sb.toString(); |
548 |
|
|
549 |
sb.append(c); |
sb.append (c); |
550 |
} |
} |
551 |
} |
} |
552 |
|
|
553 |
/** |
/** |
554 |
|
* This method reads a Java long value from an input stream |
555 |
|
* It operates by reading eight bytes from the stream and converting them to |
556 |
|
* a single Java <code>long</code> The bytes are stored most |
557 |
|
* significant byte first (i.e., "big endian") regardless of the native |
558 |
|
* host byte ordering. |
559 |
|
* <p> |
560 |
|
* As an example, if <code>byte1</code> through <code>byte8</code> |
561 |
|
* represent the first |
562 |
|
* eight bytes read from the stream, they will be |
563 |
|
* transformed to an <code>long</code> in the following manner: |
564 |
|
* <p> |
565 |
|
* <code> |
566 |
|
* (long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) + |
567 |
|
* (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) + |
568 |
|
* (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) + |
569 |
|
* (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code> |
570 |
|
* <p> |
571 |
|
* The value returned is in the range of 0 to 65535. |
572 |
|
* <p> |
573 |
|
* This method can read an <code>long</code> written by an object |
574 |
|
* implementing the |
575 |
|
* <code>writeLong()</code> method in the <code>DataOutput</code> interface. |
576 |
|
* |
577 |
|
* @return The <code>long</code> value read |
578 |
|
* |
579 |
|
* @exception EOFException If end of file is reached before reading the long |
580 |
|
* @exception IOException If any other error occurs |
581 |
|
* |
582 |
|
* @see DataOutput |
583 |
|
*/ |
584 |
|
public final synchronized long readLong() throws IOException |
585 |
|
{ |
586 |
|
readFully(buf, 0, 8); |
587 |
|
|
588 |
|
return (((long)(buf[0] & 0xff) << 56) | |
589 |
|
((long)(buf[1] & 0xff) << 48) | |
590 |
|
((long)(buf[2] & 0xff) << 40) | |
591 |
|
((long)(buf[3] & 0xff) << 32) | |
592 |
|
((long)(buf[4] & 0xff) << 24) | |
593 |
|
((long)(buf[5] & 0xff) << 16) | |
594 |
|
((long)(buf[6] & 0xff) << 8) | |
595 |
|
((long)(buf[7] & 0xff))); |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
|
* This method reads a signed 16-bit value into a Java in from the stream. |
600 |
|
* It operates by reading two bytes from the stream and converting them to |
601 |
|
* a single 16-bit Java <code>short</code> The two bytes are stored most |
602 |
|
* significant byte first (i.e., "big endian") regardless of the native |
603 |
|
* host byte ordering. |
604 |
|
* <p> |
605 |
|
* As an example, if <code>byte1</code> and code{byte2</code> |
606 |
|
* represent the first |
607 |
|
* and second byte read from the stream respectively, they will be |
608 |
|
* transformed to a <code>short</code> in the following manner: |
609 |
|
* <p> |
610 |
|
* <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code> |
611 |
|
* <p> |
612 |
|
* The value returned is in the range of -32768 to 32767. |
613 |
|
* <p> |
614 |
|
* This method can read a <code>short</code> written by an object |
615 |
|
* implementing the |
616 |
|
* <code>writeShort()</code> method in the <code>DataOutput</code> interface. |
617 |
|
* |
618 |
|
* @return The <code>short</code> value read |
619 |
|
* |
620 |
|
* @exception EOFException If end of file is reached before reading the value |
621 |
|
* @exception IOException If any other error occurs |
622 |
|
* |
623 |
|
* @see DataOutput |
624 |
|
*/ |
625 |
|
public final synchronized short readShort () throws IOException |
626 |
|
{ |
627 |
|
readFully (buf, 0, 2); |
628 |
|
|
629 |
|
return (short) ((buf[0] << 8) | (buf[1] & 0xff)); |
630 |
|
} |
631 |
|
|
632 |
|
/** |
633 |
|
* This method reads 8 unsigned bits into a Java <code>int</code> value |
634 |
|
* from the |
635 |
|
* stream. The value returned is in the range of 0 to 255. |
636 |
|
* <p> |
637 |
|
* This method can read an unsigned byte written by an object implementing |
638 |
|
* the <code>writeUnsignedByte()</code> method in the |
639 |
|
* <code>DataOutput</code> interface. |
640 |
|
* |
641 |
|
* @return The unsigned bytes value read as a Java <code>int</code> |
642 |
|
* |
643 |
|
* @exception EOFException If end of file is reached before reading the value |
644 |
|
* @exception IOException If any other error occurs |
645 |
|
* |
646 |
|
* @see DataOutput |
647 |
|
*/ |
648 |
|
public final int readUnsignedByte () throws IOException |
649 |
|
{ |
650 |
|
int byte_read = read (); |
651 |
|
|
652 |
|
if (byte_read == -1) |
653 |
|
throw new EOFException ("Unexpected end of stream"); |
654 |
|
|
655 |
|
return byte_read & 0xFF; |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
|
* This method reads 16 unsigned bits into a Java int value from the stream. |
660 |
|
* It operates by reading two bytes from the stream and converting them to |
661 |
|
* a single Java <code>int</code> The two bytes are stored most |
662 |
|
* significant byte first (i.e., "big endian") regardless of the native |
663 |
|
* host byte ordering. |
664 |
|
* <p> |
665 |
|
* As an example, if <code>byte1</code> and <code>byte2</code> |
666 |
|
* represent the first |
667 |
|
* and second byte read from the stream respectively, they will be |
668 |
|
* transformed to an <code>int</code> in the following manner: |
669 |
|
* <p> |
670 |
|
* <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code> |
671 |
|
* <p> |
672 |
|
* The value returned is in the range of 0 to 65535. |
673 |
|
* <p> |
674 |
|
* This method can read an unsigned short written by an object implementing |
675 |
|
* the <code>writeUnsignedShort()</code> method in the |
676 |
|
* <code>DataOutput</code> interface. |
677 |
|
* |
678 |
|
* @return The unsigned short value read as a Java <code>int</code> |
679 |
|
* |
680 |
|
* @exception EOFException If end of file is reached before reading the value |
681 |
|
* @exception IOException If any other error occurs |
682 |
|
*/ |
683 |
|
public final synchronized int readUnsignedShort () |
684 |
|
throws IOException |
685 |
|
{ |
686 |
|
readFully(buf, 0, 2); |
687 |
|
|
688 |
|
return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); |
689 |
|
} |
690 |
|
|
691 |
|
/** |
692 |
* This method reads a <code>String</code> from an input stream that |
* This method reads a <code>String</code> from an input stream that |
693 |
* is encoded in |
* is encoded in |
694 |
* a modified UTF-8 format. This format has a leading two byte sequence |
* a modified UTF-8 format. This format has a leading two byte sequence |
760 |
* |
* |
761 |
* @see DataOutput |
* @see DataOutput |
762 |
*/ |
*/ |
763 |
public synchronized final String readUTF() |
public synchronized final String readUTF () throws IOException |
|
throws IOException |
|
764 |
{ |
{ |
765 |
StringBuffer sb = new StringBuffer(""); |
StringBuffer sb = new StringBuffer(""); |
766 |
|
|
771 |
// FIXME: Look to migrate to new String(buf, "UTF-8") if performance ok |
// FIXME: Look to migrate to new String(buf, "UTF-8") if performance ok |
772 |
return DataInputStream.convertFromUTF(buf); |
return DataInputStream.convertFromUTF(buf); |
773 |
} |
} |
774 |
|
|
775 |
/** |
/** |
776 |
* This method sets the current file position to the specified offset |
* This method sets the current file position to the specified offset |
777 |
* from the beginning of the file. Note that some operating systems will |
* from the beginning of the file. Note that some operating systems will |
782 |
* |
* |
783 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
784 |
*/ |
*/ |
785 |
public void seek(long pos) throws IOException |
public void seek (long pos) throws IOException |
786 |
{ |
{ |
787 |
fd.seek(pos, fd.SET, false); |
fd.seek (pos, FileDescriptor.SET, false); |
788 |
} |
} |
789 |
|
|
790 |
/** |
/** |
791 |
* This method attempts to skip and discard the specified number of bytes |
* This method attempts to skip and discard the specified number of bytes |
792 |
* in the input stream. It may actually skip fewer bytes than requested. |
* in the input stream. It may actually skip fewer bytes than requested. |
799 |
* |
* |
800 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
801 |
*/ |
*/ |
802 |
public int skipBytes(int numBytes) throws IOException |
public int skipBytes (int numBytes) throws IOException |
803 |
{ |
{ |
804 |
if (numBytes < 0) |
if (numBytes < 0) |
805 |
throw new IllegalArgumentException("Can't skip negative bytes: " + |
throw new IllegalArgumentException ("Can't skip negative bytes: " + |
806 |
numBytes); |
numBytes); |
807 |
|
|
808 |
if (numBytes == 0) |
if (numBytes == 0) |
809 |
return(0); |
return 0; |
810 |
|
|
811 |
long curPos = fd.getFilePointer(); |
long curPos = fd.getFilePointer (); |
812 |
long newPos = fd.seek(numBytes, fd.CUR, true); |
long newPos = fd.seek (numBytes, FileDescriptor.CUR, true); |
813 |
|
|
814 |
return((int)(newPos-curPos)); |
return (int) (newPos - curPos); |
815 |
} |
} |
816 |
|
|
817 |
/** |
/** |
818 |
* This method writes a single byte of data to the file. The file must |
* This method writes a single byte of data to the file. The file must |
819 |
* be open for read-write in order for this operation to succeed. |
* be open for read-write in order for this operation to succeed. |
822 |
* |
* |
823 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
824 |
*/ |
*/ |
825 |
public void write(int b) throws IOException |
public void write (int oneByte) throws IOException |
826 |
{ |
{ |
827 |
if (readOnly) |
if (readOnly) |
828 |
throw new IOException("File is open read only"); |
throw new IOException("File is open read only"); |
829 |
|
|
830 |
fd.write(b); |
fd.write (oneByte); |
831 |
} |
} |
832 |
|
|
833 |
/** |
/** |
834 |
* This method writes all the bytes in the specified array to the file. |
* This method writes all the bytes in the specified array to the file. |
835 |
* The file must be open read-write in order for this operation to succeed. |
* The file must be open read-write in order for this operation to succeed. |
836 |
* |
* |
837 |
* @param buf The array of bytes to write to the file |
* @param buf The array of bytes to write to the file |
838 |
*/ |
*/ |
839 |
public void write(byte[] buf) throws IOException |
public void write (byte[] buffer) throws IOException |
840 |
{ |
{ |
841 |
write(buf, 0, buf.length); |
write (buffer, 0, buffer.length); |
842 |
} |
} |
843 |
|
|
844 |
/** |
/** |
845 |
* This method writes <code>len</code> bytes to the file from the specified |
* This method writes <code>len</code> bytes to the file from the specified |
846 |
* array starting at index <code>offset</code> into the array. |
* array starting at index <code>offset</code> into the array. |
851 |
* |
* |
852 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
853 |
*/ |
*/ |
854 |
public void write(byte[] buf, int offset, int len) throws IOException |
public void write (byte[] buffer, int offset, int len) throws IOException |
855 |
{ |
{ |
856 |
if (readOnly) |
if (readOnly) |
857 |
throw new IOException("File is open read only"); |
throw new IOException("File is open read only"); |
858 |
|
|
859 |
fd.write(buf, offset, len); |
fd.write (buffer, offset, len); |
860 |
} |
} |
861 |
|
|
862 |
/** |
/** |
863 |
* This method writes a Java <code>boolean</code> to the underlying output |
* This method writes a Java <code>boolean</code> to the underlying output |
864 |
* stream. For a value of <code>true</code>, 1 is written to the stream. |
* stream. For a value of <code>true</code>, 1 is written to the stream. |
868 |
* |
* |
869 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
870 |
*/ |
*/ |
871 |
public final void writeBoolean (boolean b) throws IOException |
public final void writeBoolean (boolean val) throws IOException |
872 |
{ |
{ |
873 |
write (b ? 1 : 0); |
write (val ? 1 : 0); |
874 |
} |
} |
875 |
|
|
876 |
/** |
/** |
877 |
* This method writes a Java <code>byte</code> value to the underlying |
* This method writes a Java <code>byte</code> value to the underlying |
878 |
* output stream. |
* output stream. |
882 |
* |
* |
883 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
884 |
*/ |
*/ |
885 |
public final void writeByte(int b) throws IOException |
public final void writeByte (int v) throws IOException |
886 |
{ |
{ |
887 |
write (b & 0xFF); |
write (v & 0xFF); |
888 |
} |
} |
889 |
|
|
890 |
/** |
/** |
891 |
* This method writes all the bytes in a <code>String</code> out to the |
* This method writes a Java <code>short</code> to the stream, high byte |
892 |
* stream. One byte is written for each character in the <code>String</code>. |
* first. This method requires two bytes to encode the value. |
|
* The high eight bits of each character are discarded. |
|
893 |
* |
* |
894 |
* @param s The <code>String</code> to write to the stream |
* @param s The <code>short</code> value to write to the stream, |
895 |
|
* passed as an <code>int</code>. |
896 |
* |
* |
897 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
898 |
*/ |
*/ |
899 |
public final void writeBytes (String s) throws IOException |
public final synchronized void writeShort (int s) throws IOException |
900 |
{ |
{ |
901 |
int len = s.length(); |
buf[0] = (byte)((s & 0xFF00) >> 8); |
902 |
|
buf[1] = (byte)(s & 0x00FF); |
|
if (len == 0) |
|
|
return; |
|
|
|
|
|
byte[] buf = new byte[len]; |
|
|
|
|
|
for (int i = 0; i < len; i++) |
|
|
buf[i] = (byte)(s.charAt(i) & 0xFF); |
|
903 |
|
|
904 |
write(buf); |
write(buf, 0, 2); |
905 |
} |
} |
906 |
|
|
907 |
/** |
/** |
908 |
* This method writes a single <code>char</code> value to the stream, |
* This method writes a single <code>char</code> value to the stream, |
909 |
* high byte first. |
* high byte first. |
910 |
* |
* |
911 |
* @param c The <code>char</code> value to write, passed as |
* @param v The <code>char</code> value to write, passed as |
912 |
* an <code>int</code>. |
* an <code>int</code>. |
913 |
* |
* |
914 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
915 |
*/ |
*/ |
916 |
public final synchronized void writeChar(int c) throws IOException |
public final synchronized void writeChar (int v) throws IOException |
|
{ |
|
|
buf[0] = (byte)((c & 0xFF00) >> 8); |
|
|
buf[1] = (byte)((int)c & 0x00FF); |
|
|
|
|
|
write(buf, 0, 2); |
|
|
} |
|
|
|
|
|
/** |
|
|
* This method writes all the characters in a <code>String</code> to the |
|
|
* stream. There will be two bytes for each character value. The high |
|
|
* byte of the character will be written first. |
|
|
* |
|
|
* @param s The <code>String</code> to write to the stream. |
|
|
* |
|
|
* @exception IOException If an error occurs |
|
|
*/ |
|
|
public final void writeChars(String s) throws IOException |
|
|
{ |
|
|
int len = s.length(); |
|
|
if (len == 0) |
|
|
return; |
|
|
|
|
|
byte[] buf = new byte[len * 2]; |
|
|
|
|
|
for (int i = 0; i < len; i++) |
|
|
{ |
|
|
buf[i * 2] = (byte)((s.charAt(i) & 0xFF00) >> 8); |
|
|
buf[(i * 2) + 1] = (byte)(s.charAt(i) & 0x00FF); |
|
|
} |
|
|
|
|
|
write(buf, 0, buf.length); |
|
|
} |
|
|
|
|
|
/** |
|
|
* This method writes a Java <code>short</code> to the stream, high byte |
|
|
* first. This method requires two bytes to encode the value. |
|
|
* |
|
|
* @param s The <code>short</code> value to write to the stream, |
|
|
* passed as an <code>int</code>. |
|
|
* |
|
|
* @exception IOException If an error occurs |
|
|
*/ |
|
|
public final synchronized void writeShort(int s) throws IOException |
|
917 |
{ |
{ |
918 |
buf[0] = (byte)((s & 0xFF00) >> 8); |
buf[0] = (byte)((v & 0xFF00) >> 8); |
919 |
buf[1] = (byte)(s & 0x00FF); |
buf[1] = (byte)((int)v & 0x00FF); |
920 |
|
|
921 |
write(buf, 0, 2); |
write(buf, 0, 2); |
922 |
} |
} |
923 |
|
|
924 |
/** |
/** |
925 |
* This method writes a Java <code>int</code> to the stream, high bytes |
* This method writes a Java <code>int</code> to the stream, high bytes |
926 |
* first. This method requires four bytes to encode the value. |
* first. This method requires four bytes to encode the value. |
927 |
* |
* |
928 |
* @param i The <code>int</code> value to write to the stream. |
* @param v The <code>int</code> value to write to the stream. |
929 |
* |
* |
930 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
931 |
*/ |
*/ |
932 |
public final synchronized void writeInt(int i) throws IOException |
public final synchronized void writeInt (int v) throws IOException |
933 |
{ |
{ |
934 |
buf[0] = (byte)((i & 0xFF000000) >> 24); |
buf[0] = (byte)((v & 0xFF000000) >> 24); |
935 |
buf[1] = (byte)((i & 0x00FF0000) >> 16); |
buf[1] = (byte)((v & 0x00FF0000) >> 16); |
936 |
buf[2] = (byte)((i & 0x0000FF00) >> 8); |
buf[2] = (byte)((v & 0x0000FF00) >> 8); |
937 |
buf[3] = (byte)(i & 0x000000FF); |
buf[3] = (byte)(v & 0x000000FF); |
938 |
|
|
939 |
write(buf, 0, 4); |
write (buf, 0, 4); |
940 |
} |
} |
941 |
|
|
942 |
/** |
/** |
943 |
* This method writes a Java <code>long</code> to the stream, high bytes |
* This method writes a Java <code>long</code> to the stream, high bytes |
944 |
* first. This method requires eight bytes to encode the value. |
* first. This method requires eight bytes to encode the value. |
945 |
* |
* |
946 |
* @param l The <code>long</code> value to write to the stream. |
* @param v The <code>long</code> value to write to the stream. |
947 |
* |
* |
948 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
949 |
*/ |
*/ |
950 |
public final synchronized void writeLong(long l) throws IOException |
public final synchronized void writeLong (long v) throws IOException |
951 |
{ |
{ |
952 |
buf[0] = (byte)((l & 0xFF00000000000000L) >> 56); |
buf[0] = (byte)((v & 0xFF00000000000000L) >> 56); |
953 |
buf[1] = (byte)((l & 0x00FF000000000000L) >> 48); |
buf[1] = (byte)((v & 0x00FF000000000000L) >> 48); |
954 |
buf[2] = (byte)((l & 0x0000FF0000000000L) >> 40); |
buf[2] = (byte)((v & 0x0000FF0000000000L) >> 40); |
955 |
buf[3] = (byte)((l & 0x000000FF00000000L) >> 32); |
buf[3] = (byte)((v & 0x000000FF00000000L) >> 32); |
956 |
buf[4] = (byte)((l & 0x00000000FF000000L) >> 24); |
buf[4] = (byte)((v & 0x00000000FF000000L) >> 24); |
957 |
buf[5] = (byte)((l & 0x0000000000FF0000L) >> 16); |
buf[5] = (byte)((v & 0x0000000000FF0000L) >> 16); |
958 |
buf[6] = (byte)((l & 0x000000000000FF00L) >> 8); |
buf[6] = (byte)((v & 0x000000000000FF00L) >> 8); |
959 |
buf[7] = (byte)(l & 0x00000000000000FFL); |
buf[7] = (byte)(v & 0x00000000000000FFL); |
960 |
|
|
961 |
write(buf, 0, 8); |
write (buf, 0, 8); |
962 |
} |
} |
963 |
|
|
964 |
/** |
/** |
965 |
* This method writes a Java <code>float</code> value to the stream. This |
* This method writes a Java <code>float</code> value to the stream. This |
966 |
* value is written by first calling the method |
* value is written by first calling the method |
969 |
* then writing this <code>int</code> value to the stream exactly the same |
* then writing this <code>int</code> value to the stream exactly the same |
970 |
* as the <code>writeInt()</code> method does. |
* as the <code>writeInt()</code> method does. |
971 |
* |
* |
972 |
* @param f The floating point number to write to the stream. |
* @param v The floating point number to write to the stream. |
973 |
* |
* |
974 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
975 |
* |
* |
976 |
* @see #writeInt(int) |
* @see #writeInt(int) |
977 |
*/ |
*/ |
978 |
public final void writeFloat(float f) throws IOException |
public final void writeFloat (float v) throws IOException |
979 |
{ |
{ |
980 |
int i = Float.floatToIntBits(f); |
int i = Float.floatToIntBits (v); |
981 |
writeInt(i); |
writeInt (i); |
982 |
} |
} |
983 |
|
|
984 |
/** |
/** |
985 |
* This method writes a Java <code>double</code> value to the stream. This |
* This method writes a Java <code>double</code> value to the stream. This |
986 |
* value is written by first calling the method |
* value is written by first calling the method |
989 |
* then writing this <code>long</code> value to the stream exactly the same |
* then writing this <code>long</code> value to the stream exactly the same |
990 |
* as the <code>writeLong()</code> method does. |
* as the <code>writeLong()</code> method does. |
991 |
* |
* |
992 |
* @param d The double precision floating point number to write to the |
* @param v The double precision floating point number to write to the |
993 |
* stream. |
* stream. |
994 |
* |
* |
995 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
996 |
* |
* |
997 |
* @see #writeLong(long) |
* @see #writeLong(long) |
998 |
*/ |
*/ |
999 |
public final void writeDouble(double d) throws IOException |
public final void writeDouble (double v) throws IOException |
1000 |
|
{ |
1001 |
|
long l = Double.doubleToLongBits (v); |
1002 |
|
writeLong (l); |
1003 |
|
} |
1004 |
|
|
1005 |
|
/** |
1006 |
|
* This method writes all the bytes in a <code>String</code> out to the |
1007 |
|
* stream. One byte is written for each character in the <code>String</code>. |
1008 |
|
* The high eight bits of each character are discarded. |
1009 |
|
* |
1010 |
|
* @param s The <code>String</code> to write to the stream |
1011 |
|
* |
1012 |
|
* @exception IOException If an error occurs |
1013 |
|
*/ |
1014 |
|
public final void writeBytes (String s) throws IOException |
1015 |
|
{ |
1016 |
|
int len = s.length(); |
1017 |
|
|
1018 |
|
if (len == 0) |
1019 |
|
return; |
1020 |
|
|
1021 |
|
byte[] buf = new byte[len]; |
1022 |
|
|
1023 |
|
for (int i = 0; i < len; i++) |
1024 |
|
buf[i] = (byte)(s.charAt(i) & 0xFF); |
1025 |
|
|
1026 |
|
write(buf); |
1027 |
|
} |
1028 |
|
|
1029 |
|
/** |
1030 |
|
* This method writes all the characters in a <code>String</code> to the |
1031 |
|
* stream. There will be two bytes for each character value. The high |
1032 |
|
* byte of the character will be written first. |
1033 |
|
* |
1034 |
|
* @param s The <code>String</code> to write to the stream. |
1035 |
|
* |
1036 |
|
* @exception IOException If an error occurs |
1037 |
|
*/ |
1038 |
|
public final void writeChars (String s) throws IOException |
1039 |
{ |
{ |
1040 |
long l = Double.doubleToLongBits(d); |
int len = s.length(); |
1041 |
writeLong(l); |
if (len == 0) |
1042 |
|
return; |
1043 |
|
|
1044 |
|
byte[] buf = new byte[len * 2]; |
1045 |
|
|
1046 |
|
for (int i = 0; i < len; i++) |
1047 |
|
{ |
1048 |
|
buf[i * 2] = (byte)((s.charAt(i) & 0xFF00) >> 8); |
1049 |
|
buf[(i * 2) + 1] = (byte)(s.charAt(i) & 0x00FF); |
1050 |
|
} |
1051 |
|
|
1052 |
|
write(buf, 0, buf.length); |
1053 |
} |
} |
1054 |
|
|
1055 |
/** |
/** |
1081 |
* |
* |
1082 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
1083 |
*/ |
*/ |
1084 |
public final void writeUTF(String s) throws IOException |
public final void writeUTF (String s) throws IOException |
1085 |
{ |
{ |
1086 |
// FIXME: Look to migrate to s.getBytes("UTF-8") if performance ok |
// FIXME: Look to migrate to s.getBytes("UTF-8") if performance ok |
1087 |
byte[] buf = DataOutputStream.convertToUTF(s); |
byte[] buf = DataOutputStream.convertToUTF(s); |
1096 |
* A file channel must be created by first creating an instance of |
* A file channel must be created by first creating an instance of |
1097 |
* Input/Output/RandomAccessFile and invoking the getChannel() method on it. |
* Input/Output/RandomAccessFile and invoking the getChannel() method on it. |
1098 |
*/ |
*/ |
1099 |
public synchronized FileChannel getChannel() |
public synchronized FileChannel getChannel () |
1100 |
{ |
{ |
1101 |
if (ch == null) |
if (ch == null) |
1102 |
ch = new FileChannelImpl ((int) (fd.getNativeFd() & 0xFFFF), this); |
ch = new FileChannelImpl ((int) (fd.getNativeFd() & 0xFFFF), this); |