1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/ByteArrayBuffer.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,213 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.util; 1.30 + 1.31 +import java.io.ByteArrayInputStream; 1.32 +import java.io.ByteArrayOutputStream; 1.33 +import java.io.IOException; 1.34 +import java.io.InputStream; 1.35 +import java.io.OutputStream; 1.36 + 1.37 +/** 1.38 + * Read/write buffer that stores a sequence of bytes. 1.39 + * 1.40 + * <p> 1.41 + * It works in a way similar to {@link ByteArrayOutputStream} but 1.42 + * this class works better in the following ways: 1.43 + * 1.44 + * <ol> 1.45 + * <li>no synchronization 1.46 + * <li>offers a {@link #newInputStream()} that creates a new {@link InputStream} 1.47 + * that won't cause buffer reallocation. 1.48 + * <li>less parameter correctness checking 1.49 + * <li>offers a {@link #write(InputStream)} method that reads the entirety of the 1.50 + * given {@link InputStream} without using a temporary buffer. 1.51 + * </ol> 1.52 + * 1.53 + * @author Kohsuke Kawaguchi 1.54 + */ 1.55 +public class ByteArrayBuffer extends OutputStream { 1.56 + /** 1.57 + * The buffer where data is stored. 1.58 + */ 1.59 + protected byte[] buf; 1.60 + 1.61 + /** 1.62 + * The number of valid bytes in the buffer. 1.63 + */ 1.64 + private int count; 1.65 + 1.66 + private static final int CHUNK_SIZE = 4096; 1.67 + 1.68 + /** 1.69 + * Creates a new byte array output stream. The buffer capacity is 1.70 + * initially 32 bytes, though its size increases if necessary. 1.71 + */ 1.72 + public ByteArrayBuffer() { 1.73 + this(32); 1.74 + } 1.75 + 1.76 + /** 1.77 + * Creates a new byte array output stream, with a buffer capacity of 1.78 + * the specified size, in bytes. 1.79 + * 1.80 + * @param size the initial size. 1.81 + * @throws IllegalArgumentException if size is negative. 1.82 + */ 1.83 + public ByteArrayBuffer(int size) { 1.84 + if (size <= 0) 1.85 + throw new IllegalArgumentException(); 1.86 + buf = new byte[size]; 1.87 + } 1.88 + 1.89 + public ByteArrayBuffer(byte[] data) { 1.90 + this(data,data.length); 1.91 + } 1.92 + 1.93 + public ByteArrayBuffer(byte[] data, int length) { 1.94 + this.buf = data; 1.95 + this.count = length; 1.96 + } 1.97 + 1.98 + /** 1.99 + * Reads all the data of the given {@link InputStream} and appends them 1.100 + * into this buffer. 1.101 + * 1.102 + * @throws IOException 1.103 + * if the read operation fails with an {@link IOException}. 1.104 + */ 1.105 + public final void write(InputStream in) throws IOException { 1.106 + while(true) { 1.107 + int cap = buf.length-count; // the remaining buffer space 1.108 + int sz = in.read(buf,count,cap); 1.109 + if(sz<0) return; // hit EOS 1.110 + count += sz; 1.111 + 1.112 + 1.113 + if(cap==sz) 1.114 + ensureCapacity(buf.length*2); // buffer filled up. 1.115 + } 1.116 + } 1.117 + 1.118 + public final void write(int b) { 1.119 + int newcount = count + 1; 1.120 + ensureCapacity(newcount); 1.121 + buf[count] = (byte) b; 1.122 + count = newcount; 1.123 + } 1.124 + 1.125 + public final void write(byte b[], int off, int len) { 1.126 + int newcount = count + len; 1.127 + ensureCapacity(newcount); 1.128 + System.arraycopy(b, off, buf, count, len); 1.129 + count = newcount; 1.130 + } 1.131 + 1.132 + private void ensureCapacity(int newcount) { 1.133 + if (newcount > buf.length) { 1.134 + byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)]; 1.135 + System.arraycopy(buf, 0, newbuf, 0, count); 1.136 + buf = newbuf; 1.137 + } 1.138 + } 1.139 + 1.140 + public final void writeTo(OutputStream out) throws IOException { 1.141 + // Instead of writing out.write(buf, 0, count) 1.142 + // Writing it in chunks that would help larger payloads 1.143 + // Also if out is System.out on windows, it doesn't show on the console 1.144 + // for larger data. 1.145 + int remaining = count; 1.146 + int off = 0; 1.147 + while(remaining > 0) { 1.148 + int chunk = (remaining > CHUNK_SIZE) ? CHUNK_SIZE : remaining; 1.149 + out.write(buf, off, chunk); 1.150 + remaining -= chunk; 1.151 + off += chunk; 1.152 + } 1.153 + } 1.154 + 1.155 + public final void reset() { 1.156 + count = 0; 1.157 + } 1.158 + 1.159 + /** 1.160 + * Gets the <b>copy</b> of exact-size byte[] that represents the written data. 1.161 + * 1.162 + * <p> 1.163 + * Since this method needs to allocate a new byte[], this method will be costly. 1.164 + * 1.165 + * @deprecated 1.166 + * this method causes a buffer reallocation. Use it only when 1.167 + * you have to. 1.168 + */ 1.169 + public final byte[] toByteArray() { 1.170 + byte newbuf[] = new byte[count]; 1.171 + System.arraycopy(buf, 0, newbuf, 0, count); 1.172 + return newbuf; 1.173 + } 1.174 + 1.175 + public final int size() { 1.176 + return count; 1.177 + } 1.178 + 1.179 + /** 1.180 + * Gets the underlying buffer that this {@link ByteArrayBuffer} uses. 1.181 + * It's never small than its {@link #size()}. 1.182 + * 1.183 + * Use with caution. 1.184 + */ 1.185 + public final byte[] getRawData() { 1.186 + return buf; 1.187 + } 1.188 + 1.189 + public void close() throws IOException { 1.190 + } 1.191 + 1.192 + /** 1.193 + * Creates a new {@link InputStream} that reads from this buffer. 1.194 + */ 1.195 + public final InputStream newInputStream() { 1.196 + return new ByteArrayInputStream(buf,0,count); 1.197 + } 1.198 + 1.199 + /** 1.200 + * Creates a new {@link InputStream} that reads a part of this bfufer. 1.201 + */ 1.202 + public final InputStream newInputStream(int start, int length) { 1.203 + return new ByteArrayInputStream(buf,start,length); 1.204 + } 1.205 + 1.206 + /** 1.207 + * Decodes the contents of this buffer by the default encoding 1.208 + * and returns it as a string. 1.209 + * 1.210 + * <p> 1.211 + * Meant to aid debugging, but no more. 1.212 + */ 1.213 + public String toString() { 1.214 + return new String(buf, 0, count); 1.215 + } 1.216 +}