Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/StreamBuffer.java
41162 views
/*1* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25*/2627package bench.serial;2829import java.io.InputStream;30import java.io.OutputStream;31import java.io.IOException;3233/**34* The StreamBuffer class provides a space that can be written to with an35* OutputStream and read from with an InputStream. It is similar to36* PipedInput/OutputStream except that it is unsynchronized and more37* lightweight. StreamBuffers are used inside of the serialization benchmarks38* in order to minimize the overhead incurred by reading and writing to/from the39* underlying stream (using ByteArrayInput/OutputStreams results in allocation40* of a new byte array with each cycle, while using PipedInput/OutputStreams41* involves threading and synchronization).42* <p>43* Writes/reads to and from a StreamBuffer must occur in distinct phases; reads44* from a StreamBuffer effectively close the StreamBuffer output stream. These45* semantics are necessary to avoid using wait/notify in46* StreamBufferInputStream.read().47*/48public class StreamBuffer {4950/**51* Output stream for writing to stream buffer.52*/53private class StreamBufferOutputStream extends OutputStream {5455private int pos;5657public void write(int b) throws IOException {58if (mode != WRITE_MODE)59throw new IOException();60while (pos >= buf.length)61grow();62buf[pos++] = (byte) b;63}6465public void write(byte[] b, int off, int len) throws IOException {66if (mode != WRITE_MODE)67throw new IOException();68while (pos + len > buf.length)69grow();70System.arraycopy(b, off, buf, pos, len);71pos += len;72}7374public void close() throws IOException {75if (mode != WRITE_MODE)76throw new IOException();77mode = READ_MODE;78}79}8081/**82* Input stream for reading from stream buffer.83*/84private class StreamBufferInputStream extends InputStream {8586private int pos;8788public int read() throws IOException {89if (mode == CLOSED_MODE)90throw new IOException();91mode = READ_MODE;92return (pos < out.pos) ? (buf[pos++] & 0xFF) : -1;93}9495public int read(byte[] b, int off, int len) throws IOException {96if (mode == CLOSED_MODE)97throw new IOException();98mode = READ_MODE;99int avail = out.pos - pos;100int rlen = (avail < len) ? avail : len;101System.arraycopy(buf, pos, b, off, rlen);102pos += rlen;103return rlen;104}105106public long skip(long len) throws IOException {107if (mode == CLOSED_MODE)108throw new IOException();109mode = READ_MODE;110int avail = out.pos - pos;111long slen = (avail < len) ? avail : len;112pos += slen;113return slen;114}115116public int available() throws IOException {117if (mode == CLOSED_MODE)118throw new IOException();119mode = READ_MODE;120return out.pos - pos;121}122123public void close() throws IOException {124if (mode == CLOSED_MODE)125throw new IOException();126mode = CLOSED_MODE;127}128}129130private static final int START_BUFSIZE = 256;131private static final int GROW_FACTOR = 2;132private static final int CLOSED_MODE = 0;133private static final int WRITE_MODE = 1;134private static final int READ_MODE = 2;135136private byte[] buf;137private StreamBufferOutputStream out = new StreamBufferOutputStream();138private StreamBufferInputStream in = new StreamBufferInputStream();139private int mode = WRITE_MODE;140141public StreamBuffer() {142this(START_BUFSIZE);143}144145public StreamBuffer(int size) {146buf = new byte[size];147}148149public OutputStream getOutputStream() {150return out;151}152153public InputStream getInputStream() {154return in;155}156157public void reset() {158in.pos = out.pos = 0;159mode = WRITE_MODE;160}161162private void grow() {163byte[] newbuf = new byte[buf.length * GROW_FACTOR];164System.arraycopy(buf, 0, newbuf, 0, buf.length);165buf = newbuf;166}167}168169170