Source Home >> Java Source 1.6.0 >> java.io.ByteArrayOutputStream V 0.09
  • 001/*
  • 002 * @(#)ByteArrayOutputStream.java 1.53 06/06/07
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.io;
  • 009
  • 010import java.util.Arrays;
  • 011
  • 012/**
  • 013 * This class implements an output stream in which the data is
  • 014 * written into a byte array. The buffer automatically grows as data
  • 015 * is written to it.
  • 016 * The data can be retrieved using <code>toByteArray()</code> and
  • 017 * <code>toString()</code>.
  • 018 * <p>
  • 019 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  • 020 * this class can be called after the stream has been closed without
  • 021 * generating an <tt>IOException</tt>.
  • 022 *
  • 023 * @author Arthur van Hoff
  • 024 * @version 1.53, 06/07/06
  • 025 * @since JDK1.0
  • 026 */
  • 027
  • 028public class ByteArrayOutputStream extends OutputStream {
  • 029
  • 030 /**
  • 031 * The buffer where data is stored.
  • 032 */
  • 033 protected byte buf[];
  • 034
  • 035 /**
  • 036 * The number of valid bytes in the buffer.
  • 037 */
  • 038 protected int count;
  • 039
  • 040 /**
  • 041 * Creates a new byte array output stream. The buffer capacity is
  • 042 * initially 32 bytes, though its size increases if necessary.
  • 043 */
  • 044 public ByteArrayOutputStream() {
  • 045 this(32);
  • 046 }
  • 047
  • 048 /**
  • 049 * Creates a new byte array output stream, with a buffer capacity of
  • 050 * the specified size, in bytes.
  • 051 *
  • 052 * @param size the initial size.
  • 053 * @exception IllegalArgumentException if size is negative.
  • 054 */
  • 055 public ByteArrayOutputStream(int size) {
  • 056 if (size < 0) {
  • 057 throw new IllegalArgumentException("Negative initial size: "
  • 058 + size);
  • 059 }
  • 060 buf = new byte[size];
  • 061 }
  • 062
  • 063 /**
  • 064 * Writes the specified byte to this byte array output stream.
  • 065 *
  • 066 * @param b the byte to be written.
  • 067 */
  • 068 public synchronized void write(int b) {
  • 069 int newcount = count + 1;
  • 070 if (newcount > buf.length) {
  • 071 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
  • 072 }
  • 073 buf[count] = (byte)b;
  • 074 count = newcount;
  • 075 }
  • 076
  • 077 /**
  • 078 * Writes <code>len</code> bytes from the specified byte array
  • 079 * starting at offset <code>off</code> to this byte array output stream.
  • 080 *
  • 081 * @param b the data.
  • 082 * @param off the start offset in the data.
  • 083 * @param len the number of bytes to write.
  • 084 */
  • 085 public synchronized void write(byte b[], int off, int len) {
  • 086 if ((off < 0) || (off > b.length) || (len < 0) ||
  • 087 ((off + len) > b.length) || ((off + len) < 0)) {
  • 088 throw new IndexOutOfBoundsException();
  • 089 } else if (len == 0) {
  • 090 return;
  • 091 }
  • 092 int newcount = count + len;
  • 093 if (newcount > buf.length) {
  • 094 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
  • 095 }
  • 096 System.arraycopy(b, off, buf, count, len);
  • 097 count = newcount;
  • 098 }
  • 099
  • 100 /**
  • 101 * Writes the complete contents of this byte array output stream to
  • 102 * the specified output stream argument, as if by calling the output
  • 103 * stream's write method using <code>out.write(buf, 0, count)</code>.
  • 104 *
  • 105 * @param out the output stream to which to write the data.
  • 106 * @exception IOException if an I/O error occurs.
  • 107 */
  • 108 public synchronized void writeTo(OutputStream out) throws IOException {
  • 109 out.write(buf, 0, count);
  • 110 }
  • 111
  • 112 /**
  • 113 * Resets the <code>count</code> field of this byte array output
  • 114 * stream to zero, so that all currently accumulated output in the
  • 115 * output stream is discarded. The output stream can be used again,
  • 116 * reusing the already allocated buffer space.
  • 117 *
  • 118 * @see java.io.ByteArrayInputStream#count
  • 119 */
  • 120 public synchronized void reset() {
  • 121 count = 0;
  • 122 }
  • 123
  • 124 /**
  • 125 * Creates a newly allocated byte array. Its size is the current
  • 126 * size of this output stream and the valid contents of the buffer
  • 127 * have been copied into it.
  • 128 *
  • 129 * @return the current contents of this output stream, as a byte array.
  • 130 * @see java.io.ByteArrayOutputStream#size()
  • 131 */
  • 132 public synchronized byte toByteArray()[] {
  • 133 return Arrays.copyOf(buf, count);
  • 134 }
  • 135
  • 136 /**
  • 137 * Returns the current size of the buffer.
  • 138 *
  • 139 * @return the value of the <code>count</code> field, which is the number
  • 140 * of valid bytes in this output stream.
  • 141 * @see java.io.ByteArrayOutputStream#count
  • 142 */
  • 143 public synchronized int size() {
  • 144 return count;
  • 145 }
  • 146
  • 147 /**
  • 148 * Converts the buffer's contents into a string decoding bytes using the
  • 149 * platform's default character set. The length of the new <tt>String</tt>
  • 150 * is a function of the character set, and hence may not be equal to the
  • 151 * size of the buffer.
  • 152 *
  • 153 * <p> This method always replaces malformed-input and unmappable-character
  • 154 * sequences with the default replacement string for the platform's
  • 155 * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
  • 156 * class should be used when more control over the decoding process is
  • 157 * required.
  • 158 *
  • 159 * @return String decoded from the buffer's contents.
  • 160 * @since JDK1.1
  • 161 */
  • 162 public synchronized String toString() {
  • 163 return new String(buf, 0, count);
  • 164 }
  • 165
  • 166 /**
  • 167 * Converts the buffer's contents into a string by decoding the bytes using
  • 168 * the specified {@link java.nio.charset.Charset charsetName}. The length of
  • 169 * the new <tt>String</tt> is a function of the charset, and hence may not be
  • 170 * equal to the length of the byte array.
  • 171 *
  • 172 * <p> This method always replaces malformed-input and unmappable-character
  • 173 * sequences with this charset's default replacement string. The {@link
  • 174 * java.nio.charset.CharsetDecoder} class should be used when more control
  • 175 * over the decoding process is required.
  • 176 *
  • 177 * @param charsetName the name of a supported
  • 178 * {@linkplain java.nio.charset.Charset </code>charset<code>}
  • 179 * @return String decoded from the buffer's contents.
  • 180 * @exception UnsupportedEncodingException
  • 181 * If the named charset is not supported
  • 182 * @since JDK1.1
  • 183 */
  • 184 public synchronized String toString(String charsetName)
  • 185 throws UnsupportedEncodingException
  • 186 {
  • 187 return new String(buf, 0, count, charsetName);
  • 188 }
  • 189
  • 190 /**
  • 191 * Creates a newly allocated string. Its size is the current size of
  • 192 * the output stream and the valid contents of the buffer have been
  • 193 * copied into it. Each character <i>c</i> in the resulting string is
  • 194 * constructed from the corresponding element <i>b</i> in the byte
  • 195 * array such that:
  • 196 * <blockquote><pre>
  • 197 * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
  • 198 * </pre></blockquote>
  • 199 *
  • 200 * @deprecated This method does not properly convert bytes into characters.
  • 201 * As of JDK 1.1, the preferred way to do this is via the
  • 202 * <code>toString(String enc)</code> method, which takes an encoding-name
  • 203 * argument, or the <code>toString()</code> method, which uses the
  • 204 * platform's default character encoding.
  • 205 *
  • 206 * @param hibyte the high byte of each resulting Unicode character.
  • 207 * @return the current contents of the output stream, as a string.
  • 208 * @see java.io.ByteArrayOutputStream#size()
  • 209 * @see java.io.ByteArrayOutputStream#toString(String)
  • 210 * @see java.io.ByteArrayOutputStream#toString()
  • 211 */
  • 212 @Deprecated
  • 213 public synchronized String toString(int hibyte) {
  • 214 return new String(buf, hibyte, 0, count);
  • 215 }
  • 216
  • 217 /**
  • 218 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  • 219 * this class can be called after the stream has been closed without
  • 220 * generating an <tt>IOException</tt>.
  • 221 * <p>
  • 222 *
  • 223 */
  • 224 public void close() throws IOException {
  • 225 }
  • 226
  • 227}

文件:ByteArrayOutputStream.java
包名:java.io
类名:ByteArrayOutputStream
继承:OutputStream
接口: