This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Old patch for DataInpuStream from Classpath


Hi,

The following patch was made last month to the Classpath version of
java.io.InputStream but was not applied to the libgcj version.
OK to commit?

2000-12-09  Bryce McKinlay  <bryce@albatross.co.nz>

    * java/io/DataInputStream.java (readBoolean): Use convertToBoolean().
    (readByte): Use convertToByte().
    (readChar): Use convertToChar().
    (readInt): Use convertToInt().
    (readLong): Use convertToLong().
    (readShort): Use convertToShort().
    (readUnsignedByte): Use convertToUnsignedByte().
    (readUnsignedShort): Use convertToUnsignedShort().
    (readUTF): Use convertToUTF().

    (convertToBoolean): Resurrected.
    (convertToByte): Ditto.
    (convertToChar): Ditto.
    (convertToInt): Ditto.
    (convertToLong): Ditto.
    (convertToShort): Ditto.
    (convertToUnsignedByte): Ditto.
    (convertToUnsignedShort): Ditto.
    (convertToUTF): Ditto.

Cheers,

Mark
===================================================================
RCS file: /home/cvs/classpath/java/io/DataInputStream.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- classpath/java/io/DataInputStream.java	2000/11/28 04:54:06	1.10
+++ classpath/java/io/DataInputStream.java	2000/12/09 06:11:33	1.11
@@ -33,6 +33,9 @@
   // handled correctly. If set, readLine() will ignore the first char it sees
   // if that char is a '\n'
   boolean ignoreInitialNewline = false;
+
+  // Byte buffer, used to make primitive read calls more efficient.
+  byte[] buf = new byte[8];
   
   /**
    * This constructor initializes a new <code>DataInputStream</code>
@@ -102,10 +105,7 @@
    */
   public final boolean readBoolean() throws IOException
   {
-    int b = in.read();
-    if (b < 0)
-      throw new EOFException();    
-    return (b != 0);
+    return convertToBoolean(in.read());
   }
 
   /**
@@ -125,11 +125,7 @@
    */
   public final byte readByte() throws IOException
   {
-    int i = in.read();
-    if (i < 0)
-      throw new EOFException();
-
-    return (byte) i;
+    return convertToByte(in.read());
   }
 
   /**
@@ -159,11 +155,10 @@
    */
   public final char readChar() throws IOException
   {
-    int a = in.read();
-    int b = in.read();
-    if (b < 0)
+    int count = in.read (buf, 0, 2);
+    if (count < 2)
       throw new EOFException();
-    return (char) ((a << 8) | (b & 0xff));
+    return convertToChar(buf);
   }
 
   /**
@@ -290,15 +285,10 @@
    */
   public final int readInt() throws IOException
   {
-    int a = in.read();
-    int b = in.read();
-    int c = in.read();
-    int d = in.read();
-    if (d < 0)
+    int count = in.read (buf, 0, 4);
+    if (count < 4)
       throw new EOFException();
-    
-    return (((a & 0xff) << 24) | ((b & 0xff) << 16) |
-	    ((c & 0xff) << 8) | (d & 0xff));
+    return convertToInt(buf);
   }
 
   /**
@@ -445,25 +435,10 @@
    */
   public final long readLong() throws IOException
   {
-    int a = in.read();
-    int b = in.read();
-    int c = in.read();
-    int d = in.read();
-    int e = in.read();
-    int f = in.read();
-    int g = in.read();
-    int h = in.read();
-    if (h < 0)
+    int count = in.read(buf, 0, 8);
+    if (count < 8)
       throw new EOFException();
-    
-    return (((long)(a & 0xff) << 56) |
-	    ((long)(b & 0xff) << 48) |
-	    ((long)(c & 0xff) << 40) |
-	    ((long)(d & 0xff) << 32) |
-	    ((long)(e & 0xff) << 24) |
-	    ((long)(f & 0xff) << 16) |
-	    ((long)(g & 0xff) <<  8) |
-	    ((long)(h & 0xff)));
+    return convertToLong(buf);
   }
 
   /**
@@ -495,11 +470,10 @@
    */
   public final short readShort() throws IOException
   {
-    int a = in.read();
-    int b = in.read();
-    if (b < 0)
+    int count = in.read(buf, 0, 2);
+    if (count < 2)
       throw new EOFException();
-    return (short) ((a << 8) | (b & 0xff));
+    return convertToShort(buf);
   }
 
   /**
@@ -520,11 +494,7 @@
    */
   public final int readUnsignedByte() throws IOException
   {
-    int i = in.read();
-    if (i < 0)
-      throw new EOFException();
-
-    return (i & 0xFF);
+    return convertToUnsignedByte(in.read());
   }
 
   /**
@@ -554,11 +524,10 @@
    */
   public final int readUnsignedShort() throws IOException
   {
-    int a = in.read();
-    int b = in.read();
-    if (b < 0)
+    int count = in.read(buf, 0, 2);
+    if (count < 2)
       throw new EOFException();
-    return (((a & 0xff) << 8) | (b & 0xff));
+    return convertToUnsignedShort(buf);
   }
 
   /**
@@ -650,41 +619,14 @@
   {
     final int UTFlen = in.readUnsignedShort();
     byte[] buf = new byte[UTFlen];
-    StringBuffer strbuf = new StringBuffer();
 
     // This blocks until the entire string is available rather than
     // doing partial processing on the bytes that are available and then
     // blocking.  An advantage of the latter is that Exceptions
     // could be thrown earlier.  The former is a bit cleaner.
     in.readFully(buf, 0, UTFlen);
-    for (int i = 0; i < UTFlen; )
-      {
-	if ((buf[i] & 0x80) == 0)		// bit pattern 0xxxxxxx
-	  strbuf.append((char) (buf[i++] & 0xFF));
-	else if ((buf[i] & 0xE0) == 0xC0)	// bit pattern 110xxxxx
-	  {
-	    if (i + 1 >= UTFlen || (buf[i+1] & 0xC0) != 0x80)
-	      throw new UTFDataFormatException();
 
-	    strbuf.append((char) (((buf[i++] & 0x1F) << 6) |
-				  (buf[i++] & 0x3F)));
-	  }
-	else if ((buf[i] & 0xF0) == 0xE0)	// bit pattern 1110xxxx
-	  {
-	    if (i + 2 >= UTFlen ||
-		(buf[i+1] & 0xC0) != 0x80 || (buf[i+2] & 0xC0) != 0x80)
-	      throw new UTFDataFormatException();
-
-	    strbuf.append((char) (((buf[i++] & 0x0F) << 12) |
-				  ((buf[i++] & 0x3F) << 6) |
-				  (buf[i++] & 0x3F)));
-	  }
-	else // must be ((buf[i] & 0xF0) == 0xF0 || (buf[i] & 0xC0) == 0x80)
-	  throw new UTFDataFormatException();	// bit patterns 1111xxxx or
-						// 		10xxxxxx
-      }
-
-    return strbuf.toString();
+    return convertFromUTF(buf);
   }
 
   /**
@@ -714,5 +656,94 @@
         // do nothing.
       }         
     return n;
+  }
+  
+  static boolean convertToBoolean(int b) throws EOFException
+  {
+    if (b < 0)
+      throw new EOFException();    
+    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)));  
+  }
+
+  static String convertFromUTF(byte[] buf) 
+    throws EOFException, UTFDataFormatException
+  {
+    StringBuffer strbuf = new StringBuffer();
+
+    for (int i = 0; i < buf.length; )
+      {
+	if ((buf[i] & 0x80) == 0)		// bit pattern 0xxxxxxx
+	  strbuf.append((char) (buf[i++] & 0xFF));
+	else if ((buf[i] & 0xE0) == 0xC0)	// bit pattern 110xxxxx
+	  {
+	    if (i + 1 >= buf.length || (buf[i+1] & 0xC0) != 0x80)
+	      throw new UTFDataFormatException();
+
+	    strbuf.append((char) (((buf[i++] & 0x1F) << 6) |
+				  (buf[i++] & 0x3F)));
+	  }
+	else if ((buf[i] & 0xF0) == 0xE0)	// bit pattern 1110xxxx
+	  {
+	    if (i + 2 >= buf.length ||
+		(buf[i+1] & 0xC0) != 0x80 || (buf[i+2] & 0xC0) != 0x80)
+	      throw new UTFDataFormatException();
+
+	    strbuf.append((char) (((buf[i++] & 0x0F) << 12) |
+				  ((buf[i++] & 0x3F) << 6) |
+				  (buf[i++] & 0x3F)));
+	  }
+	else // must be ((buf[i] & 0xF0) == 0xF0 || (buf[i] & 0xC0) == 0x80)
+	  throw new UTFDataFormatException();	// bit patterns 1111xxxx or
+						// 		10xxxxxx
+      }
+
+    return strbuf.toString();
   }
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]