Source for java.io.DataInputStream

   1: /* DataInputStream.java -- FilteredInputStream that implements DataInput
   2:    Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2008
   3:    Free Software Foundation
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11:  
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38:  
  39: package java.io;
  40: 
  41: /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  42:  * "The Java Language Specification", ISBN 0-201-63451-1
  43:  * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  44:  * Status:  Believed complete and correct.
  45:  */
  46:  
  47: /**
  48:  * This subclass of <code>FilteredInputStream</code> implements the
  49:  * <code>DataInput</code> interface that provides method for reading primitive
  50:  * Java data types from a stream.
  51:  *
  52:  * @see DataInput
  53:  *
  54:  * @author Warren Levy (warrenl@cygnus.com)
  55:  * @author Aaron M. Renn (arenn@urbanophile.com)
  56:  * @date October 20, 1998.  
  57:  */
  58: public class DataInputStream extends FilterInputStream implements DataInput
  59: {
  60:   // Byte buffer, used to make primitive read calls more efficient.
  61:   byte[] buf = new byte [8];
  62:   
  63:   /**
  64:    * This constructor initializes a new <code>DataInputStream</code>
  65:    * to read from the specified subordinate stream.
  66:    *
  67:    * @param in The subordinate <code>InputStream</code> to read from
  68:    */
  69:   public DataInputStream (InputStream in)
  70:   {
  71:     super (in);
  72:   }
  73: 
  74:   /**
  75:    * This method reads bytes from the underlying stream into the specified
  76:    * byte array buffer.  It will attempt to fill the buffer completely, but
  77:    * may return a short count if there is insufficient data remaining to be
  78:    * read to fill the buffer.
  79:    *
  80:    * @param b The buffer into which bytes will be read.
  81:    * 
  82:    * @return The actual number of bytes read, or -1 if end of stream reached 
  83:    * before reading any bytes.
  84:    *
  85:    * @exception IOException If an error occurs.
  86:    */
  87:   public final int read (byte[] b) throws IOException
  88:   {
  89:     return in.read (b, 0, b.length);
  90:   }
  91: 
  92:   /**
  93:    * This method reads bytes from the underlying stream into the specified
  94:    * byte array buffer.  It will attempt to read <code>len</code> bytes and
  95:    * will start storing them at position <code>off</code> into the buffer.
  96:    * This method can return a short count if there is insufficient data
  97:    * remaining to be read to complete the desired read length.
  98:    *
  99:    * @param b The buffer into which bytes will be read.
 100:    * @param off The offset into the buffer to start storing bytes.
 101:    * @param len The requested number of bytes to read.
 102:    *
 103:    * @return The actual number of bytes read, or -1 if end of stream reached
 104:    * before reading any bytes.
 105:    *
 106:    * @exception IOException If an error occurs.
 107:    */
 108:   public final int read (byte[] b, int off, int len) throws IOException
 109:   {
 110:     return in.read (b, off, len);
 111:   }
 112: 
 113:   /**
 114:    * This method reads a Java boolean value from an input stream.  It does
 115:    * so by reading a single byte of data.  If that byte is zero, then the
 116:    * value returned is <code>false</code>.  If the byte is non-zero, then
 117:    * the value returned is <code>true</code>.
 118:    * <p>
 119:    * This method can read a <code>boolean</code> written by an object
 120:    * implementing the <code>writeBoolean()</code> method in the
 121:    * <code>DataOutput</code> interface. 
 122:    *
 123:    * @return The <code>boolean</code> value read
 124:    *
 125:    * @exception EOFException If end of file is reached before reading
 126:    * the boolean
 127:    * @exception IOException If any other error occurs
 128:    *
 129:    * @see DataOutput#writeBoolean
 130:    */
 131:   public final boolean readBoolean () throws IOException
 132:   {
 133:     return convertToBoolean (in.read ());
 134:   }
 135: 
 136:   /**
 137:    * This method reads a Java byte value from an input stream.  The value
 138:    * is in the range of -128 to 127.
 139:    * <p>
 140:    * This method can read a <code>byte</code> written by an object
 141:    * implementing the <code>writeByte()</code> method in the
 142:    * <code>DataOutput</code> interface.
 143:    *
 144:    * @return The <code>byte</code> value read
 145:    *
 146:    * @exception EOFException If end of file is reached before reading the byte
 147:    * @exception IOException If any other error occurs
 148:    *
 149:    * @see DataOutput#writeByte
 150:    */
 151:   public final byte readByte () throws IOException
 152:   {
 153:     return convertToByte (in.read ());
 154:   }
 155: 
 156:   /**
 157:    * This method reads a Java <code>char</code> value from an input stream.  
 158:    * It operates by reading two bytes from the stream and converting them to 
 159:    * a single 16-bit Java <code>char</code>.  The two bytes are stored most
 160:    * significant byte first (i.e., "big endian") regardless of the native
 161:    * host byte ordering. 
 162:    * <p>
 163:    * As an example, if <code>byte1</code> and <code>byte2</code>
 164:    * represent the first and second byte read from the stream
 165:    * respectively, they will be transformed to a <code>char</code> in
 166:    * the following manner: 
 167:    * <p>
 168:    * <code>(char)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
 169:    * <p>
 170:    * This method can read a <code>char</code> written by an object
 171:    * implementing the <code>writeChar()</code> method in the
 172:    * <code>DataOutput</code> interface. 
 173:    *
 174:    * @return The <code>char</code> value read 
 175:    *
 176:    * @exception EOFException If end of file is reached before reading the char
 177:    * @exception IOException If any other error occurs
 178:    *
 179:    * @see DataOutput#writeChar
 180:    */
 181:   public final char readChar () throws IOException
 182:   {
 183:     readFully (buf, 0, 2);
 184:     return convertToChar (buf);
 185:   }
 186: 
 187:   /**
 188:    * This method reads a Java double value from an input stream.  It operates
 189:    * by first reading a <code>long</code> value from the stream by calling the
 190:    * <code>readLong()</code> method in this interface, then converts
 191:    * that <code>long</code> to a <code>double</code> using the
 192:    * <code>longBitsToDouble</code> method in the class
 193:    * <code>java.lang.Double</code> 
 194:    * <p>
 195:    * This method can read a <code>double</code> written by an object
 196:    * implementing the <code>writeDouble()</code> method in the
 197:    * <code>DataOutput</code> interface.
 198:    *
 199:    * @return The <code>double</code> value read
 200:    *
 201:    * @exception EOFException If end of file is reached before reading
 202:    * the double
 203:    * @exception IOException If any other error occurs
 204:    *
 205:    * @see DataOutput#writeDouble
 206:    * @see java.lang.Double#longBitsToDouble
 207:    */
 208:   public final double readDouble () throws IOException
 209:   {
 210:     return Double.longBitsToDouble (readLong ());
 211:   }
 212: 
 213:   /**
 214:    * This method reads a Java float value from an input stream.  It
 215:    * operates by first reading an <code>int</code> value from the
 216:    * stream by calling the <code>readInt()</code> method in this
 217:    * interface, then converts that <code>int</code> to a
 218:    * <code>float</code> using the <code>intBitsToFloat</code> method
 219:    * in the class <code>java.lang.Float</code>
 220:    * <p>
 221:    * This method can read a <code>float</code> written by an object
 222:    * implementing the <code>writeFloat()</code> method in the
 223:    * <code>DataOutput</code> interface.
 224:    *
 225:    * @return The <code>float</code> value read
 226:    *
 227:    * @exception EOFException If end of file is reached before reading the float
 228:    * @exception IOException If any other error occurs
 229:    *
 230:    * @see DataOutput#writeFloat 
 231:    * @see java.lang.Float#intBitsToFloat
 232:    */
 233:   public final float readFloat () throws IOException
 234:   {
 235:     return Float.intBitsToFloat (readInt ());
 236:   }
 237: 
 238:   /**
 239:    * This method reads raw bytes into the passed array until the array is
 240:    * full.  Note that this method blocks until the data is available and
 241:    * throws an exception if there is not enough data left in the stream to
 242:    * fill the buffer.  Note also that zero length buffers are permitted.
 243:    * In this case, the method will return immediately without reading any
 244:    * bytes from the stream.
 245:    *
 246:    * @param b The buffer into which to read the data
 247:    *
 248:    * @exception EOFException If end of file is reached before filling the
 249:    * buffer
 250:    * @exception IOException If any other error occurs
 251:    */
 252:   public final void readFully (byte[] b) throws IOException
 253:   {
 254:     readFully (b, 0, b.length);
 255:   }
 256: 
 257:   /**
 258:    * This method reads raw bytes into the passed array <code>buf</code>
 259:    * starting
 260:    * <code>offset</code> bytes into the buffer.  The number of bytes read
 261:    * will be
 262:    * exactly <code>len</code>.  Note that this method blocks until the data is
 263:    * available and throws an exception if there is not enough data left in
 264:    * the stream to read <code>len</code> bytes.  Note also that zero length
 265:    * buffers are permitted.  In this case, the method will return immediately
 266:    * without reading any bytes from the stream.
 267:    *
 268:    * @param buf The buffer into which to read the data
 269:    * @param offset The offset into the buffer to start storing data
 270:    * @param len The number of bytes to read into the buffer
 271:    *
 272:    * @exception EOFException If end of file is reached before filling the
 273:    * buffer
 274:    * @exception IOException If any other error occurs
 275:    */
 276:   public final void readFully (byte[] buf, int offset, int len) throws IOException
 277:   {
 278:     if (len < 0)
 279:       throw new IndexOutOfBoundsException("Negative length: " + len);
 280:     
 281:     while (len > 0)
 282:       {
 283:     // in.read will block until some data is available.
 284:     int numread = in.read (buf, offset, len);
 285:     if (numread < 0)
 286:       throw new EOFException ();
 287:     len -= numread;
 288:     offset += numread;
 289:       }
 290:   }
 291: 
 292:   /**
 293:    * This method reads a Java <code>int</code> value from an input stream
 294:    * It operates by reading four bytes from the stream and converting them to
 295:    * a single Java <code>int</code>.  The bytes are stored most
 296:    * significant byte first (i.e., "big endian") regardless of the native
 297:    * host byte ordering.
 298:    * <p>
 299:    * As an example, if <code>byte1</code> through <code>byte4</code> represent
 300:    * the first four bytes read from the stream, they will be
 301:    * transformed to an <code>int</code> in the following manner:
 302:    * <p>
 303:    * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 24) + ((byte2 &amp; 0xFF) &lt;&lt; 16) +
 304:    * ((byte3 &amp; 0xFF)&lt;&lt; 8) + (byte4 &amp; 0xFF)))</code>
 305:    * <p>
 306:    * The value returned is in the range of -2147483648 to 2147483647.
 307:    * <p>
 308:    * This method can read an <code>int</code> written by an object
 309:    * implementing the <code>writeInt()</code> method in the
 310:    * <code>DataOutput</code> interface.
 311:    *
 312:    * @return The <code>int</code> value read
 313:    *
 314:    * @exception EOFException If end of file is reached before reading the int
 315:    * @exception IOException If any other error occurs
 316:    *
 317:    * @see DataOutput#writeInt
 318:    */
 319:   public final int readInt () throws IOException
 320:   {
 321:     readFully (buf, 0, 4);
 322:     return convertToInt (buf);
 323:   }
 324: 
 325:   /**
 326:    * This method reads the next line of text data from an input
 327:    * stream.  It operates by reading bytes and converting those bytes
 328:    * to <code>char</code> values by treating the byte read as the low
 329:    * eight bits of the <code>char</code> and using 0 as the high eight
 330:    * bits.  Because of this, it does not support the full 16-bit
 331:    * Unicode character set.
 332:    * <p>
 333:    * The reading of bytes ends when either the end of file or a line
 334:    * terminator is encountered.  The bytes read are then returned as a
 335:    * <code>String</code> A line terminator is a byte sequence
 336:    * consisting of either <code>\r</code>, <code>\n</code> or
 337:    * <code>\r\n</code>.  These termination charaters are discarded and
 338:    * are not returned as part of the string.
 339:    * <p>
 340:    * This method can read data that was written by an object implementing the
 341:    * <code>writeLine()</code> method in <code>DataOutput</code>.
 342:    *
 343:    * @return The line read as a <code>String</code>
 344:    *
 345:    * @exception IOException If an error occurs
 346:    *
 347:    * @see DataOutput
 348:    *
 349:    * @deprecated
 350:    */
 351:   public final String readLine() throws IOException
 352:   {
 353:     StringBuilder strb = new StringBuilder();
 354: 
 355:     while (true)
 356:       {
 357:         int c = in.read();
 358:     if (c == -1)    // got an EOF
 359:         return strb.length() > 0 ? strb.toString() : null;
 360:     if (c == '\r')
 361:       {
 362:         int next_c = in.read();
 363:             if (next_c != '\n' && next_c != -1)
 364:               {
 365:                 if (!(in instanceof PushbackInputStream))
 366:                   in = new PushbackInputStream(in);
 367:                 ((PushbackInputStream) in).unread(next_c);
 368:               }
 369:             break;
 370:       }
 371:         if (c == '\n')
 372:             break;
 373:     strb.append((char) c);
 374:       }
 375: 
 376:     return strb.length() > 0 ? strb.toString() : "";
 377:   }
 378: 
 379:   /**
 380:    * This method reads a Java <code>long</code> value from an input stream
 381:    * It operates by reading eight bytes from the stream and converting them to
 382:    * a single Java <code>long</code>.  The bytes are stored most
 383:    * significant byte first (i.e., "big endian") regardless of the native
 384:    * host byte ordering.
 385:    * <p>
 386:    * As an example, if <code>byte1</code> through <code>byte8</code> represent
 387:    * the first eight bytes read from the stream, they will be
 388:    * transformed to an <code>long</code> in the following manner:
 389:    * <p>
 390:    * <code>(long)(((byte1 &amp; 0xFF) &lt;&lt; 56) + ((byte2 &amp; 0xFF) &lt;&lt; 48) +
 391:    * ((byte3 &amp; 0xFF) &lt;&lt; 40) + ((byte4 &amp; 0xFF) &lt;&lt; 32) +
 392:    * ((byte5 &amp; 0xFF) &lt;&lt; 24) + ((byte6 &amp; 0xFF) &lt;&lt; 16) +
 393:    * ((byte7 &amp; 0xFF) &lt;&lt; 8) + (byte8 &amp; 0xFF)))
 394:    * </code>
 395:    * <p>
 396:    * The value returned is in the range of -9223372036854775808 to
 397:    * 9223372036854775807.
 398:    * <p>
 399:    * This method can read an <code>long</code> written by an object
 400:    * implementing the <code>writeLong()</code> method in the
 401:    * <code>DataOutput</code> interface.
 402:    *
 403:    * @return The <code>long</code> value read
 404:    *
 405:    * @exception EOFException If end of file is reached before reading the long
 406:    * @exception IOException If any other error occurs
 407:    *
 408:    * @see DataOutput#writeLong
 409:    */
 410:   public final long readLong () throws IOException
 411:   {
 412:     readFully (buf, 0, 8);
 413:     return convertToLong (buf);
 414:   }
 415: 
 416:   /**
 417:    * This method reads a signed 16-bit value into a Java in from the
 418:    * stream.  It operates by reading two bytes from the stream and
 419:    * converting them to a single 16-bit Java <code>short</code>.  The
 420:    * two bytes are stored most significant byte first (i.e., "big
 421:    * endian") regardless of the native host byte ordering.
 422:    * <p>
 423:    * As an example, if <code>byte1</code> and <code>byte2</code>
 424:    * represent the first and second byte read from the stream
 425:    * respectively, they will be transformed to a <code>short</code>. in
 426:    * the following manner:
 427:    * <p>
 428:    * <code>(short)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF))</code>
 429:    * <p>
 430:    * The value returned is in the range of -32768 to 32767.
 431:    * <p>
 432:    * This method can read a <code>short</code> written by an object
 433:    * implementing the <code>writeShort()</code> method in the
 434:    * <code>DataOutput</code> interface.
 435:    *
 436:    * @return The <code>short</code> value read
 437:    *
 438:    * @exception EOFException If end of file is reached before reading the value
 439:    * @exception IOException If any other error occurs
 440:    *
 441:    * @see DataOutput#writeShort
 442:    */
 443:   public final short readShort () throws IOException
 444:   {
 445:     readFully (buf, 0, 2);
 446:     return convertToShort (buf);
 447:   }
 448:   
 449:   /**
 450:    * This method reads 8 unsigned bits into a Java <code>int</code>
 451:    * value from the stream. The value returned is in the range of 0 to
 452:    * 255.
 453:    * <p>
 454:    * This method can read an unsigned byte written by an object
 455:    * implementing the <code>writeUnsignedByte()</code> method in the
 456:    * <code>DataOutput</code> interface.
 457:    *
 458:    * @return The unsigned bytes value read as a Java <code>int</code>.
 459:    *
 460:    * @exception EOFException If end of file is reached before reading the value
 461:    * @exception IOException If any other error occurs
 462:    *
 463:    * @see DataOutput#writeByte
 464:    */
 465:   public final int readUnsignedByte () throws IOException
 466:   {
 467:     return convertToUnsignedByte (in.read ());
 468:   }
 469: 
 470:   /**
 471:    * This method reads 16 unsigned bits into a Java int value from the stream.
 472:    * It operates by reading two bytes from the stream and converting them to 
 473:    * a single Java <code>int</code>  The two bytes are stored most
 474:    * significant byte first (i.e., "big endian") regardless of the native
 475:    * host byte ordering. 
 476:    * <p>
 477:    * As an example, if <code>byte1</code> and <code>byte2</code>
 478:    * represent the first and second byte read from the stream
 479:    * respectively, they will be transformed to an <code>int</code> in
 480:    * the following manner:
 481:    * <p>
 482:    * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 8) + (byte2 &amp; 0xFF))</code>
 483:    * <p>
 484:    * The value returned is in the range of 0 to 65535.
 485:    * <p>
 486:    * This method can read an unsigned short written by an object
 487:    * implementing the <code>writeUnsignedShort()</code> method in the
 488:    * <code>DataOutput</code> interface.
 489:    *
 490:    * @return The unsigned short value read as a Java <code>int</code>
 491:    *
 492:    * @exception EOFException If end of file is reached before reading the value
 493:    * @exception IOException If any other error occurs
 494:    *
 495:    * @see DataOutput#writeShort
 496:    */
 497:   public final int readUnsignedShort () throws IOException
 498:   {
 499:     readFully (buf, 0, 2);
 500:     return convertToUnsignedShort (buf);
 501:   }
 502: 
 503:   /**
 504:    * This method reads a <code>String</code> from an input stream that
 505:    * is encoded in a modified UTF-8 format.  This format has a leading
 506:    * two byte sequence that contains the remaining number of bytes to
 507:    * read.  This two byte sequence is read using the
 508:    * <code>readUnsignedShort()</code> method of this interface.
 509:    * <p>
 510:    * After the number of remaining bytes have been determined, these
 511:    * bytes are read an transformed into <code>char</code> values.
 512:    * These <code>char</code> values are encoded in the stream using
 513:    * either a one, two, or three byte format.  The particular format
 514:    * in use can be determined by examining the first byte read.
 515:    * <p>
 516:    * If the first byte has a high order bit of 0, then that character
 517:    * consists on only one byte.  This character value consists of
 518:    * seven bits that are at positions 0 through 6 of the byte.  As an
 519:    * example, if <code>byte1</code> is the byte read from the stream,
 520:    * it would be converted to a <code>char</code> like so:
 521:    * <p>
 522:    * <code>(char)byte1</code>
 523:    * <p>
 524:    * If the first byte has 110 as its high order bits, then the 
 525:    * character consists of two bytes.  The bits that make up the character
 526:    * value are in positions 0 through 4 of the first byte and bit positions
 527:    * 0 through 5 of the second byte.  (The second byte should have 
 528:    * 10 as its high order bits).  These values are in most significant
 529:    * byte first (i.e., "big endian") order.
 530:    * <p>
 531:    * As an example, if <code>byte1</code> and <code>byte2</code> are
 532:    * the first two bytes read respectively, and the high order bits of
 533:    * them match the patterns which indicate a two byte character
 534:    * encoding, then they would be converted to a Java
 535:    * <code>char</code> like so:
 536:    * <p>
 537:    * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
 538:    * <p>
 539:    * If the first byte has a 1110 as its high order bits, then the
 540:    * character consists of three bytes.  The bits that make up the character
 541:    * value are in positions 0 through 3 of the first byte and bit positions
 542:    * 0 through 5 of the other two bytes.  (The second and third bytes should
 543:    * have 10 as their high order bits).  These values are in most
 544:    * significant byte first (i.e., "big endian") order.
 545:    * <p>
 546:    * As an example, if <code>byte1</code> <code>byte2</code> and
 547:    * <code>byte3</code> are the three bytes read, and the high order
 548:    * bits of them match the patterns which indicate a three byte
 549:    * character encoding, then they would be converted to a Java
 550:    * <code>char</code> like so:
 551:    * <p>
 552:    * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | 
 553:    * (byte3 & 0x3F))</code>
 554:    * <p>
 555:    * Note that all characters are encoded in the method that requires
 556:    * the fewest number of bytes with the exception of the character
 557:    * with the value of <code>&#92;u0000</code> which is encoded as two
 558:    * bytes.  This is a modification of the UTF standard used to
 559:    * prevent C language style <code>NUL</code> values from appearing
 560:    * in the byte stream.
 561:    * <p>
 562:    * This method can read data that was written by an object implementing the
 563:    * <code>writeUTF()</code> method in <code>DataOutput</code>
 564:    * 
 565:    * @return The <code>String</code> read
 566:    *
 567:    * @exception EOFException If end of file is reached before reading
 568:    * the String
 569:    * @exception UTFDataFormatException If the data is not in UTF-8 format
 570:    * @exception IOException If any other error occurs
 571:    *
 572:    * @see DataOutput#writeUTF
 573:    */
 574:   public final String readUTF () throws IOException
 575:   {
 576:     return readUTF (this);
 577:   }
 578: 
 579:   /**
 580:    * This method reads a String encoded in UTF-8 format from the 
 581:    * specified <code>DataInput</code> source.
 582:    *
 583:    * @param in The <code>DataInput</code> source to read from
 584:    *
 585:    * @return The String read from the source
 586:    *
 587:    * @exception IOException If an error occurs
 588:    *
 589:    * @see DataInput#readUTF
 590:    */
 591:   public static final String readUTF(DataInput in) throws IOException
 592:   {
 593:     final int UTFlen = in.readUnsignedShort ();
 594:     
 595:     return readUTF(in, UTFlen);
 596:   }
 597: 
 598:   /**
 599:    * This method is similar to <code>readUTF</code>, but the
 600:    * UTF-8 byte length is in 64 bits.
 601:    * This method is not public. It is used by <code>ObjectInputStream</code>.
 602:    * 
 603:    * @return The <code>String</code> read
 604:    *
 605:    * @exception EOFException If end of file is reached before reading
 606:    * the String
 607:    * @exception UTFDataFormatException If the data is not in UTF-8 format
 608:    * @exception IOException If any other error occurs
 609:    *
 610:    * @see DataOutput#writeUTFLong
 611:    */
 612:   final String readUTFLong () throws IOException
 613:   {
 614:     long l = readLong ();
 615:     if (l > Integer.MAX_VALUE)
 616:       throw new IOException("The string length > Integer.MAX_VALUE");
 617:     final int UTFlen = (int)l;
 618:     return readUTF (this, UTFlen);
 619:   }
 620: 
 621:   /**
 622:    * This method performs the main task of <code>readUTF</code> and
 623:    * <code>readUTFLong</code>.
 624:    *
 625:    * @param in The <code>DataInput</code> source to read from
 626:    *
 627:    * @param len The UTF-8 byte length of the String to be read
 628:    *
 629:    * @return The String read from the source
 630:    *
 631:    * @exception IOException If an error occurs
 632:    *
 633:    * @see DataInput#readUTF
 634:    */
 635:   private static final String readUTF(DataInput in, int len) throws IOException
 636:   {
 637:     byte[] buf = new byte [len];
 638: 
 639:     // This blocks until the entire string is available rather than
 640:     // doing partial processing on the bytes that are available and then
 641:     // blocking.  An advantage of the latter is that Exceptions
 642:     // could be thrown earlier.  The former is a bit cleaner.
 643:     in.readFully (buf, 0, len);
 644: 
 645:     return convertFromUTF (buf);
 646:   }
 647: 
 648:   /**
 649:    * This method attempts to skip and discard the specified number of bytes 
 650:    * in the input stream.  It may actually skip fewer bytes than requested. 
 651:    * This method will not skip any bytes if passed a negative number of bytes 
 652:    * to skip. 
 653:    *
 654:    * @param n The requested number of bytes to skip.
 655:    *
 656:    * @return The requested number of bytes to skip.
 657:    *
 658:    * @exception IOException If an error occurs.
 659:    * @specnote The JDK docs claim that this returns the number of bytes 
 660:    *  actually skipped. The JCL claims that this method can throw an 
 661:    *  EOFException. Neither of these appear to be true in the JDK 1.3's
 662:    *  implementation. This tries to implement the actual JDK behaviour.
 663:    */
 664:   public final int skipBytes (int n) throws IOException
 665:   {
 666:     if (n <= 0)
 667:       return 0;    
 668:     try
 669:       {
 670:         return (int) in.skip (n);
 671:       }
 672:     catch (EOFException x)
 673:       {
 674:         // do nothing.
 675:       }         
 676:     return n;
 677:   }
 678:   
 679:   static boolean convertToBoolean (int b) throws EOFException
 680:   {
 681:     if (b < 0)
 682:       throw new EOFException ();
 683:     
 684:     return (b != 0);
 685:   }
 686: 
 687:   static byte convertToByte (int i) throws EOFException
 688:   {
 689:     if (i < 0)
 690:       throw new EOFException ();
 691:     
 692:     return (byte) i;
 693:   }
 694: 
 695:   static int convertToUnsignedByte (int i) throws EOFException
 696:   {
 697:     if (i < 0)
 698:       throw new EOFException ();
 699:     
 700:     return (i & 0xFF);
 701:   }
 702: 
 703:   static char convertToChar (byte[] buf)
 704:   {
 705:     return (char) ((buf [0] << 8)
 706:             | (buf [1] & 0xff));  
 707:   }  
 708: 
 709:   static short convertToShort (byte[] buf)
 710:   {
 711:     return (short) ((buf [0] << 8)
 712:             | (buf [1] & 0xff));  
 713:   }  
 714: 
 715:   static int convertToUnsignedShort (byte[] buf)
 716:   {
 717:     return (((buf [0] & 0xff) << 8)
 718:         | (buf [1] & 0xff));  
 719:   }
 720: 
 721:   static int convertToInt (byte[] buf)
 722:   {
 723:     return (((buf [0] & 0xff) << 24)
 724:         | ((buf [1] & 0xff) << 16)
 725:         | ((buf [2] & 0xff) << 8)
 726:         | (buf [3] & 0xff));  
 727:   }
 728: 
 729:   static long convertToLong (byte[] buf)
 730:   {
 731:     return (((long)(buf [0] & 0xff) << 56) |
 732:         ((long)(buf [1] & 0xff) << 48) |
 733:         ((long)(buf [2] & 0xff) << 40) |
 734:         ((long)(buf [3] & 0xff) << 32) |
 735:         ((long)(buf [4] & 0xff) << 24) |
 736:         ((long)(buf [5] & 0xff) << 16) |
 737:         ((long)(buf [6] & 0xff) <<  8) |
 738:         ((long)(buf [7] & 0xff)));  
 739:   }
 740: 
 741:   // FIXME: This method should be re-thought.  I suspect we have multiple
 742:   // UTF-8 decoders floating around.  We should use the standard charset
 743:   // converters, maybe and adding a direct call into one of the new
 744:   // NIO converters for a super-fast UTF8 decode.
 745:   static String convertFromUTF (byte[] buf) 
 746:     throws EOFException, UTFDataFormatException
 747:   {
 748:     // Give StringBuffer an initial estimated size to avoid 
 749:     // enlarge buffer frequently
 750:     StringBuilder strbuf = new StringBuilder (buf.length / 2 + 2);
 751: 
 752:     for (int i = 0; i < buf.length; )
 753:       {
 754:     if ((buf [i] & 0x80) == 0)        // bit pattern 0xxxxxxx
 755:       strbuf.append ((char) (buf [i++] & 0xFF));
 756:     else if ((buf [i] & 0xE0) == 0xC0)    // bit pattern 110xxxxx
 757:       {
 758:         if (i + 1 >= buf.length
 759:         || (buf [i + 1] & 0xC0) != 0x80)
 760:           throw new UTFDataFormatException ();
 761: 
 762:         strbuf.append((char) (((buf [i++] & 0x1F) << 6)
 763:                   | (buf [i++] & 0x3F)));
 764:       }
 765:     else if ((buf [i] & 0xF0) == 0xE0)    // bit pattern 1110xxxx
 766:       {
 767:         if (i + 2 >= buf.length
 768:         || (buf [i + 1] & 0xC0) != 0x80
 769:         || (buf [i + 2] & 0xC0) != 0x80)
 770:           throw new UTFDataFormatException ();
 771: 
 772:         strbuf.append ((char) (((buf [i++] & 0x0F) << 12)
 773:                    | ((buf [i++] & 0x3F) << 6)
 774:                    | (buf [i++] & 0x3F)));
 775:       }
 776:     else // must be ((buf [i] & 0xF0) == 0xF0 || (buf [i] & 0xC0) == 0x80)
 777:       throw new UTFDataFormatException ();    // bit patterns 1111xxxx or
 778:                         //         10xxxxxx
 779:       }
 780: 
 781:     return strbuf.toString ();
 782:   }
 783: }