aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.xml.internal.ws.util;
aoqi@0:
aoqi@0: import java.io.ByteArrayInputStream;
aoqi@0: import java.io.ByteArrayOutputStream;
aoqi@0: import java.io.IOException;
aoqi@0: import java.io.InputStream;
aoqi@0: import java.io.OutputStream;
aoqi@0:
aoqi@0: /**
aoqi@0: * Read/write buffer that stores a sequence of bytes.
aoqi@0: *
aoqi@0: *
aoqi@0: * It works in a way similar to {@link ByteArrayOutputStream} but
aoqi@0: * this class works better in the following ways:
aoqi@0: *
aoqi@0: *
aoqi@0: * - no synchronization
aoqi@0: *
- offers a {@link #newInputStream()} that creates a new {@link InputStream}
aoqi@0: * that won't cause buffer reallocation.
aoqi@0: *
- less parameter correctness checking
aoqi@0: *
- offers a {@link #write(InputStream)} method that reads the entirety of the
aoqi@0: * given {@link InputStream} without using a temporary buffer.
aoqi@0: *
aoqi@0: *
aoqi@0: * @author Kohsuke Kawaguchi
aoqi@0: */
aoqi@0: public class ByteArrayBuffer extends OutputStream {
aoqi@0: /**
aoqi@0: * The buffer where data is stored.
aoqi@0: */
aoqi@0: protected byte[] buf;
aoqi@0:
aoqi@0: /**
aoqi@0: * The number of valid bytes in the buffer.
aoqi@0: */
aoqi@0: private int count;
aoqi@0:
aoqi@0: private static final int CHUNK_SIZE = 4096;
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates a new byte array output stream. The buffer capacity is
aoqi@0: * initially 32 bytes, though its size increases if necessary.
aoqi@0: */
aoqi@0: public ByteArrayBuffer() {
aoqi@0: this(32);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates a new byte array output stream, with a buffer capacity of
aoqi@0: * the specified size, in bytes.
aoqi@0: *
aoqi@0: * @param size the initial size.
aoqi@0: * @throws IllegalArgumentException if size is negative.
aoqi@0: */
aoqi@0: public ByteArrayBuffer(int size) {
aoqi@0: if (size <= 0)
aoqi@0: throw new IllegalArgumentException();
aoqi@0: buf = new byte[size];
aoqi@0: }
aoqi@0:
aoqi@0: public ByteArrayBuffer(byte[] data) {
aoqi@0: this(data,data.length);
aoqi@0: }
aoqi@0:
aoqi@0: public ByteArrayBuffer(byte[] data, int length) {
aoqi@0: this.buf = data;
aoqi@0: this.count = length;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Reads all the data of the given {@link InputStream} and appends them
aoqi@0: * into this buffer.
aoqi@0: *
aoqi@0: * @throws IOException
aoqi@0: * if the read operation fails with an {@link IOException}.
aoqi@0: */
aoqi@0: public final void write(InputStream in) throws IOException {
aoqi@0: while(true) {
aoqi@0: int cap = buf.length-count; // the remaining buffer space
aoqi@0: int sz = in.read(buf,count,cap);
aoqi@0: if(sz<0) return; // hit EOS
aoqi@0: count += sz;
aoqi@0:
aoqi@0:
aoqi@0: if(cap==sz)
aoqi@0: ensureCapacity(buf.length*2); // buffer filled up.
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public final void write(int b) {
aoqi@0: int newcount = count + 1;
aoqi@0: ensureCapacity(newcount);
aoqi@0: buf[count] = (byte) b;
aoqi@0: count = newcount;
aoqi@0: }
aoqi@0:
aoqi@0: public final void write(byte b[], int off, int len) {
aoqi@0: int newcount = count + len;
aoqi@0: ensureCapacity(newcount);
aoqi@0: System.arraycopy(b, off, buf, count, len);
aoqi@0: count = newcount;
aoqi@0: }
aoqi@0:
aoqi@0: private void ensureCapacity(int newcount) {
aoqi@0: if (newcount > buf.length) {
aoqi@0: byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)];
aoqi@0: System.arraycopy(buf, 0, newbuf, 0, count);
aoqi@0: buf = newbuf;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public final void writeTo(OutputStream out) throws IOException {
aoqi@0: // Instead of writing out.write(buf, 0, count)
aoqi@0: // Writing it in chunks that would help larger payloads
aoqi@0: // Also if out is System.out on windows, it doesn't show on the console
aoqi@0: // for larger data.
aoqi@0: int remaining = count;
aoqi@0: int off = 0;
aoqi@0: while(remaining > 0) {
aoqi@0: int chunk = (remaining > CHUNK_SIZE) ? CHUNK_SIZE : remaining;
aoqi@0: out.write(buf, off, chunk);
aoqi@0: remaining -= chunk;
aoqi@0: off += chunk;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public final void reset() {
aoqi@0: count = 0;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Gets the copy of exact-size byte[] that represents the written data.
aoqi@0: *
aoqi@0: *
aoqi@0: * Since this method needs to allocate a new byte[], this method will be costly.
aoqi@0: *
aoqi@0: * @deprecated
aoqi@0: * this method causes a buffer reallocation. Use it only when
aoqi@0: * you have to.
aoqi@0: */
aoqi@0: public final byte[] toByteArray() {
aoqi@0: byte newbuf[] = new byte[count];
aoqi@0: System.arraycopy(buf, 0, newbuf, 0, count);
aoqi@0: return newbuf;
aoqi@0: }
aoqi@0:
aoqi@0: public final int size() {
aoqi@0: return count;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Gets the underlying buffer that this {@link ByteArrayBuffer} uses.
aoqi@0: * It's never small than its {@link #size()}.
aoqi@0: *
aoqi@0: * Use with caution.
aoqi@0: */
aoqi@0: public final byte[] getRawData() {
aoqi@0: return buf;
aoqi@0: }
aoqi@0:
aoqi@0: public void close() throws IOException {
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates a new {@link InputStream} that reads from this buffer.
aoqi@0: */
aoqi@0: public final InputStream newInputStream() {
aoqi@0: return new ByteArrayInputStream(buf,0,count);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates a new {@link InputStream} that reads a part of this bfufer.
aoqi@0: */
aoqi@0: public final InputStream newInputStream(int start, int length) {
aoqi@0: return new ByteArrayInputStream(buf,start,length);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Decodes the contents of this buffer by the default encoding
aoqi@0: * and returns it as a string.
aoqi@0: *
aoqi@0: *
aoqi@0: * Meant to aid debugging, but no more.
aoqi@0: */
aoqi@0: public String toString() {
aoqi@0: return new String(buf, 0, count);
aoqi@0: }
aoqi@0: }