/[classpath]/classpath/java/io/DataInputStream.java
ViewVC logotype

Diff of /classpath/java/io/DataInputStream.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.16 by mark, Fri Oct 25 13:24:41 2002 UTC revision 1.17 by arenn, Sun Mar 2 01:24:34 2003 UTC
# Line 1  Line 1 
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    
# Line 50  package java.io; Line 50  package java.io;
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.  
# Line 134  public class DataInputStream extends Fil Line 132  public class DataInputStream extends Fil
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    /**    /**
# Line 154  public class DataInputStream extends Fil Line 156  public class DataInputStream extends Fil
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    /**    /**
# Line 182  public class DataInputStream extends Fil Line 187  public class DataInputStream extends Fil
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    /**    /**
# Line 310  public class DataInputStream extends Fil Line 315  public class DataInputStream extends Fil
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    /**    /**
# Line 458  public class DataInputStream extends Fil Line 464  public class DataInputStream extends Fil
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    /**    /**
# Line 491  public class DataInputStream extends Fil Line 504  public class DataInputStream extends Fil
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    /**    /**
# Line 515  public class DataInputStream extends Fil Line 528  public class DataInputStream extends Fil
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    /**    /**
# Line 543  public class DataInputStream extends Fil Line 559  public class DataInputStream extends Fil
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    /**    /**
# Line 598  public class DataInputStream extends Fil Line 614  public class DataInputStream extends Fil
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
# Line 621  public class DataInputStream extends Fil Line 638  public class DataInputStream extends Fil
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    /**    /**
# Line 677  public class DataInputStream extends Fil Line 707  public class DataInputStream extends Fil
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    {    {

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.17

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26