Fri, 14 Feb 2014 11:13:45 +0100
8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.util; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.io.ByteArrayInputStream; |
ohair@286 | 29 | import java.io.ByteArrayOutputStream; |
ohair@286 | 30 | import java.io.IOException; |
ohair@286 | 31 | import java.io.InputStream; |
ohair@286 | 32 | import java.io.OutputStream; |
ohair@286 | 33 | |
ohair@286 | 34 | /** |
ohair@286 | 35 | * Read/write buffer that stores a sequence of bytes. |
ohair@286 | 36 | * |
ohair@286 | 37 | * <p> |
ohair@286 | 38 | * It works in a way similar to {@link ByteArrayOutputStream} but |
ohair@286 | 39 | * this class works better in the following ways: |
ohair@286 | 40 | * |
ohair@286 | 41 | * <ol> |
ohair@286 | 42 | * <li>no synchronization |
ohair@286 | 43 | * <li>offers a {@link #newInputStream()} that creates a new {@link InputStream} |
ohair@286 | 44 | * that won't cause buffer reallocation. |
ohair@286 | 45 | * <li>less parameter correctness checking |
ohair@286 | 46 | * <li>offers a {@link #write(InputStream)} method that reads the entirety of the |
ohair@286 | 47 | * given {@link InputStream} without using a temporary buffer. |
ohair@286 | 48 | * </ol> |
ohair@286 | 49 | * |
ohair@286 | 50 | * @author Kohsuke Kawaguchi |
ohair@286 | 51 | */ |
ohair@286 | 52 | public class ByteArrayBuffer extends OutputStream { |
ohair@286 | 53 | /** |
ohair@286 | 54 | * The buffer where data is stored. |
ohair@286 | 55 | */ |
ohair@286 | 56 | protected byte[] buf; |
ohair@286 | 57 | |
ohair@286 | 58 | /** |
ohair@286 | 59 | * The number of valid bytes in the buffer. |
ohair@286 | 60 | */ |
ohair@286 | 61 | private int count; |
ohair@286 | 62 | |
ohair@286 | 63 | private static final int CHUNK_SIZE = 4096; |
ohair@286 | 64 | |
ohair@286 | 65 | /** |
ohair@286 | 66 | * Creates a new byte array output stream. The buffer capacity is |
ohair@286 | 67 | * initially 32 bytes, though its size increases if necessary. |
ohair@286 | 68 | */ |
ohair@286 | 69 | public ByteArrayBuffer() { |
ohair@286 | 70 | this(32); |
ohair@286 | 71 | } |
ohair@286 | 72 | |
ohair@286 | 73 | /** |
ohair@286 | 74 | * Creates a new byte array output stream, with a buffer capacity of |
ohair@286 | 75 | * the specified size, in bytes. |
ohair@286 | 76 | * |
ohair@286 | 77 | * @param size the initial size. |
ohair@286 | 78 | * @throws IllegalArgumentException if size is negative. |
ohair@286 | 79 | */ |
ohair@286 | 80 | public ByteArrayBuffer(int size) { |
ohair@286 | 81 | if (size <= 0) |
ohair@286 | 82 | throw new IllegalArgumentException(); |
ohair@286 | 83 | buf = new byte[size]; |
ohair@286 | 84 | } |
ohair@286 | 85 | |
ohair@286 | 86 | public ByteArrayBuffer(byte[] data) { |
ohair@286 | 87 | this(data,data.length); |
ohair@286 | 88 | } |
ohair@286 | 89 | |
ohair@286 | 90 | public ByteArrayBuffer(byte[] data, int length) { |
ohair@286 | 91 | this.buf = data; |
ohair@286 | 92 | this.count = length; |
ohair@286 | 93 | } |
ohair@286 | 94 | |
ohair@286 | 95 | /** |
ohair@286 | 96 | * Reads all the data of the given {@link InputStream} and appends them |
ohair@286 | 97 | * into this buffer. |
ohair@286 | 98 | * |
ohair@286 | 99 | * @throws IOException |
ohair@286 | 100 | * if the read operation fails with an {@link IOException}. |
ohair@286 | 101 | */ |
ohair@286 | 102 | public final void write(InputStream in) throws IOException { |
ohair@286 | 103 | while(true) { |
ohair@286 | 104 | int cap = buf.length-count; // the remaining buffer space |
ohair@286 | 105 | int sz = in.read(buf,count,cap); |
ohair@286 | 106 | if(sz<0) return; // hit EOS |
ohair@286 | 107 | count += sz; |
ohair@286 | 108 | |
ohair@286 | 109 | |
ohair@286 | 110 | if(cap==sz) |
ohair@286 | 111 | ensureCapacity(buf.length*2); // buffer filled up. |
ohair@286 | 112 | } |
ohair@286 | 113 | } |
ohair@286 | 114 | |
ohair@286 | 115 | public final void write(int b) { |
ohair@286 | 116 | int newcount = count + 1; |
ohair@286 | 117 | ensureCapacity(newcount); |
ohair@286 | 118 | buf[count] = (byte) b; |
ohair@286 | 119 | count = newcount; |
ohair@286 | 120 | } |
ohair@286 | 121 | |
ohair@286 | 122 | public final void write(byte b[], int off, int len) { |
ohair@286 | 123 | int newcount = count + len; |
ohair@286 | 124 | ensureCapacity(newcount); |
ohair@286 | 125 | System.arraycopy(b, off, buf, count, len); |
ohair@286 | 126 | count = newcount; |
ohair@286 | 127 | } |
ohair@286 | 128 | |
ohair@286 | 129 | private void ensureCapacity(int newcount) { |
ohair@286 | 130 | if (newcount > buf.length) { |
ohair@286 | 131 | byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)]; |
ohair@286 | 132 | System.arraycopy(buf, 0, newbuf, 0, count); |
ohair@286 | 133 | buf = newbuf; |
ohair@286 | 134 | } |
ohair@286 | 135 | } |
ohair@286 | 136 | |
ohair@286 | 137 | public final void writeTo(OutputStream out) throws IOException { |
ohair@286 | 138 | // Instead of writing out.write(buf, 0, count) |
ohair@286 | 139 | // Writing it in chunks that would help larger payloads |
ohair@286 | 140 | // Also if out is System.out on windows, it doesn't show on the console |
ohair@286 | 141 | // for larger data. |
ohair@286 | 142 | int remaining = count; |
ohair@286 | 143 | int off = 0; |
ohair@286 | 144 | while(remaining > 0) { |
ohair@286 | 145 | int chunk = (remaining > CHUNK_SIZE) ? CHUNK_SIZE : remaining; |
ohair@286 | 146 | out.write(buf, off, chunk); |
ohair@286 | 147 | remaining -= chunk; |
ohair@286 | 148 | off += chunk; |
ohair@286 | 149 | } |
ohair@286 | 150 | } |
ohair@286 | 151 | |
ohair@286 | 152 | public final void reset() { |
ohair@286 | 153 | count = 0; |
ohair@286 | 154 | } |
ohair@286 | 155 | |
ohair@286 | 156 | /** |
ohair@286 | 157 | * Gets the <b>copy</b> of exact-size byte[] that represents the written data. |
ohair@286 | 158 | * |
ohair@286 | 159 | * <p> |
ohair@286 | 160 | * Since this method needs to allocate a new byte[], this method will be costly. |
ohair@286 | 161 | * |
ohair@286 | 162 | * @deprecated |
ohair@286 | 163 | * this method causes a buffer reallocation. Use it only when |
ohair@286 | 164 | * you have to. |
ohair@286 | 165 | */ |
ohair@286 | 166 | public final byte[] toByteArray() { |
ohair@286 | 167 | byte newbuf[] = new byte[count]; |
ohair@286 | 168 | System.arraycopy(buf, 0, newbuf, 0, count); |
ohair@286 | 169 | return newbuf; |
ohair@286 | 170 | } |
ohair@286 | 171 | |
ohair@286 | 172 | public final int size() { |
ohair@286 | 173 | return count; |
ohair@286 | 174 | } |
ohair@286 | 175 | |
ohair@286 | 176 | /** |
ohair@286 | 177 | * Gets the underlying buffer that this {@link ByteArrayBuffer} uses. |
ohair@286 | 178 | * It's never small than its {@link #size()}. |
ohair@286 | 179 | * |
ohair@286 | 180 | * Use with caution. |
ohair@286 | 181 | */ |
ohair@286 | 182 | public final byte[] getRawData() { |
ohair@286 | 183 | return buf; |
ohair@286 | 184 | } |
ohair@286 | 185 | |
ohair@286 | 186 | public void close() throws IOException { |
ohair@286 | 187 | } |
ohair@286 | 188 | |
ohair@286 | 189 | /** |
ohair@286 | 190 | * Creates a new {@link InputStream} that reads from this buffer. |
ohair@286 | 191 | */ |
ohair@286 | 192 | public final InputStream newInputStream() { |
ohair@286 | 193 | return new ByteArrayInputStream(buf,0,count); |
ohair@286 | 194 | } |
ohair@286 | 195 | |
ohair@286 | 196 | /** |
ohair@286 | 197 | * Creates a new {@link InputStream} that reads a part of this bfufer. |
ohair@286 | 198 | */ |
ohair@286 | 199 | public final InputStream newInputStream(int start, int length) { |
ohair@286 | 200 | return new ByteArrayInputStream(buf,start,length); |
ohair@286 | 201 | } |
ohair@286 | 202 | |
ohair@286 | 203 | /** |
ohair@286 | 204 | * Decodes the contents of this buffer by the default encoding |
ohair@286 | 205 | * and returns it as a string. |
ohair@286 | 206 | * |
ohair@286 | 207 | * <p> |
ohair@286 | 208 | * Meant to aid debugging, but no more. |
ohair@286 | 209 | */ |
ohair@286 | 210 | public String toString() { |
ohair@286 | 211 | return new String(buf, 0, count); |
ohair@286 | 212 | } |
ohair@286 | 213 | } |