harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndbe...@apache.org
Subject svn commit: r431832 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io: ByteArrayInputStream.java ByteArrayOutputStream.java
Date Wed, 16 Aug 2006 05:44:22 GMT
Author: ndbeyer
Date: Tue Aug 15 22:44:21 2006
New Revision: 431832

URL: http://svn.apache.org/viewvc?rev=431832&view=rev
Log:
Cleanup source, remove compiler warnings and set eol-style property.

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayInputStream.java
  (contents, props changed)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayOutputStream.java
  (contents, props changed)

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayInputStream.java?rev=431832&r1=431831&r2=431832&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayInputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayInputStream.java
Tue Aug 15 22:44:21 2006
@@ -1,203 +1,213 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-
-/**
- * ByteArrayInputStream is used for streaming over a byte array.
- * 
- * @see ByteArrayOutputStream
- */
-public class ByteArrayInputStream extends InputStream {
-	/**
-	 * The <code>byte</code> array containing the bytes to stream over.
-	 */
-	protected byte[] buf;
-
-	/**
-	 * The current position within the byte array.
-	 */
-	protected int pos;
-
-	/**
-	 * The current mark position. Initially set to 0 or the <code>offset</code>
-	 * parameter within the constructor.
-	 */
-	protected int mark;
-
-	/**
-	 * The total number of bytes initially available in the byte array
-	 * <code>buf</code>.
-	 */
-	protected int count;
-
-	/**
-	 * Constructs a new ByteArrayInputStream on the byte array <code>buf</code>.
-	 * 
-	 * @param buf
-	 *            the byte array to stream over
-	 */
-	public ByteArrayInputStream(byte buf[]) {
-		this.mark = 0;
-		this.buf = buf;
-		this.count = buf.length;
-	}
-
-	/**
-	 * Constructs a new ByteArrayInputStream on the byte array <code>buf</code>
-	 * with the position set to <code>offset</code> and the number of bytes
-	 * available set to <code>offset</code> + <code>length</code>.
-	 * 
-	 * @param buf
-	 *            the byte array to stream over
-	 * @param offset
-	 *            the offset in <code>buf</code> to start streaming at
-	 * @param length
-	 *            the number of bytes available to stream over.
-	 */
-	public ByteArrayInputStream(byte buf[], int offset, int length) {
-		this.buf = buf;
-		pos = offset >= buf.length ? buf.length : offset;
-		mark = pos;
-		count = length + pos > buf.length ? buf.length : length + pos;
-	}
-
-	/**
-	 * Answers a int representing then number of bytes that are available before
-	 * this ByteArrayInputStream will block. This method returns the number of
-	 * bytes yet to be read from the underlying byte array.
-	 * 
-	 * @return the number of bytes available before blocking.
-	 */
-	public synchronized int available() {
-		return count - pos;
-	}
-
-	/**
-	 * Close the ByteArrayInputStream. This implementation frees up resources
-	 * associated with this stream.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to close this InputStream.
-	 */
-	public void close() throws IOException {
-		// Do nothing on close, this matches JDK behaviour.
-	}
-
-	/**
-	 * Set a Mark position in this ByteArrayInputStream. The parameter
-	 * <code>readLimit</code> is ignored. Sending reset() will reposition the
-	 * stream back to the marked position.
-	 * 
-	 * @param readlimit
-	 *            ignored.
-	 */
-	public void mark(int readlimit) {
-		mark = pos;
-	}
-
-	/**
-	 * Answers a boolean indicating whether or not this ByteArrayInputStream
-	 * supports mark() and reset(). This implementation answers
-	 * <code>true</code>.
-	 * 
-	 * @return <code>true</code> indicates this stream supports mark/reset,
-	 *         <code>false
-	 *				<code> otherwise.
-	 */
-	public boolean markSupported() {
-		return true;
-	}
-
-	/**
-	 * Reads a single byte from this ByteArrayInputStream and returns the result
-	 * as an int. The low-order byte is returned or -1 of the end of stream was
-	 * encountered. This implementation returns the next available byte from the
-	 * target byte array.
-	 * 
-	 * @return the byte read or -1 if end of stream.
-	 */
-	public synchronized int read() {
-		return pos < count ? buf[pos++] & 0xFF : -1;
-	}
-
-	/**
-	 * Reads at most <code>len</code> bytes from this ByteArrayInputStream and
-	 * stores them in byte array <code>b</code> starting at offset
-	 * <code>off</code>. Answer the number of bytes actually read or -1 if no
-	 * bytes were read and end of stream was encountered. This implementation
-	 * reads bytes from the target byte array.
-	 * 
-	 * @param b
-	 *            the byte array in which to store the read bytes.
-	 * @param offset
-	 *            the offset in <code>b</code> to store the read bytes.
-	 * @param length
-	 *            the maximum number of bytes to store in <code>b</code>.
-	 * @return the number of bytes actually read or -1 if end of stream.
-	 */
-	public synchronized int read(byte b[], int offset, int length) {
-		// Are there any bytes available
-		if (this.pos >= this.count)
-			return -1;
-
-		if (b != null) {
-			// avoid int overflow
-			if (0 <= offset && offset <= b.length && 0 <= length
-					&& length <= b.length - offset) {
-				if (length == 0)
-					return 0;
-
-				int copylen = this.count - pos < length ? this.count - pos
-						: length;
-				System.arraycopy(buf, pos, b, offset, copylen);
-				pos += copylen;
-				return copylen;
-			}
-			throw new ArrayIndexOutOfBoundsException();
-		}
-		throw new NullPointerException();
-	}
-
-	/**
-	 * Reset this ByteArrayInputStream to the last marked location. This
-	 * implementation resets the position to either the marked position, the
-	 * start position supplied in the constructor or <code>0</code> if neither
-	 * is provided.
-	 * 
-	 */
-	public synchronized void reset() {
-		pos = mark;
-	}
-
-	/**
-	 * Skips <code>count</code> number of bytes in this InputStream.
-	 * Subsequent <code>read()</code>'s will not return these bytes unless
-	 * <code>reset()</code> is used. This implementation skips
-	 * <code>count</code> number of bytes in the target stream.
-	 * 
-	 * @param n
-	 *            the number of bytes to skip.
-	 * @return the number of bytes actually skipped.
-	 */
-	public synchronized long skip(long n) {
-		if (n <= 0)
-			return 0;
-		int temp = pos;
-		pos = this.count - pos < n ? this.count : (int) (pos + n);
-		return pos - temp;
-	}
-}
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+/**
+ * ByteArrayInputStream is used for streaming over a byte array.
+ * 
+ * @see ByteArrayOutputStream
+ */
+public class ByteArrayInputStream extends InputStream {
+	/**
+	 * The <code>byte</code> array containing the bytes to stream over.
+	 */
+	protected byte[] buf;
+
+	/**
+	 * The current position within the byte array.
+	 */
+	protected int pos;
+
+	/**
+	 * The current mark position. Initially set to 0 or the <code>offset</code>
+	 * parameter within the constructor.
+	 */
+	protected int mark;
+
+	/**
+	 * The total number of bytes initially available in the byte array
+	 * <code>buf</code>.
+	 */
+	protected int count;
+
+	/**
+	 * Constructs a new ByteArrayInputStream on the byte array <code>buf</code>.
+	 * 
+	 * @param buf
+	 *            the byte array to stream over
+	 */
+	public ByteArrayInputStream(byte buf[]) {
+		this.mark = 0;
+		this.buf = buf;
+		this.count = buf.length;
+	}
+
+	/**
+	 * Constructs a new ByteArrayInputStream on the byte array <code>buf</code>
+	 * with the position set to <code>offset</code> and the number of bytes
+	 * available set to <code>offset</code> + <code>length</code>.
+	 * 
+	 * @param buf
+	 *            the byte array to stream over
+	 * @param offset
+	 *            the offset in <code>buf</code> to start streaming at
+	 * @param length
+	 *            the number of bytes available to stream over.
+	 */
+	public ByteArrayInputStream(byte buf[], int offset, int length) {
+		this.buf = buf;
+		pos = offset >= buf.length ? buf.length : offset;
+		mark = pos;
+		count = length + pos > buf.length ? buf.length : length + pos;
+	}
+
+	/**
+	 * Answers a int representing then number of bytes that are available before
+	 * this ByteArrayInputStream will block. This method returns the number of
+	 * bytes yet to be read from the underlying byte array.
+	 * 
+	 * @return the number of bytes available before blocking.
+	 */
+	@Override
+    public synchronized int available() {
+		return count - pos;
+	}
+
+	/**
+	 * Close the ByteArrayInputStream. This implementation frees up resources
+	 * associated with this stream.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to close this InputStream.
+	 */
+	@Override
+    public void close() throws IOException {
+		// Do nothing on close, this matches JDK behaviour.
+	}
+
+	/**
+	 * Set a Mark position in this ByteArrayInputStream. The parameter
+	 * <code>readLimit</code> is ignored. Sending reset() will reposition the
+	 * stream back to the marked position.
+	 * 
+	 * @param readlimit
+	 *            ignored.
+	 */
+	@Override
+    public void mark(int readlimit) {
+		mark = pos;
+	}
+
+	/**
+	 * Answers a boolean indicating whether or not this ByteArrayInputStream
+	 * supports mark() and reset(). This implementation answers
+	 * <code>true</code>.
+	 * 
+	 * @return <code>true</code> indicates this stream supports mark/reset,
+	 *         <code>false
+	 *				<code> otherwise.
+	 */
+	@Override
+    public boolean markSupported() {
+		return true;
+	}
+
+	/**
+	 * Reads a single byte from this ByteArrayInputStream and returns the result
+	 * as an int. The low-order byte is returned or -1 of the end of stream was
+	 * encountered. This implementation returns the next available byte from the
+	 * target byte array.
+	 * 
+	 * @return the byte read or -1 if end of stream.
+	 */
+	@Override
+    public synchronized int read() {
+		return pos < count ? buf[pos++] & 0xFF : -1;
+	}
+
+	/**
+	 * Reads at most <code>len</code> bytes from this ByteArrayInputStream and
+	 * stores them in byte array <code>b</code> starting at offset
+	 * <code>off</code>. Answer the number of bytes actually read or -1 if no
+	 * bytes were read and end of stream was encountered. This implementation
+	 * reads bytes from the target byte array.
+	 * 
+	 * @param b
+	 *            the byte array in which to store the read bytes.
+	 * @param offset
+	 *            the offset in <code>b</code> to store the read bytes.
+	 * @param length
+	 *            the maximum number of bytes to store in <code>b</code>.
+	 * @return the number of bytes actually read or -1 if end of stream.
+	 */
+	@Override
+    public synchronized int read(byte b[], int offset, int length) {
+		// Are there any bytes available
+		if (this.pos >= this.count) {
+            return -1;
+        }
+
+		if (b != null) {
+			// avoid int overflow
+			if (0 <= offset && offset <= b.length && 0 <= length
+					&& length <= b.length - offset) {
+				if (length == 0) {
+                    return 0;
+                }
+
+				int copylen = this.count - pos < length ? this.count - pos
+						: length;
+				System.arraycopy(buf, pos, b, offset, copylen);
+				pos += copylen;
+				return copylen;
+			}
+			throw new ArrayIndexOutOfBoundsException();
+		}
+		throw new NullPointerException();
+	}
+
+	/**
+	 * Reset this ByteArrayInputStream to the last marked location. This
+	 * implementation resets the position to either the marked position, the
+	 * start position supplied in the constructor or <code>0</code> if neither
+	 * is provided.
+	 * 
+	 */
+	@Override
+    public synchronized void reset() {
+		pos = mark;
+	}
+
+	/**
+	 * Skips <code>count</code> number of bytes in this InputStream.
+	 * Subsequent <code>read()</code>'s will not return these bytes unless
+	 * <code>reset()</code> is used. This implementation skips
+	 * <code>count</code> number of bytes in the target stream.
+	 * 
+	 * @param n
+	 *            the number of bytes to skip.
+	 * @return the number of bytes actually skipped.
+	 */
+	@Override
+    public synchronized long skip(long n) {
+		if (n <= 0) {
+            return 0;
+        }
+		int temp = pos;
+		pos = this.count - pos < n ? this.count : (int) (pos + n);
+		return pos - temp;
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayOutputStream.java?rev=431832&r1=431831&r2=431832&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayOutputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayOutputStream.java
Tue Aug 15 22:44:21 2006
@@ -1,245 +1,253 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-
-/**
- * ByteArrayOutputStream is a class whose underlying stream is represented by a
- * byte array. As bytes are written to this stream, the local byte array may be
- * expanded to hold more bytes.
- * 
- * @see ByteArrayInputStream
- */
-public class ByteArrayOutputStream extends OutputStream {
-	/**
-	 * The byte array containing the bytes written.
-	 */
-	protected byte[] buf;
-
-	/**
-	 * The number of bytes written.
-	 */
-	protected int count;
-
-	/**
-	 * Constructs a new ByteArrayOutputStream with a default size of 32 bytes.
-	 * If more than 32 bytes are written to this instance, the underlying byte
-	 * array will expand to accomodate.
-	 * 
-	 */
-	public ByteArrayOutputStream() {
-		super();
-		buf = new byte[32];
-	}
-
-	/**
-	 * Constructs a new ByteArrayOutputStream with a default size of
-	 * <code>size</code> bytes. If more than <code>size</code> bytes
are
-	 * written to this instance, the underlying byte array will expand to
-	 * accomodate.
-	 * 
-	 * @param size
-	 *            an non-negative integer representing the initial size for the
-	 *            underlying byte array.
-	 */
-	public ByteArrayOutputStream(int size) {
-		super();
-		if (size >= 0)
-			buf = new byte[size];
-		else
-			throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
-					.getString("K005e")); //$NON-NLS-1$
-	}
-
-	/**
-	 * Close this ByteArrayOutputStream. This implementation releases System
-	 * resources used for this stream.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs attempting to close this OutputStream.
-	 */
-	public void close() throws IOException {
-		/**
-		 * Although the spec claims "A closed stream cannot perform output
-		 * operations and cannot be reopened.", this implementation must do
-		 * nothing.
-		 */
-		super.close();
-	}
-
-	private void expand(int i) {
-		/* Can the buffer handle @i more bytes, if not expand it */
-		if (count + i <= buf.length)
-			return;
-
-		byte[] newbuf = new byte[(count + i) * 2];
-		System.arraycopy(buf, 0, newbuf, 0, count);
-		buf = newbuf;
-	}
-
-	/**
-	 * Reset this ByteArrayOutputStream to the beginning of the underlying byte
-	 * array. All subsequent writes will overwrite any bytes previously stored
-	 * in this stream.
-	 * 
-	 */
-	public synchronized void reset() {
-		count = 0;
-	}
-
-	/**
-	 * Answers the total number of bytes written to this stream thus far.
-	 * 
-	 * @return the number of bytes written to this Stream.
-	 */
-	public int size() {
-		return count;
-	}
-
-	/**
-	 * Answer the contents of this ByteArrayOutputStream as a byte array. Any
-	 * changes made to the receiver after returning will not be reflected in the
-	 * byte array returned to the caller.
-	 * 
-	 * @return this streams current contents as a byte array.
-	 */
-	public synchronized byte[] toByteArray() {
-		byte[] newArray = new byte[count];
-		System.arraycopy(buf, 0, newArray, 0, count);
-		return newArray;
-	}
-
-	/**
-	 * Answer the contents of this ByteArrayOutputStream as a String. Any
-	 * changes made to the receiver after returning will not be reflected in the
-	 * String returned to the caller.
-	 * 
-	 * @return this streams current contents as a String.
-	 */
-
-	public String toString() {
-		return new String(buf, 0, count);
-	}
-
-	/**
-	 * Answer the contents of this ByteArrayOutputStream as a String. Each byte
-	 * <code>b</code> in this stream is converted to a character
-	 * <code>c</code> using the following function:
-	 * <code>c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))</code>.
This
-	 * method is deprecated and either toString(), or toString(enc) should be
-	 * used.
-	 * 
-	 * @param hibyte
-	 *            the high byte of each resulting Unicode character
-	 * @return this streams current contents as a String with the high byte set
-	 *         to <code>hibyte</code>
-	 * 
-	 * @deprecated Use toString()
-	 */
-	public String toString(int hibyte) {
-		char[] newBuf = new char[size()];
-		for (int i = 0; i < newBuf.length; i++)
-			newBuf[i] = (char) (((hibyte & 0xff) << 8) | (buf[i] & 0xff));
-		return new String(newBuf);
-	}
-
-	/**
-	 * Answer the contents of this ByteArrayOutputStream as a String converted
-	 * using the encoding declared in <code>enc</code>.
-	 * 
-	 * @param enc
-	 *            A String representing the encoding to use when translating
-	 *            this stream to a String.
-	 * @return this streams current contents as a String.
-	 * 
-	 * @throws UnsupportedEncodingException
-	 *             If declared encoding is not supported
-	 */
-	public String toString(String enc) throws UnsupportedEncodingException {
-		return new String(buf, 0, count, enc);
-	}
-
-	/**
-	 * Writes <code>count</code> <code>bytes</code> from the byte array
-	 * <code>buffer</code> starting at offset <code>index</code> to
the
-	 * ByteArrayOutputStream.
-	 * 
-	 * @param buffer
-	 *            the buffer to be written
-	 * @param offset
-	 *            offset in buffer to get bytes
-	 * @param len
-	 *            number of bytes in buffer to write
-	 * 
-	 * @throws NullPointerException
-	 *             If buffer is null.
-	 * @throws IndexOutOfBoundsException
-	 *             If offset or count are outside of bounds.
-	 */
-	public synchronized void write(byte[] buffer, int offset, int len) {
-		/* Unsure what to do here, spec is unclear */
-		if (buf == null)
-			return;
-		if (buffer != null) {
-			// avoid int overflow
-			if (0 <= offset && offset <= buffer.length && 0 <= len
-					&& len <= buffer.length - offset) {
-				/* Expand if necessary */
-				expand(len);
-				System.arraycopy(buffer, offset, buf, this.count, len);
-				this.count += len;
-			} else
-				throw new IndexOutOfBoundsException(org.apache.harmony.luni.util.Msg
-						.getString("K002f")); //$NON-NLS-1$
-		} else
-			throw new NullPointerException(org.apache.harmony.luni.util.Msg
-					.getString("K0047")); //$NON-NLS-1$
-	}
-
-	/**
-	 * Writes the specified byte <code>oneByte</code> to the OutputStream.
-	 * Only the low order byte of <code>oneByte</code> is written.
-	 * 
-	 * @param oneByte
-	 *            the byte to be written
-	 */
-	public synchronized void write(int oneByte) {
-		try {
-			buf[count] = (byte) oneByte;
-			count++;
-		} catch (IndexOutOfBoundsException e) {
-			// Expand when necessary
-			expand(1);
-			buf[count++] = (byte) oneByte;
-		} catch (NullPointerException e) {
-		}
-	}
-
-	/**
-	 * Take the contents of this stream and write it to the output stream
-	 * <code>out</code>.
-	 * 
-	 * @param out
-	 *            An OutputStream on which to write the contents of this stream.
-	 * 
-	 * @throws IOException
-	 *             If an error occurs when writing to output stream
-	 */
-	public void writeTo(OutputStream out) throws IOException {
-		out.write(buf, 0, count);
-	}
-
-}
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+import org.apache.harmony.luni.util.Msg;
+
+/**
+ * ByteArrayOutputStream is a class whose underlying stream is represented by a
+ * byte array. As bytes are written to this stream, the local byte array may be
+ * expanded to hold more bytes.
+ * 
+ * @see ByteArrayInputStream
+ */
+public class ByteArrayOutputStream extends OutputStream {
+	/**
+	 * The byte array containing the bytes written.
+	 */
+	protected byte[] buf;
+
+	/**
+	 * The number of bytes written.
+	 */
+	protected int count;
+
+	/**
+	 * Constructs a new ByteArrayOutputStream with a default size of 32 bytes.
+	 * If more than 32 bytes are written to this instance, the underlying byte
+	 * array will expand to accommodate.
+	 * 
+	 */
+	public ByteArrayOutputStream() {
+		super();
+		buf = new byte[32];
+	}
+
+	/**
+	 * Constructs a new ByteArrayOutputStream with a default size of
+	 * <code>size</code> bytes. If more than <code>size</code> bytes
are
+	 * written to this instance, the underlying byte array will expand to
+	 * accommodate.
+	 * 
+	 * @param size
+	 *            an non-negative integer representing the initial size for the
+	 *            underlying byte array.
+	 */
+	public ByteArrayOutputStream(int size) {
+		super();
+		if (size >= 0) {
+            buf = new byte[size];
+        } else {
+            throw new IllegalArgumentException(Msg.getString("K005e")); //$NON-NLS-1$
+        }
+	}
+
+	/**
+	 * Close this ByteArrayOutputStream. This implementation releases System
+	 * resources used for this stream.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs attempting to close this OutputStream.
+	 */
+	@Override
+    public void close() throws IOException {
+		/**
+		 * Although the spec claims "A closed stream cannot perform output
+		 * operations and cannot be reopened.", this implementation must do
+		 * nothing.
+		 */
+		super.close();
+	}
+
+	private void expand(int i) {
+		/* Can the buffer handle @i more bytes, if not expand it */
+		if (count + i <= buf.length) {
+            return;
+        }
+
+		byte[] newbuf = new byte[(count + i) * 2];
+		System.arraycopy(buf, 0, newbuf, 0, count);
+		buf = newbuf;
+	}
+
+	/**
+	 * Reset this ByteArrayOutputStream to the beginning of the underlying byte
+	 * array. All subsequent writes will overwrite any bytes previously stored
+	 * in this stream.
+	 * 
+	 */
+	public synchronized void reset() {
+		count = 0;
+	}
+
+	/**
+	 * Answers the total number of bytes written to this stream thus far.
+	 * 
+	 * @return the number of bytes written to this Stream.
+	 */
+	public int size() {
+		return count;
+	}
+
+	/**
+	 * Answer the contents of this ByteArrayOutputStream as a byte array. Any
+	 * changes made to the receiver after returning will not be reflected in the
+	 * byte array returned to the caller.
+	 * 
+	 * @return this streams current contents as a byte array.
+	 */
+	public synchronized byte[] toByteArray() {
+		byte[] newArray = new byte[count];
+		System.arraycopy(buf, 0, newArray, 0, count);
+		return newArray;
+	}
+
+	/**
+	 * Answer the contents of this ByteArrayOutputStream as a String. Any
+	 * changes made to the receiver after returning will not be reflected in the
+	 * String returned to the caller.
+	 * 
+	 * @return this streams current contents as a String.
+	 */
+
+	@Override
+    public String toString() {
+		return new String(buf, 0, count);
+	}
+
+	/**
+	 * Answer the contents of this ByteArrayOutputStream as a String. Each byte
+	 * <code>b</code> in this stream is converted to a character
+	 * <code>c</code> using the following function:
+	 * <code>c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))</code>.
This
+	 * method is deprecated and either toString(), or toString(enc) should be
+	 * used.
+	 * 
+	 * @param hibyte
+	 *            the high byte of each resulting Unicode character
+	 * @return this streams current contents as a String with the high byte set
+	 *         to <code>hibyte</code>
+	 * 
+	 * @deprecated Use toString()
+	 */
+	@Deprecated
+    public String toString(int hibyte) {
+		char[] newBuf = new char[size()];
+		for (int i = 0; i < newBuf.length; i++) {
+            newBuf[i] = (char) (((hibyte & 0xff) << 8) | (buf[i] & 0xff));
+        }
+		return new String(newBuf);
+	}
+
+	/**
+	 * Answer the contents of this ByteArrayOutputStream as a String converted
+	 * using the encoding declared in <code>enc</code>.
+	 * 
+	 * @param enc
+	 *            A String representing the encoding to use when translating
+	 *            this stream to a String.
+	 * @return this streams current contents as a String.
+	 * 
+	 * @throws UnsupportedEncodingException
+	 *             If declared encoding is not supported
+	 */
+	public String toString(String enc) throws UnsupportedEncodingException {
+		return new String(buf, 0, count, enc);
+	}
+
+	/**
+	 * Writes <code>count</code> <code>bytes</code> from the byte array
+	 * <code>buffer</code> starting at offset <code>index</code> to
the
+	 * ByteArrayOutputStream.
+	 * 
+	 * @param buffer
+	 *            the buffer to be written
+	 * @param offset
+	 *            offset in buffer to get bytes
+	 * @param len
+	 *            number of bytes in buffer to write
+	 * 
+	 * @throws NullPointerException
+	 *             If buffer is null.
+	 * @throws IndexOutOfBoundsException
+	 *             If offset or count are outside of bounds.
+	 */
+	@Override
+    public synchronized void write(byte[] buffer, int offset, int len) {
+		/* Unsure what to do here, spec is unclear */
+		if (buf == null) {
+            return;
+        }
+		if (buffer != null) {
+			// avoid int overflow
+			if (0 <= offset && offset <= buffer.length && 0 <= len
+					&& len <= buffer.length - offset) {
+				/* Expand if necessary */
+				expand(len);
+				System.arraycopy(buffer, offset, buf, this.count, len);
+				this.count += len;
+			} else {
+                throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
+            }
+		} else {
+            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
+        }
+	}
+
+	/**
+	 * Writes the specified byte <code>oneByte</code> to the OutputStream.
+	 * Only the low order byte of <code>oneByte</code> is written.
+	 * 
+	 * @param oneByte
+	 *            the byte to be written
+	 */
+	@Override
+    public synchronized void write(int oneByte) {
+		try {
+			buf[count] = (byte) oneByte;
+			count++;
+		} catch (IndexOutOfBoundsException e) {
+			// Expand when necessary
+			expand(1);
+			buf[count++] = (byte) oneByte;
+		} catch (NullPointerException e) {
+		}
+	}
+
+	/**
+	 * Take the contents of this stream and write it to the output stream
+	 * <code>out</code>.
+	 * 
+	 * @param out
+	 *            An OutputStream on which to write the contents of this stream.
+	 * 
+	 * @throws IOException
+	 *             If an error occurs when writing to output stream
+	 */
+	public void writeTo(OutputStream out) throws IOException {
+		out.write(buf, 0, count);
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ByteArrayOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message