src/share/jaxws_classes/com/sun/xml/internal/ws/util/ByteArrayBuffer.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

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

mercurial