169N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A */
0N/A
0N/Apackage bench.serial;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * The StreamBuffer class provides a space that can be written to with an
0N/A * OutputStream and read from with an InputStream. It is similar to
0N/A * PipedInput/OutputStream except that it is unsynchronized and more
0N/A * lightweight. StreamBuffers are used inside of the serialization benchmarks
0N/A * in order to minimize the overhead incurred by reading and writing to/from the
0N/A * underlying stream (using ByteArrayInput/OutputStreams results in allocation
0N/A * of a new byte array with each cycle, while using PipedInput/OutputStreams
0N/A * involves threading and synchronization).
0N/A * <p>
0N/A * Writes/reads to and from a StreamBuffer must occur in distinct phases; reads
0N/A * from a StreamBuffer effectively close the StreamBuffer output stream. These
0N/A * semantics are necessary to avoid using wait/notify in
0N/A * StreamBufferInputStream.read().
0N/A */
0N/Apublic class StreamBuffer {
169N/A
0N/A /**
0N/A * Output stream for writing to stream buffer.
0N/A */
0N/A private class StreamBufferOutputStream extends OutputStream {
169N/A
169N/A private int pos;
169N/A
169N/A public void write(int b) throws IOException {
169N/A if (mode != WRITE_MODE)
169N/A throw new IOException();
169N/A while (pos >= buf.length)
169N/A grow();
169N/A buf[pos++] = (byte) b;
169N/A }
0N/A
169N/A public void write(byte[] b, int off, int len) throws IOException {
169N/A if (mode != WRITE_MODE)
169N/A throw new IOException();
169N/A while (pos + len > buf.length)
169N/A grow();
169N/A System.arraycopy(b, off, buf, pos, len);
169N/A pos += len;
169N/A }
169N/A
169N/A public void close() throws IOException {
169N/A if (mode != WRITE_MODE)
169N/A throw new IOException();
169N/A mode = READ_MODE;
169N/A }
0N/A }
169N/A
0N/A /**
0N/A * Input stream for reading from stream buffer.
0N/A */
0N/A private class StreamBufferInputStream extends InputStream {
169N/A
169N/A private int pos;
169N/A
169N/A public int read() throws IOException {
169N/A if (mode == CLOSED_MODE)
169N/A throw new IOException();
169N/A mode = READ_MODE;
169N/A return (pos < out.pos) ? (buf[pos++] & 0xFF) : -1;
169N/A }
169N/A
169N/A public int read(byte[] b, int off, int len) throws IOException {
169N/A if (mode == CLOSED_MODE)
169N/A throw new IOException();
169N/A mode = READ_MODE;
169N/A int avail = out.pos - pos;
169N/A int rlen = (avail < len) ? avail : len;
169N/A System.arraycopy(buf, pos, b, off, rlen);
169N/A pos += rlen;
169N/A return rlen;
169N/A }
0N/A
169N/A public long skip(long len) throws IOException {
169N/A if (mode == CLOSED_MODE)
169N/A throw new IOException();
169N/A mode = READ_MODE;
169N/A int avail = out.pos - pos;
169N/A long slen = (avail < len) ? avail : len;
169N/A pos += slen;
169N/A return slen;
169N/A }
0N/A
169N/A public int available() throws IOException {
169N/A if (mode == CLOSED_MODE)
169N/A throw new IOException();
169N/A mode = READ_MODE;
169N/A return out.pos - pos;
169N/A }
169N/A
169N/A public void close() throws IOException {
169N/A if (mode == CLOSED_MODE)
169N/A throw new IOException();
169N/A mode = CLOSED_MODE;
169N/A }
0N/A }
0N/A
0N/A private static final int START_BUFSIZE = 256;
0N/A private static final int GROW_FACTOR = 2;
0N/A private static final int CLOSED_MODE = 0;
0N/A private static final int WRITE_MODE = 1;
0N/A private static final int READ_MODE = 2;
0N/A
0N/A private byte[] buf;
0N/A private StreamBufferOutputStream out = new StreamBufferOutputStream();
0N/A private StreamBufferInputStream in = new StreamBufferInputStream();
0N/A private int mode = WRITE_MODE;
0N/A
0N/A public StreamBuffer() {
169N/A this(START_BUFSIZE);
0N/A }
169N/A
0N/A public StreamBuffer(int size) {
169N/A buf = new byte[size];
0N/A }
169N/A
0N/A public OutputStream getOutputStream() {
169N/A return out;
0N/A }
169N/A
0N/A public InputStream getInputStream() {
169N/A return in;
0N/A }
169N/A
0N/A public void reset() {
169N/A in.pos = out.pos = 0;
169N/A mode = WRITE_MODE;
0N/A }
169N/A
0N/A private void grow() {
169N/A byte[] newbuf = new byte[buf.length * GROW_FACTOR];
169N/A System.arraycopy(buf, 0, newbuf, 0, buf.length);
169N/A buf = newbuf;
0N/A }
0N/A}