Thu, 31 Aug 2017 15:18:52 +0800
merge
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.messaging.saaj.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.BufferedOutputStream; |
aoqi@0 | 29 | import java.io.IOException; |
aoqi@0 | 30 | import java.io.OutputStream; |
aoqi@0 | 31 | import java.io.InputStream; |
aoqi@0 | 32 | import java.io.ByteArrayInputStream; |
aoqi@0 | 33 | |
aoqi@0 | 34 | /** |
aoqi@0 | 35 | * Customized {@link BufferedOutputStream}. |
aoqi@0 | 36 | * |
aoqi@0 | 37 | * <p> |
aoqi@0 | 38 | * Compared to {@link BufferedOutputStream}, |
aoqi@0 | 39 | * this class: |
aoqi@0 | 40 | * |
aoqi@0 | 41 | * <ol> |
aoqi@0 | 42 | * <li>doesn't do synchronization |
aoqi@0 | 43 | * <li>allows access to the raw buffer |
aoqi@0 | 44 | * <li>almost no parameter check |
aoqi@0 | 45 | */ |
aoqi@0 | 46 | public final class ByteOutputStream extends OutputStream { |
aoqi@0 | 47 | /** |
aoqi@0 | 48 | * The buffer where data is stored. |
aoqi@0 | 49 | */ |
aoqi@0 | 50 | protected byte[] buf; |
aoqi@0 | 51 | |
aoqi@0 | 52 | /** |
aoqi@0 | 53 | * The number of valid bytes in the buffer. |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | protected int count = 0; |
aoqi@0 | 56 | |
aoqi@0 | 57 | public ByteOutputStream() { |
aoqi@0 | 58 | this(1024); |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | public ByteOutputStream(int size) { |
aoqi@0 | 62 | buf = new byte[size]; |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | /** |
aoqi@0 | 66 | * Copies all the bytes from this input into this buffer. |
aoqi@0 | 67 | */ |
aoqi@0 | 68 | public void write(InputStream in) throws IOException { |
aoqi@0 | 69 | if (in instanceof ByteArrayInputStream) { |
aoqi@0 | 70 | int size = in.available(); |
aoqi@0 | 71 | ensureCapacity(size); |
aoqi@0 | 72 | count += in.read(buf,count,size); |
aoqi@0 | 73 | return; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | while(true) { |
aoqi@0 | 76 | int cap = buf.length-count; |
aoqi@0 | 77 | int sz = in.read(buf,count,cap); |
aoqi@0 | 78 | if(sz<0) return; // hit EOS |
aoqi@0 | 79 | |
aoqi@0 | 80 | count += sz; |
aoqi@0 | 81 | if(cap==sz) |
aoqi@0 | 82 | // the buffer filled up. double the buffer |
aoqi@0 | 83 | ensureCapacity(count); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | public void write(int b) { |
aoqi@0 | 88 | ensureCapacity(1); |
aoqi@0 | 89 | buf[count] = (byte) b; |
aoqi@0 | 90 | count++; |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | /** |
aoqi@0 | 94 | * Ensure that the buffer has at least this much space. |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | private void ensureCapacity(int space) { |
aoqi@0 | 97 | int newcount = space + count; |
aoqi@0 | 98 | if (newcount > buf.length) { |
aoqi@0 | 99 | byte[] newbuf = new byte[Math.max(buf.length << 1, newcount)]; |
aoqi@0 | 100 | System.arraycopy(buf, 0, newbuf, 0, count); |
aoqi@0 | 101 | buf = newbuf; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | public void write(byte[] b, int off, int len) { |
aoqi@0 | 106 | ensureCapacity(len); |
aoqi@0 | 107 | System.arraycopy(b, off, buf, count, len); |
aoqi@0 | 108 | count += len; |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | public void write(byte[] b) { |
aoqi@0 | 112 | write(b, 0, b.length); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | /** |
aoqi@0 | 116 | * Writes a string as ASCII string. |
aoqi@0 | 117 | */ |
aoqi@0 | 118 | public void writeAsAscii(String s) { |
aoqi@0 | 119 | int len = s.length(); |
aoqi@0 | 120 | |
aoqi@0 | 121 | ensureCapacity(len); |
aoqi@0 | 122 | |
aoqi@0 | 123 | int ptr = count; |
aoqi@0 | 124 | for( int i=0; i<len; i++ ) |
aoqi@0 | 125 | buf[ptr++] = (byte)s.charAt(i); |
aoqi@0 | 126 | count = ptr; |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | public void writeTo(OutputStream out) throws IOException { |
aoqi@0 | 130 | out.write(buf, 0, count); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | public void reset() { |
aoqi@0 | 134 | count = 0; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | /** |
aoqi@0 | 138 | * Evil buffer reallocation method. |
aoqi@0 | 139 | * Don't use it unless you absolutely have to. |
aoqi@0 | 140 | * |
aoqi@0 | 141 | * @deprecated |
aoqi@0 | 142 | * because this is evil! |
aoqi@0 | 143 | */ |
aoqi@0 | 144 | public byte toByteArray()[] { |
aoqi@0 | 145 | byte[] newbuf = new byte[count]; |
aoqi@0 | 146 | System.arraycopy(buf, 0, newbuf, 0, count); |
aoqi@0 | 147 | return newbuf; |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | public int size() { |
aoqi@0 | 151 | return count; |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | public ByteInputStream newInputStream() { |
aoqi@0 | 155 | return new ByteInputStream(buf,count); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | /** |
aoqi@0 | 159 | * Converts the buffer's contents into a string, translating bytes into |
aoqi@0 | 160 | * characters according to the platform's default character encoding. |
aoqi@0 | 161 | * |
aoqi@0 | 162 | * @return String translated from the buffer's contents. |
aoqi@0 | 163 | * @since JDK1.1 |
aoqi@0 | 164 | */ |
aoqi@0 | 165 | public String toString() { |
aoqi@0 | 166 | return new String(buf, 0, count); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | |
aoqi@0 | 169 | public void close() { |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | public byte[] getBytes() { |
aoqi@0 | 173 | return buf; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | |
aoqi@0 | 177 | public int getCount() { |
aoqi@0 | 178 | return count; |
aoqi@0 | 179 | } |
aoqi@0 | 180 | } |