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