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.messaging.saaj.util;
325N/A
325N/Aimport java.io.BufferedOutputStream;
325N/Aimport java.io.IOException;
325N/Aimport java.io.OutputStream;
325N/Aimport java.io.InputStream;
325N/Aimport java.io.ByteArrayInputStream;
325N/A
325N/A/**
325N/A * Customized {@link BufferedOutputStream}.
325N/A *
325N/A * <p>
325N/A * Compared to {@link BufferedOutputStream},
325N/A * this class:
325N/A *
325N/A * <ol>
325N/A * <li>doesn't do synchronization
325N/A * <li>allows access to the raw buffer
325N/A * <li>almost no parameter check
325N/A */
325N/Apublic final class ByteOutputStream 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 protected int count = 0;
325N/A
325N/A public ByteOutputStream() {
325N/A this(1024);
325N/A }
325N/A
325N/A public ByteOutputStream(int size) {
325N/A buf = new byte[size];
325N/A }
325N/A
325N/A /**
325N/A * Copies all the bytes from this input into this buffer.
325N/A */
325N/A public void write(InputStream in) throws IOException {
325N/A if (in instanceof ByteArrayInputStream) {
325N/A int size = in.available();
325N/A ensureCapacity(size);
325N/A count += in.read(buf,count,size);
325N/A return;
325N/A }
325N/A while(true) {
325N/A int cap = buf.length-count;
325N/A int sz = in.read(buf,count,cap);
325N/A if(sz<0) return; // hit EOS
325N/A
325N/A count += sz;
325N/A if(cap==sz)
325N/A // the buffer filled up. double the buffer
325N/A ensureCapacity(count);
325N/A }
325N/A }
325N/A
325N/A public void write(int b) {
325N/A ensureCapacity(1);
325N/A buf[count] = (byte) b;
325N/A count++;
325N/A }
325N/A
325N/A /**
325N/A * Ensure that the buffer has at least this much space.
325N/A */
325N/A private void ensureCapacity(int space) {
325N/A int newcount = space + count;
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 void write(byte[] b, int off, int len) {
325N/A ensureCapacity(len);
325N/A System.arraycopy(b, off, buf, count, len);
325N/A count += len;
325N/A }
325N/A
325N/A public void write(byte[] b) {
325N/A write(b, 0, b.length);
325N/A }
325N/A
325N/A /**
325N/A * Writes a string as ASCII string.
325N/A */
325N/A public void writeAsAscii(String s) {
325N/A int len = s.length();
325N/A
325N/A ensureCapacity(len);
325N/A
325N/A int ptr = count;
325N/A for( int i=0; i<len; i++ )
325N/A buf[ptr++] = (byte)s.charAt(i);
325N/A count = ptr;
325N/A }
325N/A
325N/A public void writeTo(OutputStream out) throws IOException {
325N/A out.write(buf, 0, count);
325N/A }
325N/A
325N/A public void reset() {
325N/A count = 0;
325N/A }
325N/A
325N/A /**
325N/A * Evil buffer reallocation method.
325N/A * Don't use it unless you absolutely have to.
325N/A *
325N/A * @deprecated
325N/A * because this is evil!
325N/A */
325N/A public 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 int size() {
325N/A return count;
325N/A }
325N/A
325N/A public ByteInputStream newInputStream() {
325N/A return new ByteInputStream(buf,count);
325N/A }
325N/A
325N/A /**
325N/A * Converts the buffer's contents into a string, translating bytes into
325N/A * characters according to the platform's default character encoding.
325N/A *
325N/A * @return String translated from the buffer's contents.
325N/A * @since JDK1.1
325N/A */
325N/A public String toString() {
325N/A return new String(buf, 0, count);
325N/A }
325N/A
325N/A public void close() {
325N/A }
325N/A
325N/A public byte[] getBytes() {
325N/A return buf;
325N/A }
325N/A
325N/A
325N/A public int getCount() {
325N/A return count;
325N/A }
325N/A}