ByteArrayOutputStream.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* BufferedReader.java
  2. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.io;
  33. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  34. * "The Java Language Specification", ISBN 0-201-63451-1
  35. * Status: Complete to version 1.1.
  36. */
  37. /**
  38. * This class allows data to be written to a byte array buffer and
  39. * and then retrieved by an application. The internal byte array
  40. * buffer is dynamically resized to hold all the data written. Please
  41. * be aware that writing large amounts to data to this stream will
  42. * cause large amounts of memory to be allocated.
  43. * <p>
  44. * The size of the internal buffer defaults to 32 and it is resized
  45. * by doubling the size of the buffer. This default size can be
  46. * overridden by using the
  47. * <code>gnu.java.io.ByteArrayOutputStream.initialBufferSize</code>
  48. * property.
  49. * <p>
  50. * There is a constructor that specified the initial buffer size and
  51. * that is the preferred way to set that value because it it portable
  52. * across all Java class library implementations.
  53. * <p>
  54. * Note that this class also has methods that convert the byte array
  55. * buffer to a <code>String</code> using either the system default or an
  56. * application specified character encoding. Thus it can handle
  57. * multibyte character encodings.
  58. *
  59. * @author Aaron M. Renn (arenn@urbanophile.com)
  60. * @author Tom Tromey (tromey@cygnus.com)
  61. * @date September 24, 1998
  62. */
  63. public class ByteArrayOutputStream extends OutputStream
  64. {
  65. /**
  66. * This method initializes a new <code>ByteArrayOutputStream</code>
  67. * with the default buffer size of 32 bytes. If a different initial
  68. * buffer size is desired, see the constructor
  69. * <code>ByteArrayOutputStream(int size)</code>. For applications
  70. * where the source code is not available, the default buffer size
  71. * can be set using the system property
  72. * <code>gnu.java.io.ByteArrayOutputStream.initialBufferSize</code>
  73. */
  74. public ByteArrayOutputStream ()
  75. {
  76. this (initial_buffer_size);
  77. }
  78. /**
  79. * This method initializes a new <code>ByteArrayOutputStream</code> with
  80. * a specified initial buffer size.
  81. *
  82. * @param size The initial buffer size in bytes
  83. */
  84. public ByteArrayOutputStream (int size)
  85. {
  86. buf = new byte[size];
  87. count = 0;
  88. }
  89. /**
  90. * This method discards all of the bytes that have been written to
  91. * the internal buffer so far by setting the <code>count</code>
  92. * variable to 0. The internal buffer remains at its currently
  93. * allocated size.
  94. */
  95. public synchronized void reset ()
  96. {
  97. count = 0;
  98. }
  99. /**
  100. * This method returns the number of bytes that have been written to
  101. * the buffer so far. This is the same as the value of the protected
  102. * <code>count</code> variable. If the <code>reset</code> method is
  103. * called, then this value is reset as well. Note that this method does
  104. * not return the length of the internal buffer, but only the number
  105. * of bytes that have been written to it.
  106. *
  107. * @return The number of bytes in the internal buffer
  108. *
  109. * @see #reset()
  110. */
  111. public int size ()
  112. {
  113. return count;
  114. }
  115. /**
  116. * This method returns a byte array containing the bytes that have been
  117. * written to this stream so far. This array is a copy of the valid
  118. * bytes in the internal buffer and its length is equal to the number of
  119. * valid bytes, not necessarily to the the length of the current
  120. * internal buffer. Note that since this method allocates a new array,
  121. * it should be used with caution when the internal buffer is very large.
  122. */
  123. public synchronized byte[] toByteArray ()
  124. {
  125. byte[] ret = new byte[count];
  126. System.arraycopy(buf, 0, ret, 0, count);
  127. return ret;
  128. }
  129. /**
  130. * Returns the bytes in the internal array as a <code>String</code>. The
  131. * bytes in the buffer are converted to characters using the system default
  132. * encoding. There is an overloaded <code>toString()</code> method that
  133. * allows an application specified character encoding to be used.
  134. *
  135. * @return A <code>String</code> containing the data written to this
  136. * stream so far
  137. */
  138. public String toString ()
  139. {
  140. return new String (buf, 0, count);
  141. }
  142. /**
  143. * Returns the bytes in the internal array as a <code>String</code>. The
  144. * bytes in the buffer are converted to characters using the specified
  145. * encoding.
  146. *
  147. * @param enc The name of the character encoding to use
  148. *
  149. * @return A <code>String</code> containing the data written to this
  150. * stream so far
  151. *
  152. * @exception UnsupportedEncodingException If the named encoding is
  153. * not available
  154. */
  155. public String toString (String enc) throws UnsupportedEncodingException
  156. {
  157. return new String (buf, 0, count, enc);
  158. }
  159. /**
  160. * This method returns the bytes in the internal array as a
  161. * <code>String</code>. It uses each byte in the array as the low
  162. * order eight bits of the Unicode character value and the passed in
  163. * parameter as the high eight bits.
  164. * <p>
  165. * This method does not convert bytes to characters in the proper way and
  166. * so is deprecated in favor of the other overloaded <code>toString</code>
  167. * methods which use a true character encoding.
  168. *
  169. * @param hibyte The high eight bits to use for each character in
  170. * the <code>String</code>
  171. *
  172. * @return A <code>String</code> containing the data written to this
  173. * stream so far
  174. *
  175. * @deprecated
  176. */
  177. public String toString (int hibyte)
  178. {
  179. return new String (buf, hibyte, 0, count);
  180. }
  181. // Resize buffer to accommodate new bytes.
  182. private void resize (int add)
  183. {
  184. if (count + add > buf.length)
  185. {
  186. int newlen = buf.length * 2;
  187. if (count + add > newlen)
  188. newlen = count + add;
  189. byte[] newbuf = new byte[newlen];
  190. System.arraycopy(buf, 0, newbuf, 0, count);
  191. buf = newbuf;
  192. }
  193. }
  194. /**
  195. * This method writes the writes the specified byte into the internal
  196. * buffer.
  197. *
  198. * @param oneByte The byte to be read passed as an int
  199. */
  200. public synchronized void write (int oneByte)
  201. {
  202. resize (1);
  203. buf[count++] = (byte) oneByte;
  204. }
  205. /**
  206. * This method writes <code>len</code> bytes from the passed in array
  207. * <code>buf</code> starting at index <code>offset</code> into the
  208. * internal buffer.
  209. *
  210. * @param buffer The byte array to write data from
  211. * @param offset The index into the buffer to start writing data from
  212. * @param add The number of bytes to write
  213. */
  214. public synchronized void write (byte[] buffer, int offset, int add)
  215. {
  216. // If ADD < 0 then arraycopy will throw the appropriate error for
  217. // us.
  218. if (add >= 0)
  219. resize (add);
  220. System.arraycopy(buffer, offset, buf, count, add);
  221. count += add;
  222. }
  223. /**
  224. * This method writes all the bytes that have been written to this stream
  225. * from the internal buffer to the specified <code>OutputStream</code>.
  226. *
  227. * @param out The <code>OutputStream</code> to write to
  228. *
  229. * @exception IOException If an error occurs
  230. */
  231. public synchronized void writeTo (OutputStream out) throws IOException
  232. {
  233. out.write(buf, 0, count);
  234. }
  235. /**
  236. * The internal buffer where the data written is stored
  237. */
  238. protected byte[] buf;
  239. /**
  240. * The number of bytes that have been written to the buffer
  241. */
  242. protected int count;
  243. /**
  244. * The default initial buffer size. Specified by the JCL.
  245. */
  246. private static final int DEFAULT_INITIAL_BUFFER_SIZE = 32;
  247. // The default buffer size which can be overridden by the user.
  248. private static final int initial_buffer_size;
  249. static
  250. {
  251. int r
  252. = Integer.getInteger ("gnu.java.io.ByteArrayOutputStream.initialBufferSize",
  253. DEFAULT_INITIAL_BUFFER_SIZE).intValue ();
  254. if (r <= 0)
  255. r = DEFAULT_INITIAL_BUFFER_SIZE;
  256. initial_buffer_size = r;
  257. }
  258. }