<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FOutputStream</id>
		<title>Java Tutorial/File/OutputStream - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FOutputStream"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/OutputStream&amp;action=history"/>
		<updated>2026-04-07T00:34:27Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/File/OutputStream&amp;diff=5410&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/OutputStream&amp;diff=5410&amp;oldid=prev"/>
				<updated>2010-06-01T05:20:32Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:20, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/File/OutputStream&amp;diff=5409&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/OutputStream&amp;diff=5409&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  A null output stream. All data written to this stream is ignored. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * &lt;br /&gt;
 * JFreeReport : a free Java reporting library&lt;br /&gt;
 * &lt;br /&gt;
 *&lt;br /&gt;
 * Project Info:  http://reporting.pentaho.org/&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.&lt;br /&gt;
 *&lt;br /&gt;
 * This library is free software; you can redistribute it and/or modify it under the terms&lt;br /&gt;
 * of the GNU Lesser General Public License as published by the Free Software Foundation;&lt;br /&gt;
 * either version 2.1 of the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;&lt;br /&gt;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.&lt;br /&gt;
 * See the GNU Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public License along with this&lt;br /&gt;
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,&lt;br /&gt;
 * Boston, MA 02111-1307, USA.&lt;br /&gt;
 *&lt;br /&gt;
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.&lt;br /&gt;
 * in the United States and other countries.]&lt;br /&gt;
 *&lt;br /&gt;
 * ------------&lt;br /&gt;
 * NullOutputStream.java&lt;br /&gt;
 * ------------&lt;br /&gt;
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
/**&lt;br /&gt;
 * A null output stream. All data written to this stream is ignored.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Thomas Morgner&lt;br /&gt;
 */&lt;br /&gt;
public class NullOutputStream extends OutputStream&lt;br /&gt;
{&lt;br /&gt;
  /**&lt;br /&gt;
   * Default constructor.&lt;br /&gt;
   */&lt;br /&gt;
  public NullOutputStream ()&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Writes to the stream (in this case, does nothing).&lt;br /&gt;
   *&lt;br /&gt;
   * @param i the value.&lt;br /&gt;
   * @throws IOException if there is an I/O problem.&lt;br /&gt;
   */&lt;br /&gt;
  public void write (final int i)&lt;br /&gt;
          throws IOException&lt;br /&gt;
  {&lt;br /&gt;
    // no i wont do anything here ...&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Writes to the stream (in this case, does nothing).&lt;br /&gt;
   *&lt;br /&gt;
   * @param bytes the bytes.&lt;br /&gt;
   * @throws IOException if there is an I/O problem.&lt;br /&gt;
   */&lt;br /&gt;
  public void write (final byte[] bytes)&lt;br /&gt;
          throws IOException&lt;br /&gt;
  {&lt;br /&gt;
    // no i wont do anything here ...&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Writes to the stream (in this case, does nothing).&lt;br /&gt;
   *&lt;br /&gt;
   * @param bytes the bytes.&lt;br /&gt;
   * @param off   the start offset in the data.&lt;br /&gt;
   * @param len   the number of bytes to write.&lt;br /&gt;
   * @throws IOException if there is an I/O problem.&lt;br /&gt;
   */&lt;br /&gt;
  public void write (final byte[] bytes, final int off, final int len)&lt;br /&gt;
          throws IOException&lt;br /&gt;
  {&lt;br /&gt;
    // no i wont do anything here ...&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Byte Counting OutputStream ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
/**&lt;br /&gt;
 * Output stream that counts bytes written to it (but discards them).&lt;br /&gt;
 * &lt;br /&gt;
 * @author Jonathan Locke&lt;br /&gt;
 */&lt;br /&gt;
public final class ByteCountingOutputStream extends OutputStream&lt;br /&gt;
{&lt;br /&gt;
  private long size;&lt;br /&gt;
  /**&lt;br /&gt;
   * @see java.io.OutputStream#write(int)&lt;br /&gt;
   */&lt;br /&gt;
  public void write(int b) throws IOException&lt;br /&gt;
  {&lt;br /&gt;
    size++;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * @see java.io.OutputStream#write(byte[], int, int)&lt;br /&gt;
   */&lt;br /&gt;
  public void write(byte b[], int off, int len) throws IOException&lt;br /&gt;
  {&lt;br /&gt;
    size += len;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * @return Number of bytes written to this stream&lt;br /&gt;
   */&lt;br /&gt;
  public long size()&lt;br /&gt;
  {&lt;br /&gt;
    return size;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Memory Byte Array OutputStream ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * &lt;br /&gt;
 * JFreeReport : a free Java reporting library&lt;br /&gt;
 * &lt;br /&gt;
 *&lt;br /&gt;
 * Project Info:  http://reporting.pentaho.org/&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.&lt;br /&gt;
 *&lt;br /&gt;
 * This library is free software; you can redistribute it and/or modify it under the terms&lt;br /&gt;
 * of the GNU Lesser General Public License as published by the Free Software Foundation;&lt;br /&gt;
 * either version 2.1 of the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;&lt;br /&gt;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.&lt;br /&gt;
 * See the GNU Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public License along with this&lt;br /&gt;
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,&lt;br /&gt;
 * Boston, MA 02111-1307, USA.&lt;br /&gt;
 *&lt;br /&gt;
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.&lt;br /&gt;
 * in the United States and other countries.]&lt;br /&gt;
 *&lt;br /&gt;
 * ------------&lt;br /&gt;
 * MemoryByteArrayOutputStream.java&lt;br /&gt;
 * ------------&lt;br /&gt;
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
/**&lt;br /&gt;
 * A string writer that is able to write large amounts of data. The original&lt;br /&gt;
 * StringWriter contained in Java doubles its buffersize everytime the buffer&lt;br /&gt;
 * overflows. This is nice with small amounts of data, but awfull for huge&lt;br /&gt;
 * buffers.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Thomas Morgner&lt;br /&gt;
 */&lt;br /&gt;
public class MemoryByteArrayOutputStream extends OutputStream {&lt;br /&gt;
  private int maximumBufferIncrement;&lt;br /&gt;
  private int cursor;&lt;br /&gt;
  private byte[] buffer;&lt;br /&gt;
  private byte[] singleIntArray;&lt;br /&gt;
  /**&lt;br /&gt;
   * Create a new character-stream writer whose critical sections will&lt;br /&gt;
   * synchronize on the writer itself.&lt;br /&gt;
   */&lt;br /&gt;
  public MemoryByteArrayOutputStream() {&lt;br /&gt;
    this(4096, 65536);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Create a new character-stream writer whose critical sections will&lt;br /&gt;
   * synchronize on the writer itself.&lt;br /&gt;
   */&lt;br /&gt;
  public MemoryByteArrayOutputStream(final int bufferSize, final int maximumBufferIncrement) {&lt;br /&gt;
    this.maximumBufferIncrement = maximumBufferIncrement;&lt;br /&gt;
    this.buffer = new byte[bufferSize];&lt;br /&gt;
    this.singleIntArray = new byte[1];&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Write a portion of an array of characters.&lt;br /&gt;
   * &lt;br /&gt;
   * @param cbuf&lt;br /&gt;
   *          Array of characters&lt;br /&gt;
   * @param off&lt;br /&gt;
   *          Offset from which to start writing characters&lt;br /&gt;
   * @param len&lt;br /&gt;
   *          Number of characters to write&lt;br /&gt;
   * @throws java.io.IOException&lt;br /&gt;
   *           If an I/O error occurs&lt;br /&gt;
   */&lt;br /&gt;
  public synchronized void write(final byte[] cbuf, final int off, final int len)&lt;br /&gt;
      throws IOException {&lt;br /&gt;
    if (len &amp;lt; 0) {&lt;br /&gt;
      throw new IllegalArgumentException();&lt;br /&gt;
    }&lt;br /&gt;
    if (off &amp;lt; 0) {&lt;br /&gt;
      throw new IndexOutOfBoundsException();&lt;br /&gt;
    }&lt;br /&gt;
    if (cbuf == null) {&lt;br /&gt;
      throw new NullPointerException();&lt;br /&gt;
    }&lt;br /&gt;
    if ((len + off) &amp;gt; cbuf.length) {&lt;br /&gt;
      throw new IndexOutOfBoundsException();&lt;br /&gt;
    }&lt;br /&gt;
    ensureSize(cursor + len);&lt;br /&gt;
    System.arraycopy(cbuf, off, this.buffer, cursor, len);&lt;br /&gt;
    cursor += len;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Writes &amp;lt;code&amp;gt;b.length&amp;lt;/code&amp;gt; bytes from the specified byte array to this&lt;br /&gt;
   * output stream. The general contract for &amp;lt;code&amp;gt;write(b)&amp;lt;/code&amp;gt; is that it&lt;br /&gt;
   * should have exactly the same effect as the call &amp;lt;code&amp;gt;write(b, 0,&lt;br /&gt;
   * b.length)&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * &lt;br /&gt;
   * @param b&lt;br /&gt;
   *          the data.&lt;br /&gt;
   * @throws java.io.IOException&lt;br /&gt;
   *           if an I/O error occurs.&lt;br /&gt;
   * @see java.io.OutputStream#write(byte[], int, int)&lt;br /&gt;
   */&lt;br /&gt;
  public void write(final byte[] b) throws IOException {&lt;br /&gt;
    write(b, 0, b.length);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Writes the specified byte to this output stream. The general contract for&lt;br /&gt;
   * &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt; is that one byte is written to the output stream. The&lt;br /&gt;
   * byte to be written is the eight low-order bits of the argument&lt;br /&gt;
   * &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;. The 24 high-order bits of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are ignored.&lt;br /&gt;
   * &amp;lt;p/&amp;gt; Subclasses of &amp;lt;code&amp;gt;OutputStream&amp;lt;/code&amp;gt; must provide an&lt;br /&gt;
   * implementation for this method.&lt;br /&gt;
   * &lt;br /&gt;
   * @param b&lt;br /&gt;
   *          the &amp;lt;code&amp;gt;byte&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * @throws java.io.IOException&lt;br /&gt;
   *           if an I/O error occurs. In particular, an&lt;br /&gt;
   *           &amp;lt;code&amp;gt;IOException&amp;lt;/code&amp;gt; may be thrown if the output stream has&lt;br /&gt;
   *           been closed.&lt;br /&gt;
   */&lt;br /&gt;
  public synchronized void write(final int b) throws IOException {&lt;br /&gt;
    this.singleIntArray[0] = (byte) (0xFF &amp;amp; b);&lt;br /&gt;
    write(singleIntArray, 0, 1);&lt;br /&gt;
  }&lt;br /&gt;
  private void ensureSize(final int size) {&lt;br /&gt;
    if (this.buffer.length &amp;gt;= size) {&lt;br /&gt;
      return;&lt;br /&gt;
    }&lt;br /&gt;
    final int computedSize = (int) Math.min((this.buffer.length + 1) * 1.5, this.buffer.length&lt;br /&gt;
        + maximumBufferIncrement);&lt;br /&gt;
    final int newSize = Math.max(size, computedSize);&lt;br /&gt;
    final byte[] newBuffer = new byte[newSize];&lt;br /&gt;
    System.arraycopy(this.buffer, 0, newBuffer, 0, cursor);&lt;br /&gt;
    this.buffer = newBuffer;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Flush the stream. If the stream has saved any characters from the various&lt;br /&gt;
   * write() methods in a buffer, write them immediately to their intended&lt;br /&gt;
   * destination. Then, if that destination is another character or byte stream,&lt;br /&gt;
   * flush it. Thus one flush() invocation will flush all the buffers in a chain&lt;br /&gt;
   * of Writers and OutputStreams. &amp;lt;p/&amp;gt; If the intended destination of this&lt;br /&gt;
   * stream is an abstraction provided by the underlying operating system, for&lt;br /&gt;
   * example a file, then flushing the stream guarantees only that bytes&lt;br /&gt;
   * previously written to the stream are passed to the operating system for&lt;br /&gt;
   * writing; it does not guarantee that they are actually written to a physical&lt;br /&gt;
   * device such as a disk drive.&lt;br /&gt;
   * &lt;br /&gt;
   * @throws java.io.IOException&lt;br /&gt;
   *           If an I/O error occurs&lt;br /&gt;
   */&lt;br /&gt;
  public void flush() throws IOException {&lt;br /&gt;
    if ((buffer.length - cursor) &amp;gt; 50000) {&lt;br /&gt;
      System.out.println(&amp;quot;WASTED: &amp;quot; + (buffer.length - cursor));&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Close the stream, flushing it first. Once a stream has been closed, further&lt;br /&gt;
   * write() or flush() invocations will cause an IOException to be thrown.&lt;br /&gt;
   * Closing a previously-closed stream, however, has no effect.&lt;br /&gt;
   * &lt;br /&gt;
   * @throws java.io.IOException&lt;br /&gt;
   *           If an I/O error occurs&lt;br /&gt;
   */&lt;br /&gt;
  public void close() throws IOException {&lt;br /&gt;
  }&lt;br /&gt;
  public synchronized byte[] toByteArray() {&lt;br /&gt;
    final byte[] retval = new byte[cursor];&lt;br /&gt;
    System.arraycopy(buffer, 0, retval, 0, cursor);&lt;br /&gt;
    return retval;&lt;br /&gt;
  }&lt;br /&gt;
  public int getLength() {&lt;br /&gt;
    return cursor;&lt;br /&gt;
  }&lt;br /&gt;
  public byte[] getRaw() {&lt;br /&gt;
    if ((buffer.length - cursor) &amp;gt; 50000) {&lt;br /&gt;
      System.out.println(&amp;quot;WASTED: &amp;quot; + (buffer.length - cursor) + &amp;quot; Length: &amp;quot; + buffer.length);&lt;br /&gt;
    }&lt;br /&gt;
    return buffer;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Provides true Closable semantics ordinarily missing in a {@link java.io.ByteArrayOutputStream}. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/* Copyright (c) 2001-2009, The HSQL Development Group&lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions are met:&lt;br /&gt;
 *&lt;br /&gt;
 * Redistributions of source code must retain the above copyright notice, this&lt;br /&gt;
 * list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistributions in binary form must reproduce the above copyright notice,&lt;br /&gt;
 * this list of conditions and the following disclaimer in the documentation&lt;br /&gt;
 * and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 * Neither the name of the HSQL Development Group nor the names of its&lt;br /&gt;
 * contributors may be used to endorse or promote products derived from this&lt;br /&gt;
 * software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS IS&amp;quot;&lt;br /&gt;
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE&lt;br /&gt;
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE&lt;br /&gt;
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,&lt;br /&gt;
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;&lt;br /&gt;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND&lt;br /&gt;
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT&lt;br /&gt;
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.io.ByteArrayInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
import java.io.UnsupportedEncodingException;&lt;br /&gt;
/* $Id: ClosableByteArrayOutputStream.java 2946 2009-03-22 17:44:48Z fredt $ */&lt;br /&gt;
/**&lt;br /&gt;
 * @todo - finer-grained synchronization to reduce average&lt;br /&gt;
 * potential monitor contention&lt;br /&gt;
 */&lt;br /&gt;
/**&lt;br /&gt;
 * Provides true Closable semantics ordinarily missing in a&lt;br /&gt;
 * {@link java.io.ByteArrayOutputStream}. &lt;br /&gt;
 *&lt;br /&gt;
 * Accumulates output in a byte array that automatically grows as needed.&lt;br /&gt;
 *&lt;br /&gt;
 * Data is retrieved using &amp;lt;tt&amp;gt;toByteArray()&amp;lt;/tt&amp;gt;,&lt;br /&gt;
 * &amp;lt;tt&amp;gt;toByteArrayInputStream()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;toString()&amp;lt;/tt&amp;gt; and&lt;br /&gt;
 * &amp;lt;tt&amp;gt;toString(encoding)&amp;lt;/tt&amp;gt;. &lt;br /&gt;
 *&lt;br /&gt;
 * {@link #close() Closing} a &amp;lt;tt&amp;gt;ClosableByteArrayOutputStream&amp;lt;/tt&amp;gt; prevents&lt;br /&gt;
 * further write operations, but all other operations may succeed until after&lt;br /&gt;
 * the first invocation of {@link #free() free()}.&lt;br /&gt;
 *&lt;br /&gt;
 * Freeing a &amp;lt;tt&amp;gt;ClosableByteArrayOutputStream&amp;lt;/tt&amp;gt; closes the stream and&lt;br /&gt;
 * releases the internal buffer, preventing successful invocation of all&lt;br /&gt;
 * operations, with the exception of &amp;lt;tt&amp;gt;size()&amp;lt;tt&amp;gt;, &amp;lt;tt&amp;gt;close()&amp;lt;/tt&amp;gt;,&lt;br /&gt;
 * &amp;lt;tt&amp;gt;isClosed()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;free()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;isFreed()&amp;lt;/tt&amp;gt;. &lt;br /&gt;
 *&lt;br /&gt;
 * This class is especially useful when an accumulating output stream must be&lt;br /&gt;
 * handed off to an extenal client under contract that the stream should&lt;br /&gt;
 * exhibit true Closable behaviour in response both to internally tracked&lt;br /&gt;
 * events and to client invocation of the &amp;lt;tt&amp;gt;OutputStream.close()&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
 *&lt;br /&gt;
 * @author boucherb@users&lt;br /&gt;
 * @version 1.9.0&lt;br /&gt;
 * @since 1.9.0&lt;br /&gt;
 */&lt;br /&gt;
public class ClosableByteArrayOutputStream extends OutputStream {&lt;br /&gt;
    /**&lt;br /&gt;
     * Data buffer.&lt;br /&gt;
     */&lt;br /&gt;
    protected byte[] buf;&lt;br /&gt;
    /**&lt;br /&gt;
     * # of valid bytes in buffer.&lt;br /&gt;
     */&lt;br /&gt;
    protected int count;&lt;br /&gt;
    /**&lt;br /&gt;
     * Whether this stream is closed.&lt;br /&gt;
     */&lt;br /&gt;
    protected boolean closed;&lt;br /&gt;
    /**&lt;br /&gt;
     * Whether this stream is freed.&lt;br /&gt;
     */&lt;br /&gt;
    protected boolean freed;&lt;br /&gt;
    /**&lt;br /&gt;
     * Creates a new output stream. &lt;br /&gt;
     *&lt;br /&gt;
     * The buffer capacity is initially 32 bytes, though its size increases&lt;br /&gt;
     * if necessary.&lt;br /&gt;
     */&lt;br /&gt;
    public ClosableByteArrayOutputStream() {&lt;br /&gt;
        this(32);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Creates a new output stream with a buffer capacity of the specified&lt;br /&gt;
     * &amp;lt;tt&amp;gt;size&amp;lt;/tt&amp;gt;, in bytes.&lt;br /&gt;
     *&lt;br /&gt;
     * @param size the initial size.&lt;br /&gt;
     * @exception IllegalArgumentException if size is negative.&lt;br /&gt;
     */&lt;br /&gt;
    public ClosableByteArrayOutputStream(int size)&lt;br /&gt;
    throws IllegalArgumentException {&lt;br /&gt;
        if (size &amp;lt; 0) {&lt;br /&gt;
            throw new IllegalArgumentException(&amp;quot;Negative initial size: &amp;quot;&lt;br /&gt;
                                               + size);    // NOI18N&lt;br /&gt;
        }&lt;br /&gt;
        buf = new byte[size];&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Writes the specified single byte.&lt;br /&gt;
     *&lt;br /&gt;
     * @param b the single byte to be written.&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #close() closed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void write(int b) throws IOException {&lt;br /&gt;
        checkClosed();&lt;br /&gt;
        int newcount = count + 1;&lt;br /&gt;
        if (newcount &amp;gt; buf.length) {&lt;br /&gt;
            buf = copyOf(buf, Math.max(buf.length &amp;lt;&amp;lt; 1, newcount));&lt;br /&gt;
        }&lt;br /&gt;
        buf[count] = (byte) b;&lt;br /&gt;
        count      = newcount;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Writes the specified portion of the designated octet sequence. &lt;br /&gt;
     *&lt;br /&gt;
     * @param b the data.&lt;br /&gt;
     * @param off the start offset in the data.&lt;br /&gt;
     * @param len the number of bytes to write.&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #close() closed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void write(byte b[], int off,&lt;br /&gt;
                                   int len) throws IOException {&lt;br /&gt;
        checkClosed();&lt;br /&gt;
        if ((off &amp;lt; 0) || (off &amp;gt; b.length) || (len &amp;lt; 0)&lt;br /&gt;
                || ((off + len) &amp;gt; b.length) || ((off + len) &amp;lt; 0)) {&lt;br /&gt;
            throw new IndexOutOfBoundsException();&lt;br /&gt;
        } else if (len == 0) {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
        int newcount = count + len;&lt;br /&gt;
        if (newcount &amp;gt; buf.length) {&lt;br /&gt;
            buf = copyOf(buf, Math.max(buf.length &amp;lt;&amp;lt; 1, newcount));&lt;br /&gt;
        }&lt;br /&gt;
        System.arraycopy(b, off, buf, count, len);&lt;br /&gt;
        count = newcount;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * By default, does nothing. &lt;br /&gt;
     *&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #close() closed}.&lt;br /&gt;
     */&lt;br /&gt;
    public void flush() throws IOException {&lt;br /&gt;
        checkClosed();&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Writes the complete contents of this stream&amp;quot;s accumulated data to the&lt;br /&gt;
     * specified output stream. &lt;br /&gt;
     *&lt;br /&gt;
     * The operation occurs as if by calling &amp;lt;tt&amp;gt;out.write(buf, 0, count)&amp;lt;/tt&amp;gt;.&lt;br /&gt;
     *&lt;br /&gt;
     * @param out the output stream to which to write the data.&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #free() freed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void writeTo(OutputStream out) throws IOException {&lt;br /&gt;
        checkFreed();&lt;br /&gt;
        out.write(buf, 0, count);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the current capacity of this stream&amp;quot;s data buffer.&lt;br /&gt;
     *&lt;br /&gt;
     * @return  the length of the internal data array&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #free() freed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized int capacity() throws IOException {&lt;br /&gt;
        checkFreed();&lt;br /&gt;
        return buf.length;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Resets the &amp;lt;tt&amp;gt;count&amp;lt;/tt&amp;gt; field of this output stream to zero, so that&lt;br /&gt;
     * all currently accumulated data is effectively discarded. &lt;br /&gt;
     *&lt;br /&gt;
     * Further write operations will reuse the allocated buffer space. &lt;br /&gt;
     *&lt;br /&gt;
     * @see #count&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #close() closed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void reset() throws IOException {&lt;br /&gt;
        checkClosed();&lt;br /&gt;
        count = 0;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Attempts to reduce this stream&amp;quot;s capacity to its current size. &lt;br /&gt;
     *&lt;br /&gt;
     * If the data buffer is larger than necessary to hold its current sequence&lt;br /&gt;
     * of bytes, then it may be resized to become more space efficient.&lt;br /&gt;
     * Calling this method may, but is not required to, affect the value&lt;br /&gt;
     * returned by a subsequent call to the {@link #capacity()} method. &lt;br /&gt;
     *&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #free() freed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void trimToSize() throws IOException {&lt;br /&gt;
        checkFreed();&lt;br /&gt;
        if (buf.length &amp;gt; count) {&lt;br /&gt;
            buf = copyOf(buf, count);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves a copy of this stream&amp;quot;s accumated data, as a byte array.&lt;br /&gt;
     *&lt;br /&gt;
     * @return a copy of this stream&amp;quot;s accumated data, as a byte array.&lt;br /&gt;
     * @see #size()&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #free() freed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized byte[] toByteArray() throws IOException {&lt;br /&gt;
        checkFreed();&lt;br /&gt;
        return copyOf(buf, count);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the current size of this stream&amp;quot;s accumated data.&lt;br /&gt;
     *&lt;br /&gt;
     * @return the value of the &amp;lt;tt&amp;gt;count&amp;lt;/tt&amp;gt; field, which is the number&lt;br /&gt;
     *      of valid bytes in this output stream.&lt;br /&gt;
     * @see #count&lt;br /&gt;
     * @throws java.io.IOException never&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized int size() throws IOException {&lt;br /&gt;
        return count;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets the size of this stream&amp;quot;s accumulated data. &lt;br /&gt;
     *&lt;br /&gt;
     * @param   newSize the new size&lt;br /&gt;
     * @throws  ArrayIndexOutOfBoundsException if new size is negative&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void setSize(int newSize) {&lt;br /&gt;
        if (newSize &amp;lt; 0) {&lt;br /&gt;
            throw new ArrayIndexOutOfBoundsException(newSize);&lt;br /&gt;
        } else if (newSize &amp;gt; buf.length) {&lt;br /&gt;
            buf = copyOf(buf, Math.max(buf.length &amp;lt;&amp;lt; 1, newSize));&lt;br /&gt;
        }&lt;br /&gt;
        count = newSize;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Performs an effecient (zero-copy) conversion of the data accumulated in&lt;br /&gt;
     * this output stream to an input stream. &lt;br /&gt;
     *&lt;br /&gt;
     * To ensure the future integrity of the resulting input stream, {@link&lt;br /&gt;
     * #free() free} is invoked upon this output stream as a side-effect.&lt;br /&gt;
     *&lt;br /&gt;
     * @return an input stream representing this output stream&amp;quot;s accumulated&lt;br /&gt;
     *      data&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs.&lt;br /&gt;
     *      In particular, an &amp;lt;tt&amp;gt;IOException&amp;lt;/tt&amp;gt; may be thrown&lt;br /&gt;
     *      if this output stream has been {@link #free() freed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized ByteArrayInputStream toByteArrayInputStream()&lt;br /&gt;
    throws IOException {&lt;br /&gt;
        checkFreed();&lt;br /&gt;
        ByteArrayInputStream inputStream = new ByteArrayInputStream(buf, 0,&lt;br /&gt;
            count);&lt;br /&gt;
        free();&lt;br /&gt;
        return inputStream;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Converts this stream&amp;quot;s accumuated data into a string, translating bytes&lt;br /&gt;
     * into characters according to the platform&amp;quot;s default character encoding.&lt;br /&gt;
     *&lt;br /&gt;
     * @return String translated from this stream&amp;quot;s accumuated data.&lt;br /&gt;
     * @throws RuntimeException may be thrown if this output stream has been&lt;br /&gt;
     *      {@link #free() freed}.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized String toString() {&lt;br /&gt;
        try {&lt;br /&gt;
            checkFreed();&lt;br /&gt;
        } catch (IOException ex) {&lt;br /&gt;
            throw new RuntimeException(ex.toString());&lt;br /&gt;
        }&lt;br /&gt;
        return new String(buf, 0, count);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Converts this stream&amp;quot;s accumuated data into a string, translating bytes&lt;br /&gt;
     * into characters according to the specified character encoding.&lt;br /&gt;
     *&lt;br /&gt;
     * @return String translated from the buffer&amp;quot;s contents.&lt;br /&gt;
     * @param enc a character-encoding name.&lt;br /&gt;
     * @throws java.io.IOException may be thrown if this output stream has been&lt;br /&gt;
     *      {@link #free() freed}.&lt;br /&gt;
     * @throws UnsupportedEncodingException If the named encoding is not&lt;br /&gt;
     *      supported.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized String toString(String enc)&lt;br /&gt;
    throws IOException, UnsupportedEncodingException {&lt;br /&gt;
        checkFreed();&lt;br /&gt;
        return new String(buf, 0, count, enc);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Closes this object for further writing. &lt;br /&gt;
     *&lt;br /&gt;
     * Other operations may continue to succeed until after the first invocation&lt;br /&gt;
     * of {@link #free() free()}. &lt;br /&gt;
     *&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs (default: never)&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void close() throws IOException {&lt;br /&gt;
        closed = true;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves whether this stream is closed. &lt;br /&gt;
     * @return &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; if this stream is closed, else &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized boolean isClosed() {&lt;br /&gt;
        return closed;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Closes this object and releases the underlying buffer for&lt;br /&gt;
     * garbage collection. &lt;br /&gt;
     *&lt;br /&gt;
     * @throws java.io.IOException if an I/O error occurs while closing&lt;br /&gt;
     *      this stream (default: never).&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized void free() throws IOException {&lt;br /&gt;
        closed = true;&lt;br /&gt;
        freed  = true;&lt;br /&gt;
        buf    = null;&lt;br /&gt;
        count  = 0;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves whether this stream is freed. &lt;br /&gt;
     *&lt;br /&gt;
     * @return &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; if this stream is freed; else &amp;lt;tt&amp;gt;false&amp;lt;/tt&amp;gt;.&lt;br /&gt;
     */&lt;br /&gt;
    public synchronized boolean isFreed() {&lt;br /&gt;
        return freed;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests whether this stream is closed. &lt;br /&gt;
     *&lt;br /&gt;
     * @throws java.io.IOException if this stream is closed.&lt;br /&gt;
     */&lt;br /&gt;
    protected synchronized void checkClosed() throws IOException {&lt;br /&gt;
        if (closed) {&lt;br /&gt;
            throw new IOException(&amp;quot;stream is closed.&amp;quot;);    // NOI18N&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests whether this stream is freed. &lt;br /&gt;
     *&lt;br /&gt;
     * @throws java.io.IOException if this stream is freed.&lt;br /&gt;
     */&lt;br /&gt;
    protected synchronized void checkFreed() throws IOException {&lt;br /&gt;
        if (freed) {&lt;br /&gt;
            throw new IOException(&amp;quot;stream buffer is freed.&amp;quot;);    // NOI18N&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves a copy of &amp;lt;tt&amp;gt;original&amp;lt;/tt&amp;gt; with the given&lt;br /&gt;
     * &amp;lt;tt&amp;gt;newLength&amp;lt;/tt&amp;gt;. &lt;br /&gt;
     *&lt;br /&gt;
     * @param original the object to copy&lt;br /&gt;
     * @param newLength the length of the copy&lt;br /&gt;
     * @return copy of &amp;lt;tt&amp;gt;original&amp;lt;/tt&amp;gt; with the given &amp;lt;tt&amp;gt;newLength&amp;lt;/tt&amp;gt;&lt;br /&gt;
     */&lt;br /&gt;
    protected byte[] copyOf(byte[] original, int newLength) {&lt;br /&gt;
        byte[] copy = new byte[newLength];&lt;br /&gt;
        System.arraycopy(original, 0, copy, 0,&lt;br /&gt;
                         Math.min(original.length, newLength));&lt;br /&gt;
        return copy;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Transfers all bytes that can be read from one stream to another stream. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
&lt;br /&gt;
  /**&lt;br /&gt;
   * Transfers all bytes that can be read from &amp;lt;tt&amp;gt;in&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;out&amp;lt;/tt&amp;gt;.&lt;br /&gt;
   *&lt;br /&gt;
   * @param in The InputStream to read data from.&lt;br /&gt;
   * @param out The OutputStream to write data to.&lt;br /&gt;
   * @return The total number of bytes transfered.&lt;br /&gt;
   */&lt;br /&gt;
  public static final long transfer(InputStream in, OutputStream out)&lt;br /&gt;
    throws IOException&lt;br /&gt;
  {&lt;br /&gt;
    long totalBytes = 0;&lt;br /&gt;
    int bytesInBuf = 0;&lt;br /&gt;
    byte[] buf = new byte[4096];&lt;br /&gt;
    while ((bytesInBuf = in.read(buf)) != -1) {&lt;br /&gt;
      out.write(buf, 0, bytesInBuf);&lt;br /&gt;
      totalBytes += bytesInBuf;&lt;br /&gt;
    }&lt;br /&gt;
    return totalBytes;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when finished. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * JBoss DNA (http://www.jboss.org/dna)&lt;br /&gt;
 * See the COPYRIGHT.txt file distributed with this work for information&lt;br /&gt;
 * regarding copyright ownership.  Some portions may be licensed&lt;br /&gt;
 * to Red Hat, Inc. under one or more contributor license agreements.&lt;br /&gt;
 * See the AUTHORS.txt file in the distribution for a full listing of &lt;br /&gt;
 * individual contributors. &lt;br /&gt;
 *&lt;br /&gt;
 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA&lt;br /&gt;
 * is licensed to you under the terms of the GNU Lesser General Public License as&lt;br /&gt;
 * published by the Free Software Foundation; either version 2.1 of&lt;br /&gt;
 * the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * JBoss DNA is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU&lt;br /&gt;
 * Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
 * License along with this software; if not, write to the Free&lt;br /&gt;
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA&lt;br /&gt;
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
/**&lt;br /&gt;
 * @author Randall Hauch&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  /**&lt;br /&gt;
   * Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when&lt;br /&gt;
   * finished.&lt;br /&gt;
   * &lt;br /&gt;
   * @param content the content to write to the stream; may be null&lt;br /&gt;
   * @param stream the stream to which the content is to be written&lt;br /&gt;
   * @throws IOException&lt;br /&gt;
   * @throws IllegalArgumentException if the stream is null&lt;br /&gt;
   */&lt;br /&gt;
  public static void write( String content,&lt;br /&gt;
                            OutputStream stream ) throws IOException {&lt;br /&gt;
      boolean error = false;&lt;br /&gt;
      try {&lt;br /&gt;
          if (content != null) {&lt;br /&gt;
              byte[] bytes = content.getBytes();&lt;br /&gt;
              stream.write(bytes, 0, bytes.length);&lt;br /&gt;
          }&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
          error = true; // this error should be thrown, even if there is an error flushing/closing stream&lt;br /&gt;
          throw e;&lt;br /&gt;
      } catch (RuntimeException e) {&lt;br /&gt;
          error = true; // this error should be thrown, even if there is an error flushing/closing stream&lt;br /&gt;
          throw e;&lt;br /&gt;
      } finally {&lt;br /&gt;
          try {&lt;br /&gt;
              stream.flush();&lt;br /&gt;
          } catch (IOException e) {&lt;br /&gt;
              if (!error) throw e;&lt;br /&gt;
          } finally {&lt;br /&gt;
              try {&lt;br /&gt;
                  stream.close();&lt;br /&gt;
              } catch (IOException e) {&lt;br /&gt;
                  if (!error) throw e;&lt;br /&gt;
              }&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>