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