1. /*
  2. * @(#)ByteArrayOutputStream.java 1.49 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * This class implements an output stream in which the data is
  10. * written into a byte array. The buffer automatically grows as data
  11. * is written to it.
  12. * The data can be retrieved using <code>toByteArray()</code> and
  13. * <code>toString()</code>.
  14. * <p>
  15. * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  16. * this class can be called after the stream has been closed without
  17. * generating an <tt>IOException</tt>.
  18. *
  19. * @author Arthur van Hoff
  20. * @version 1.49, 05/18/04
  21. * @since JDK1.0
  22. */
  23. public class ByteArrayOutputStream extends OutputStream {
  24. /**
  25. * The buffer where data is stored.
  26. */
  27. protected byte buf[];
  28. /**
  29. * The number of valid bytes in the buffer.
  30. */
  31. protected int count;
  32. /**
  33. * Creates a new byte array output stream. The buffer capacity is
  34. * initially 32 bytes, though its size increases if necessary.
  35. */
  36. public ByteArrayOutputStream() {
  37. this(32);
  38. }
  39. /**
  40. * Creates a new byte array output stream, with a buffer capacity of
  41. * the specified size, in bytes.
  42. *
  43. * @param size the initial size.
  44. * @exception IllegalArgumentException if size is negative.
  45. */
  46. public ByteArrayOutputStream(int size) {
  47. if (size < 0) {
  48. throw new IllegalArgumentException("Negative initial size: "
  49. + size);
  50. }
  51. buf = new byte[size];
  52. }
  53. /**
  54. * Writes the specified byte to this byte array output stream.
  55. *
  56. * @param b the byte to be written.
  57. */
  58. public synchronized void write(int b) {
  59. int newcount = count + 1;
  60. if (newcount > buf.length) {
  61. byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  62. System.arraycopy(buf, 0, newbuf, 0, count);
  63. buf = newbuf;
  64. }
  65. buf[count] = (byte)b;
  66. count = newcount;
  67. }
  68. /**
  69. * Writes <code>len</code> bytes from the specified byte array
  70. * starting at offset <code>off</code> to this byte array output stream.
  71. *
  72. * @param b the data.
  73. * @param off the start offset in the data.
  74. * @param len the number of bytes to write.
  75. */
  76. public synchronized void write(byte b[], int off, int len) {
  77. if ((off < 0) || (off > b.length) || (len < 0) ||
  78. ((off + len) > b.length) || ((off + len) < 0)) {
  79. throw new IndexOutOfBoundsException();
  80. } else if (len == 0) {
  81. return;
  82. }
  83. int newcount = count + len;
  84. if (newcount > buf.length) {
  85. byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
  86. System.arraycopy(buf, 0, newbuf, 0, count);
  87. buf = newbuf;
  88. }
  89. System.arraycopy(b, off, buf, count, len);
  90. count = newcount;
  91. }
  92. /**
  93. * Writes the complete contents of this byte array output stream to
  94. * the specified output stream argument, as if by calling the output
  95. * stream's write method using <code>out.write(buf, 0, count)</code>.
  96. *
  97. * @param out the output stream to which to write the data.
  98. * @exception IOException if an I/O error occurs.
  99. */
  100. public synchronized void writeTo(OutputStream out) throws IOException {
  101. out.write(buf, 0, count);
  102. }
  103. /**
  104. * Resets the <code>count</code> field of this byte array output
  105. * stream to zero, so that all currently accumulated output in the
  106. * output stream is discarded. The output stream can be used again,
  107. * reusing the already allocated buffer space.
  108. *
  109. * @see java.io.ByteArrayInputStream#count
  110. */
  111. public synchronized void reset() {
  112. count = 0;
  113. }
  114. /**
  115. * Creates a newly allocated byte array. Its size is the current
  116. * size of this output stream and the valid contents of the buffer
  117. * have been copied into it.
  118. *
  119. * @return the current contents of this output stream, as a byte array.
  120. * @see java.io.ByteArrayOutputStream#size()
  121. */
  122. public synchronized byte toByteArray()[] {
  123. byte newbuf[] = new byte[count];
  124. System.arraycopy(buf, 0, newbuf, 0, count);
  125. return newbuf;
  126. }
  127. /**
  128. * Returns the current size of the buffer.
  129. *
  130. * @return the value of the <code>count</code> field, which is the number
  131. * of valid bytes in this output stream.
  132. * @see java.io.ByteArrayOutputStream#count
  133. */
  134. public int size() {
  135. return count;
  136. }
  137. /**
  138. * Converts the buffer's contents into a string, translating bytes into
  139. * characters according to the platform's default character encoding.
  140. *
  141. * @return String translated from the buffer's contents.
  142. * @since JDK1.1
  143. */
  144. public String toString() {
  145. return new String(buf, 0, count);
  146. }
  147. /**
  148. * Converts the buffer's contents into a string, translating bytes into
  149. * characters according to the specified character encoding.
  150. *
  151. * @param enc a character-encoding name.
  152. * @return String translated from the buffer's contents.
  153. * @throws UnsupportedEncodingException
  154. * If the named encoding is not supported.
  155. * @since JDK1.1
  156. */
  157. public String toString(String enc) throws UnsupportedEncodingException {
  158. return new String(buf, 0, count, enc);
  159. }
  160. /**
  161. * Creates a newly allocated string. Its size is the current size of
  162. * the output stream and the valid contents of the buffer have been
  163. * copied into it. Each character <i>c</i> in the resulting string is
  164. * constructed from the corresponding element <i>b</i> in the byte
  165. * array such that:
  166. * <blockquote><pre>
  167. * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
  168. * </pre></blockquote>
  169. *
  170. * @deprecated This method does not properly convert bytes into characters.
  171. * As of JDK 1.1, the preferred way to do this is via the
  172. * <code>toString(String enc)</code> method, which takes an encoding-name
  173. * argument, or the <code>toString()</code> method, which uses the
  174. * platform's default character encoding.
  175. *
  176. * @param hibyte the high byte of each resulting Unicode character.
  177. * @return the current contents of the output stream, as a string.
  178. * @see java.io.ByteArrayOutputStream#size()
  179. * @see java.io.ByteArrayOutputStream#toString(String)
  180. * @see java.io.ByteArrayOutputStream#toString()
  181. */
  182. @Deprecated
  183. public String toString(int hibyte) {
  184. return new String(buf, hibyte, 0, count);
  185. }
  186. /**
  187. * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  188. * this class can be called after the stream has been closed without
  189. * generating an <tt>IOException</tt>.
  190. * <p>
  191. *
  192. */
  193. public void close() throws IOException {
  194. }
  195. }