Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/rmi/reliability/benchmark/bench/serial/StreamBuffer.java
41162 views
1
/*
2
* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
*
26
*/
27
28
package bench.serial;
29
30
import java.io.InputStream;
31
import java.io.OutputStream;
32
import java.io.IOException;
33
34
/**
35
* The StreamBuffer class provides a space that can be written to with an
36
* OutputStream and read from with an InputStream. It is similar to
37
* PipedInput/OutputStream except that it is unsynchronized and more
38
* lightweight. StreamBuffers are used inside of the serialization benchmarks
39
* in order to minimize the overhead incurred by reading and writing to/from the
40
* underlying stream (using ByteArrayInput/OutputStreams results in allocation
41
* of a new byte array with each cycle, while using PipedInput/OutputStreams
42
* involves threading and synchronization).
43
* <p>
44
* Writes/reads to and from a StreamBuffer must occur in distinct phases; reads
45
* from a StreamBuffer effectively close the StreamBuffer output stream. These
46
* semantics are necessary to avoid using wait/notify in
47
* StreamBufferInputStream.read().
48
*/
49
public class StreamBuffer {
50
51
/**
52
* Output stream for writing to stream buffer.
53
*/
54
private class StreamBufferOutputStream extends OutputStream {
55
56
private int pos;
57
58
public void write(int b) throws IOException {
59
if (mode != WRITE_MODE)
60
throw new IOException();
61
while (pos >= buf.length)
62
grow();
63
buf[pos++] = (byte) b;
64
}
65
66
public void write(byte[] b, int off, int len) throws IOException {
67
if (mode != WRITE_MODE)
68
throw new IOException();
69
while (pos + len > buf.length)
70
grow();
71
System.arraycopy(b, off, buf, pos, len);
72
pos += len;
73
}
74
75
public void close() throws IOException {
76
if (mode != WRITE_MODE)
77
throw new IOException();
78
mode = READ_MODE;
79
}
80
}
81
82
/**
83
* Input stream for reading from stream buffer.
84
*/
85
private class StreamBufferInputStream extends InputStream {
86
87
private int pos;
88
89
public int read() throws IOException {
90
if (mode == CLOSED_MODE)
91
throw new IOException();
92
mode = READ_MODE;
93
return (pos < out.pos) ? (buf[pos++] & 0xFF) : -1;
94
}
95
96
public int read(byte[] b, int off, int len) throws IOException {
97
if (mode == CLOSED_MODE)
98
throw new IOException();
99
mode = READ_MODE;
100
int avail = out.pos - pos;
101
int rlen = (avail < len) ? avail : len;
102
System.arraycopy(buf, pos, b, off, rlen);
103
pos += rlen;
104
return rlen;
105
}
106
107
public long skip(long len) throws IOException {
108
if (mode == CLOSED_MODE)
109
throw new IOException();
110
mode = READ_MODE;
111
int avail = out.pos - pos;
112
long slen = (avail < len) ? avail : len;
113
pos += slen;
114
return slen;
115
}
116
117
public int available() throws IOException {
118
if (mode == CLOSED_MODE)
119
throw new IOException();
120
mode = READ_MODE;
121
return out.pos - pos;
122
}
123
124
public void close() throws IOException {
125
if (mode == CLOSED_MODE)
126
throw new IOException();
127
mode = CLOSED_MODE;
128
}
129
}
130
131
private static final int START_BUFSIZE = 256;
132
private static final int GROW_FACTOR = 2;
133
private static final int CLOSED_MODE = 0;
134
private static final int WRITE_MODE = 1;
135
private static final int READ_MODE = 2;
136
137
private byte[] buf;
138
private StreamBufferOutputStream out = new StreamBufferOutputStream();
139
private StreamBufferInputStream in = new StreamBufferInputStream();
140
private int mode = WRITE_MODE;
141
142
public StreamBuffer() {
143
this(START_BUFSIZE);
144
}
145
146
public StreamBuffer(int size) {
147
buf = new byte[size];
148
}
149
150
public OutputStream getOutputStream() {
151
return out;
152
}
153
154
public InputStream getInputStream() {
155
return in;
156
}
157
158
public void reset() {
159
in.pos = out.pos = 0;
160
mode = WRITE_MODE;
161
}
162
163
private void grow() {
164
byte[] newbuf = new byte[buf.length * GROW_FACTOR];
165
System.arraycopy(buf, 0, newbuf, 0, buf.length);
166
buf = newbuf;
167
}
168
}
169
170