Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 78478 invoked from network); 16 Aug 2006 05:44:46 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 16 Aug 2006 05:44:46 -0000 Received: (qmail 76828 invoked by uid 500); 16 Aug 2006 05:44:46 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 76725 invoked by uid 500); 16 Aug 2006 05:44:46 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 76714 invoked by uid 99); 16 Aug 2006 05:44:46 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Aug 2006 22:44:46 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Aug 2006 22:44:42 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id AE6961A981A; Tue, 15 Aug 2006 22:44:22 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: harmony-commits@incubator.apache.org From: ndbeyer@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060816054422.AE6961A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 byte 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 offset - * parameter within the constructor. - */ - protected int mark; - - /** - * The total number of bytes initially available in the byte array - * buf. - */ - protected int count; - - /** - * Constructs a new ByteArrayInputStream on the byte array buf. - * - * @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 buf - * with the position set to offset and the number of bytes - * available set to offset + length. - * - * @param buf - * the byte array to stream over - * @param offset - * the offset in buf 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 - * readLimit 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 - * true. - * - * @return true indicates this stream supports mark/reset, - * false - * 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 len bytes from this ByteArrayInputStream and - * stores them in byte array b starting at offset - * off. 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 b to store the read bytes. - * @param length - * the maximum number of bytes to store in b. - * @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 0 if neither - * is provided. - * - */ - public synchronized void reset() { - pos = mark; - } - - /** - * Skips count number of bytes in this InputStream. - * Subsequent read()'s will not return these bytes unless - * reset() is used. This implementation skips - * count 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 byte 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 offset + * parameter within the constructor. + */ + protected int mark; + + /** + * The total number of bytes initially available in the byte array + * buf. + */ + protected int count; + + /** + * Constructs a new ByteArrayInputStream on the byte array buf. + * + * @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 buf + * with the position set to offset and the number of bytes + * available set to offset + length. + * + * @param buf + * the byte array to stream over + * @param offset + * the offset in buf 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 + * readLimit 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 + * true. + * + * @return true indicates this stream supports mark/reset, + * false + * 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 len bytes from this ByteArrayInputStream and + * stores them in byte array b starting at offset + * off. 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 b to store the read bytes. + * @param length + * the maximum number of bytes to store in b. + * @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 0 if neither + * is provided. + * + */ + @Override + public synchronized void reset() { + pos = mark; + } + + /** + * Skips count number of bytes in this InputStream. + * Subsequent read()'s will not return these bytes unless + * reset() is used. This implementation skips + * count 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 - * size bytes. If more than size 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 - * b in this stream is converted to a character - * c using the following function: - * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff)). 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 hibyte - * - * @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 enc. - * - * @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 count bytes from the byte array - * buffer starting at offset index 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 oneByte to the OutputStream. - * Only the low order byte of oneByte 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 - * out. - * - * @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 + * size bytes. If more than size 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 + * b in this stream is converted to a character + * c using the following function: + * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff)). 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 hibyte + * + * @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 enc. + * + * @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 count bytes from the byte array + * buffer starting at offset index 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 oneByte to the OutputStream. + * Only the low order byte of oneByte 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 + * out. + * + * @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