You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/12/12 12:40:33 UTC

svn commit: r486103 - in /harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io: BufferedInputStream.java BufferedOutputStream.java

Author: tellison
Date: Tue Dec 12 03:40:33 2006
New Revision: 486103

URL: http://svn.apache.org/viewvc?view=rev&rev=486103
Log:
Code formatting and tidy-up.
Increase buffer size to 8k.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java?view=diff&rev=486103&r1=486102&r2=486103
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedInputStream.java Tue Dec 12 03:40:33 2006
@@ -54,14 +54,12 @@
      * The current position within the byte array <code>buf</code>.
      */
     protected int pos;
-    
+
     private boolean closed = false;
-    
-    
 
     /**
      * Constructs a new <code>BufferedInputStream</code> on the InputStream
-     * <code>in</code>. The default buffer size (2K) is allocated and all
+     * <code>in</code>. The default buffer size (8Kb) is allocated and all
      * reads can now be filtered through this stream.
      * 
      * @param in
@@ -69,7 +67,7 @@
      */
     public BufferedInputStream(InputStream in) {
         super(in);
-        buf = (in == null) ? null : new byte[2048];
+        buf = (in == null) ? null : new byte[8192];
     }
 
     /**
@@ -84,11 +82,11 @@
      */
     public BufferedInputStream(InputStream in, int size) {
         super(in);
-        if (size > 0) {
-            buf = (in == null) ? null : new byte[size];
-        } else {
+        if (size <= 0) {
+            // K0058=size must be > 0
             throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
         }
+        buf = (in == null) ? null : new byte[size];
     }
 
     /**
@@ -104,6 +102,7 @@
     @Override
     public synchronized int available() throws IOException {
         if (buf == null) {
+            // K0059=Stream is closed
             throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
         }
         return count - pos + in.available();
@@ -118,10 +117,10 @@
      */
     @Override
     public synchronized void close() throws IOException {
-    	if(null != in ){
+        if (null != in) {
             super.close();
             in = null;
-    	}
+        }
         buf = null;
         closed = true;
     }
@@ -201,19 +200,21 @@
      */
     @Override
     public synchronized int read() throws IOException {
-        if (buf != null) {
-            /* Are there buffered bytes available? */
-            if (pos >= count && fillbuf() == -1) {
-                return -1; /* no, fill buffer */
-            }
+        if (buf == null) {
+            // K0059=Stream is closed
+            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+        }
 
-            /* Did filling the buffer fail with -1 (EOF)? */
-            if (count - pos > 0) {
-                return buf[pos++] & 0xFF;
-            }
-            return -1;
+        /* Are there buffered bytes available? */
+        if (pos >= count && fillbuf() == -1) {
+            return -1; /* no, fill buffer */
+        }
+
+        /* Did filling the buffer fail with -1 (EOF)? */
+        if (count - pos > 0) {
+            return buf[pos++] & 0xFF;
         }
-        throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+        return -1;
     }
 
     /**
@@ -240,65 +241,66 @@
      */
     @Override
     public synchronized int read(byte[] buffer, int offset, int length)
-			throws IOException {
-		if (closed) {
-			throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
-		}
-		// avoid int overflow
-		if (offset > buffer.length - length || offset < 0 || length < 0) {
-			throw new IndexOutOfBoundsException();
-		}
-		if (length == 0) {
-			return 0;
-		}
-		if (null == buf) {
-			throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
-		}
-
-		int required;
-		if (pos < count) {
-			/* There are bytes available in the buffer. */
-			int copylength = count - pos >= length ? length : count - pos;
-			System.arraycopy(buf, pos, buffer, offset, copylength);
-			pos += copylength;
-			if (copylength == length || in.available() == 0) {
-				return copylength;
-			}
-			offset += copylength;
-			required = length - copylength;
-		} else {
-			required = length;
-		}
-
-		while (true) {
-			int read;
-			/*
-			 * If we're not marked and the required size is greater than the
-			 * buffer, simply read the bytes directly bypassing the buffer.
-			 */
-			if (markpos == -1 && required >= buf.length) {
-				read = in.read(buffer, offset, required);
-				if (read == -1) {
-					return required == length ? -1 : length - required;
-				}
-			} else {
-				if (fillbuf() == -1) {
-					return required == length ? -1 : length - required;
-				}
-				read = count - pos >= required ? required : count - pos;
-				System.arraycopy(buf, pos, buffer, offset, read);
-				pos += read;
-			}
-			required -= read;
-			if (required == 0) {
-				return length;
-			}
-			if (in.available() == 0) {
-				return length - required;
-			}
-			offset += read;
-		}
-	}
+            throws IOException {
+        if (closed) {
+            // K0059=Stream is closed
+            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+        }
+        // avoid int overflow
+        if (offset > buffer.length - length || offset < 0 || length < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+        if (length == 0) {
+            return 0;
+        }
+        if (null == buf) {
+            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
+        }
+
+        int required;
+        if (pos < count) {
+            /* There are bytes available in the buffer. */
+            int copylength = count - pos >= length ? length : count - pos;
+            System.arraycopy(buf, pos, buffer, offset, copylength);
+            pos += copylength;
+            if (copylength == length || in.available() == 0) {
+                return copylength;
+            }
+            offset += copylength;
+            required = length - copylength;
+        } else {
+            required = length;
+        }
+
+        while (true) {
+            int read;
+            /*
+             * If we're not marked and the required size is greater than the
+             * buffer, simply read the bytes directly bypassing the buffer.
+             */
+            if (markpos == -1 && required >= buf.length) {
+                read = in.read(buffer, offset, required);
+                if (read == -1) {
+                    return required == length ? -1 : length - required;
+                }
+            } else {
+                if (fillbuf() == -1) {
+                    return required == length ? -1 : length - required;
+                }
+                read = count - pos >= required ? required : count - pos;
+                System.arraycopy(buf, pos, buffer, offset, read);
+                pos += read;
+            }
+            required -= read;
+            if (required == 0) {
+                return length;
+            }
+            if (in.available() == 0) {
+                return length - required;
+            }
+            offset += read;
+        }
+    }
 
     /**
      * Reset this BufferedInputStream to the last marked location. If the
@@ -313,13 +315,15 @@
 
     @Override
     public synchronized void reset() throws IOException {
-    	if (closed) {
-    		throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$	
-    	}
+        if (closed) {
+            // K0059=Stream is closed
+            throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$	
+        }
         if (-1 == markpos) {
-        	throw new IOException(Msg.getString("K005a")); //$NON-NLS-1$
-        }    
-        pos = markpos;         
+            // K005a=Mark has been invalidated.
+            throw new IOException(Msg.getString("K005a")); //$NON-NLS-1$
+        }
+        pos = markpos;
     }
 
     /**
@@ -338,6 +342,7 @@
     @Override
     public synchronized long skip(long amount) throws IOException {
         if (null == in) {
+            // K0059=Stream is closed
             throw new IOException(Msg.getString("K0059")); //$NON-NLS-1$
         }
         if (amount < 1) {

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java?view=diff&rev=486103&r1=486102&r2=486103
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedOutputStream.java Tue Dec 12 03:40:33 2006
@@ -29,153 +29,151 @@
  * @see BufferedInputStream
  */
 public class BufferedOutputStream extends FilterOutputStream {
-	/**
-	 * The buffer containing the bytes to be written to the target OutputStream.
-	 */
-	protected byte[] buf;
-
-	/**
-	 * The total number of bytes inside the byte array <code>buf</code>.
-	 */
-	protected int count;
-
-	/**
-	 * Constructs a new BufferedOutputStream on the OutputStream
-	 * <code>out</code>. The default buffer size (512 bytes) is allocated and
-	 * all writes are now filtered through this stream.
-	 * 
-	 * @param out
-	 *            the OutputStream to buffer writes on.
-	 * 
-	 */
-	public BufferedOutputStream(OutputStream out) {
-		super(out);
-		buf = new byte[512];
-	}
-
-	/**
-	 * Constructs a new BufferedOutputStream on the OutputStream
-	 * <code>out</code>. The buffer size is set to <code>size</code> and
-	 * all writes are now filtered through this stream.
-	 * 
+    /**
+     * The buffer containing the bytes to be written to the target OutputStream.
+     */
+    protected byte[] buf;
+
+    /**
+     * The total number of bytes inside the byte array <code>buf</code>.
+     */
+    protected int count;
+
+    /**
+     * Constructs a new BufferedOutputStream on the OutputStream
+     * <code>out</code>. The default buffer size (8Kb) is allocated and all
+     * writes are now filtered through this stream.
+     * 
      * @param out
-	 *            the OutputStream to buffer writes on.
-	 * @param size
-	 *            the size of the buffer in bytes.
-	 * @throws IllegalArgumentException
-	 *             the size is <= 0
-	 * 
-	 */
-	public BufferedOutputStream(OutputStream out, int size) {
-		super(out);
-		if (size > 0) {
-            buf = new byte[size];
-        } else {
+     *            the OutputStream to buffer writes on.
+     */
+    public BufferedOutputStream(OutputStream out) {
+        super(out);
+        buf = new byte[8192];
+    }
+
+    /**
+     * Constructs a new BufferedOutputStream on the OutputStream
+     * <code>out</code>. The buffer size is set to <code>size</code> and
+     * all writes are now filtered through this stream.
+     * 
+     * @param out
+     *            the OutputStream to buffer writes on.
+     * @param size
+     *            the size of the buffer in bytes.
+     * @throws IllegalArgumentException
+     *             the size is <= 0
+     */
+    public BufferedOutputStream(OutputStream out, int size) {
+        super(out);
+        if (size <= 0) {
+            // K0058=size must be > 0
             throw new IllegalArgumentException(Msg.getString("K0058")); //$NON-NLS-1$
         }
-	}
+        buf = new byte[size];
+    }
 
-	/**
-	 * Flush this BufferedOutputStream to ensure all pending data is written out
-	 * to the target OutputStream. In addition, the target stream is also
-	 * flushed.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to flush this
-	 *             BufferedOutputStream.
-	 */
-	@Override
+    /**
+     * Flush this BufferedOutputStream to ensure all pending data is written out
+     * to the target OutputStream. In addition, the target stream is also
+     * flushed.
+     * 
+     * @throws IOException
+     *             If an error occurs attempting to flush this
+     *             BufferedOutputStream.
+     */
+    @Override
     public synchronized void flush() throws IOException {
-		if (count > 0) {
+        if (count > 0) {
             out.write(buf, 0, count);
         }
-		count = 0;
-		out.flush();
-	}
-
-	/**
-	 * Writes <code>count</code> <code>bytes</code> from the byte array
-	 * <code>buffer</code> starting at <code>offset</code> to this
-	 * BufferedOutputStream. If there is room in the buffer to hold the bytes,
-	 * they are copied in. If not, the buffered bytes plus the bytes in
-	 * <code>buffer</code> are written to the target stream, the target is
-	 * flushed, and the buffer is cleared.
-	 * 
-	 * @param buffer
-	 *            the buffer to be written
-	 * @param offset
-	 *            offset in buffer to get bytes
-	 * @param length
-	 *            number of bytes in buffer to write
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to write to this
-	 *             BufferedOutputStream.
-	 * @throws NullPointerException
-	 *             If buffer is null.
-	 * @throws ArrayIndexOutOfBoundsException
-	 *             If offset or count is outside of bounds.
-	 */
-	@Override
-	public synchronized void write(byte[] buffer, int offset, int length)
-			throws IOException {
-		if (buffer == null) {
-			throw new NullPointerException(org.apache.harmony.luni.util.Msg
-					.getString("K0047")); //$NON-NLS-1$
-		}
-		if (offset < 0 || offset > buffer.length - length || length < 0) {
-			throw new ArrayIndexOutOfBoundsException(
-					org.apache.harmony.luni.util.Msg.getString("K002f")); //$NON-NLS-1$
-		}
-		if (count == 0 && length >= buf.length) {
-			out.write(buffer, offset, length);
-			return;
-		}
-		int available = buf.length - count;
-		if (length < available) {
-			available = length;
-		}
-		if (available > 0) {
-			System.arraycopy(buffer, offset, buf, count, available);
-			count += available;
-		}
-		if (count == buf.length) {
-			out.write(buf, 0, buf.length);
-			count = 0;
-			if (length > available) {
-				offset += available;
-				available = length - available;
-				if (available >= buf.length) {
-					out.write(buffer, offset, available);
-				} else {
-					System.arraycopy(buffer, offset, buf, count, available);
-					count += available;
-				}
-			}
-		}
-	}
-
-	/**
-	 * Writes the specified byte <code>oneByte</code> to this
-	 * BufferedOutputStream. Only the low order byte of <code>oneByte</code>
-	 * is written. If there is room in the buffer, the byte is copied in and the
-	 * count incremented. Otherwise, the buffer plus <code>oneByte</code> are
-	 * written to the target stream, the target is flushed, and the buffer is
-	 * reset.
-	 * 
-	 * @param oneByte
-	 *            the byte to be written
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to write to this
-	 *             BufferedOutputStream.
-	 */
-	@Override
+        count = 0;
+        out.flush();
+    }
+
+    /**
+     * Writes <code>count</code> <code>bytes</code> from the byte array
+     * <code>buffer</code> starting at <code>offset</code> to this
+     * BufferedOutputStream. If there is room in the buffer to hold the bytes,
+     * they are copied in. If not, the buffered bytes plus the bytes in
+     * <code>buffer</code> are written to the target stream, the target is
+     * flushed, and the buffer is cleared.
+     * 
+     * @param buffer
+     *            the buffer to be written
+     * @param offset
+     *            offset in buffer to get bytes
+     * @param length
+     *            number of bytes in buffer to write
+     * 
+     * @throws IOException
+     *             If an error occurs attempting to write to this
+     *             BufferedOutputStream.
+     * @throws NullPointerException
+     *             If buffer is null.
+     * @throws ArrayIndexOutOfBoundsException
+     *             If offset or count is outside of bounds.
+     */
+    @Override
+    public synchronized void write(byte[] buffer, int offset, int length)
+            throws IOException {
+        if (buffer == null) {
+            // K0047=buffer is null
+            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
+        }
+        if (offset < 0 || offset > buffer.length - length || length < 0) {
+            // K002f=Arguments out of bounds
+            throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
+        }
+        if (count == 0 && length >= buf.length) {
+            out.write(buffer, offset, length);
+            return;
+        }
+        int available = buf.length - count;
+        if (length < available) {
+            available = length;
+        }
+        if (available > 0) {
+            System.arraycopy(buffer, offset, buf, count, available);
+            count += available;
+        }
+        if (count == buf.length) {
+            out.write(buf, 0, buf.length);
+            count = 0;
+            if (length > available) {
+                offset += available;
+                available = length - available;
+                if (available >= buf.length) {
+                    out.write(buffer, offset, available);
+                } else {
+                    System.arraycopy(buffer, offset, buf, count, available);
+                    count += available;
+                }
+            }
+        }
+    }
+
+    /**
+     * Writes the specified byte <code>oneByte</code> to this
+     * BufferedOutputStream. Only the low order byte of <code>oneByte</code>
+     * is written. If there is room in the buffer, the byte is copied in and the
+     * count incremented. Otherwise, the buffer plus <code>oneByte</code> are
+     * written to the target stream, the target is flushed, and the buffer is
+     * reset.
+     * 
+     * @param oneByte
+     *            the byte to be written
+     * 
+     * @throws IOException
+     *             If an error occurs attempting to write to this
+     *             BufferedOutputStream.
+     */
+    @Override
     public synchronized void write(int oneByte) throws IOException {
-		if (count == buf.length) {
-			out.write(buf, 0, count);
-			count = 0;
-		}
-		buf[count++] = (byte) oneByte;
-	}
+        if (count == buf.length) {
+            out.write(buf, 0, count);
+            count = 0;
+        }
+        buf[count++] = (byte) oneByte;
+    }
 }