1 |
/* DataInputStream.java -- FilteredInputStream that implements DataInput |
/* DataInputStream.java -- FilteredInputStream that implements DataInput |
2 |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation |
Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
50 |
* |
* |
51 |
* @see DataInput |
* @see DataInput |
52 |
* |
* |
|
* @version 0.0 |
|
|
* |
|
53 |
* @author Warren Levy <warrenl@cygnus.com> |
* @author Warren Levy <warrenl@cygnus.com> |
54 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
55 |
* @date October 20, 1998. |
* @date October 20, 1998. |
132 |
*/ |
*/ |
133 |
public final boolean readBoolean() throws IOException |
public final boolean readBoolean() throws IOException |
134 |
{ |
{ |
135 |
return convertToBoolean(in.read()); |
int b = in.read(); |
136 |
|
if (b < 0) |
137 |
|
throw new EOFException(); |
138 |
|
else |
139 |
|
return(b != 0); |
140 |
} |
} |
141 |
|
|
142 |
/** |
/** |
156 |
*/ |
*/ |
157 |
public final byte readByte() throws IOException |
public final byte readByte() throws IOException |
158 |
{ |
{ |
159 |
return convertToByte(in.read()); |
int i = in.read(); |
160 |
|
if (i < 0) |
161 |
|
throw new EOFException(); |
162 |
|
return (byte) i; |
163 |
} |
} |
164 |
|
|
165 |
/** |
/** |
187 |
* |
* |
188 |
* @see DataOutput |
* @see DataOutput |
189 |
*/ |
*/ |
190 |
public final char readChar() throws IOException |
public synchronized final char readChar() throws IOException |
191 |
{ |
{ |
192 |
readFully (buf, 0, 2); |
readFully (buf, 0, 2); |
193 |
return convertToChar(buf); |
return (char) ((buf[0] << 8) | (buf[1] & 0xff)); |
194 |
} |
} |
195 |
|
|
196 |
/** |
/** |
315 |
* |
* |
316 |
* @see DataOutput |
* @see DataOutput |
317 |
*/ |
*/ |
318 |
public final int readInt() throws IOException |
public synchronized final int readInt() throws IOException |
319 |
{ |
{ |
320 |
readFully (buf, 0, 4); |
readFully (buf, 0, 4); |
321 |
return convertToInt(buf); |
return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | |
322 |
|
((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); |
323 |
} |
} |
324 |
|
|
325 |
/** |
/** |
464 |
* |
* |
465 |
* @see DataOutput |
* @see DataOutput |
466 |
*/ |
*/ |
467 |
public final long readLong() throws IOException |
public synchronized final long readLong() throws IOException |
468 |
{ |
{ |
469 |
readFully (buf, 0, 8); |
readFully (buf, 0, 8); |
470 |
return convertToLong(buf); |
return (((long)(buf[0] & 0xff) << 56) | |
471 |
|
((long)(buf[1] & 0xff) << 48) | |
472 |
|
((long)(buf[2] & 0xff) << 40) | |
473 |
|
((long)(buf[3] & 0xff) << 32) | |
474 |
|
((long)(buf[4] & 0xff) << 24) | |
475 |
|
((long)(buf[5] & 0xff) << 16) | |
476 |
|
((long)(buf[6] & 0xff) << 8) | |
477 |
|
((long)(buf[7] & 0xff))); |
478 |
} |
} |
479 |
|
|
480 |
/** |
/** |
504 |
* |
* |
505 |
* @see DataOutput |
* @see DataOutput |
506 |
*/ |
*/ |
507 |
public final short readShort() throws IOException |
public synchronized final short readShort() throws IOException |
508 |
{ |
{ |
509 |
readFully (buf, 0, 2); |
readFully (buf, 0, 2); |
510 |
return convertToShort(buf); |
return (short) ((buf[0] << 8) | (buf[1] & 0xff)); |
511 |
} |
} |
512 |
|
|
513 |
/** |
/** |
528 |
*/ |
*/ |
529 |
public final int readUnsignedByte() throws IOException |
public final int readUnsignedByte() throws IOException |
530 |
{ |
{ |
531 |
return convertToUnsignedByte(in.read()); |
int i = in.read(); |
532 |
|
if (i < 0) |
533 |
|
throw new EOFException(); |
534 |
|
return (i & 0xFF); |
535 |
} |
} |
536 |
|
|
537 |
/** |
/** |
559 |
* @exception EOFException If end of file is reached before reading the value |
* @exception EOFException If end of file is reached before reading the value |
560 |
* @exception IOException If any other error occurs |
* @exception IOException If any other error occurs |
561 |
*/ |
*/ |
562 |
public final int readUnsignedShort() throws IOException |
public final synchronized int readUnsignedShort() throws IOException |
563 |
{ |
{ |
564 |
readFully (buf, 0, 2); |
readFully (buf, 0, 2); |
565 |
return convertToUnsignedShort(buf); |
return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); |
566 |
} |
} |
567 |
|
|
568 |
/** |
/** |
614 |
* character encoding, then they would be converted to a Java |
* character encoding, then they would be converted to a Java |
615 |
* <code>char</code> like so: |
* <code>char</code> like so: |
616 |
* <p> |
* <p> |
617 |
* <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F))</code> |
* <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | |
618 |
|
* (byte3 & 0x3F))</code> |
619 |
* <p> |
* <p> |
620 |
* Note that all characters are encoded in the method that requires |
* Note that all characters are encoded in the method that requires |
621 |
* the fewest number of bytes with the exception of the character |
* the fewest number of bytes with the exception of the character |
638 |
*/ |
*/ |
639 |
public final String readUTF() throws IOException |
public final String readUTF() throws IOException |
640 |
{ |
{ |
641 |
return readUTF(this); |
int UTFlen = readUnsignedShort(); |
642 |
|
byte[] buf = new byte[UTFlen]; |
643 |
|
|
644 |
|
// This blocks until the entire string is available rather than |
645 |
|
// doing partial processing on the bytes that are available and then |
646 |
|
// blocking. An advantage of the latter is that Exceptions |
647 |
|
// could be thrown earlier. The former is a bit cleaner. |
648 |
|
readFully(buf, 0, UTFlen); |
649 |
|
|
650 |
|
// FIXME: This should probably be replaced with something like |
651 |
|
// new String(buf, "UTF-8"), however that might take a performance |
652 |
|
// due to time looking up encoders. But having a private UTF |
653 |
|
// converter may not be the way to go either. |
654 |
|
return (convertFromUTF(buf)); |
655 |
} |
} |
656 |
|
|
657 |
/** |
/** |
707 |
return n; |
return n; |
708 |
} |
} |
709 |
|
|
710 |
static boolean convertToBoolean(int b) throws EOFException |
// FIXME: This method should be re-thought. I suspect we have multiple |
711 |
{ |
// UTF-8 decoders floating around. We should use the standard charset |
712 |
if (b < 0) |
// converters, maybe and adding a direct call into one of the new |
713 |
throw new EOFException(); |
// NIO converters for a super-fast UTF8 decode. |
|
return (b != 0); |
|
|
} |
|
|
|
|
|
static byte convertToByte(int i) throws EOFException |
|
|
{ |
|
|
if (i < 0) |
|
|
throw new EOFException(); |
|
|
return (byte) i; |
|
|
} |
|
|
|
|
|
static int convertToUnsignedByte(int i) throws EOFException |
|
|
{ |
|
|
if (i < 0) |
|
|
throw new EOFException(); |
|
|
return (i & 0xFF); |
|
|
} |
|
|
|
|
|
static char convertToChar(byte[] buf) |
|
|
{ |
|
|
return (char) ((buf[0] << 8) | (buf[1] & 0xff)); |
|
|
} |
|
|
|
|
|
static short convertToShort(byte[] buf) |
|
|
{ |
|
|
return (short) ((buf[0] << 8) | (buf[1] & 0xff)); |
|
|
} |
|
|
|
|
|
static int convertToUnsignedShort(byte[] buf) |
|
|
{ |
|
|
return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff)); |
|
|
} |
|
|
|
|
|
static int convertToInt(byte[] buf) |
|
|
{ |
|
|
return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | |
|
|
((buf[2] & 0xff) << 8) | (buf[3] & 0xff)); |
|
|
} |
|
|
|
|
|
static long convertToLong(byte[] buf) |
|
|
{ |
|
|
return (((long)(buf[0] & 0xff) << 56) | |
|
|
((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))); |
|
|
} |
|
|
|
|
714 |
static String convertFromUTF(byte[] buf) |
static String convertFromUTF(byte[] buf) |
715 |
throws EOFException, UTFDataFormatException |
throws EOFException, UTFDataFormatException |
716 |
{ |
{ |