1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.examples.stress;
18
19 import org.apache.commons.rng.UniformRandomProvider;
20
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.nio.ByteOrder;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 abstract class RngDataOutput implements Closeable {
59
60 protected final byte[] buffer;
61
62
63 private final OutputStream out;
64
65
66
67
68
69
70
71 private static class BIntRngDataOutput extends RngDataOutput {
72
73
74
75
76 BIntRngDataOutput(OutputStream out, int size) {
77 super(out, size);
78 }
79
80 @Override
81 public void fillBuffer(UniformRandomProvider rng) {
82 for (int i = 0; i < buffer.length; i += 4) {
83 writeIntBE(i, rng.nextInt());
84 }
85 }
86 }
87
88
89
90
91
92
93
94 private static class LIntRngDataOutput extends RngDataOutput {
95
96
97
98
99 LIntRngDataOutput(OutputStream out, int size) {
100 super(out, size);
101 }
102
103 @Override
104 public void fillBuffer(UniformRandomProvider rng) {
105 for (int i = 0; i < buffer.length; i += 4) {
106 writeIntLE(i, rng.nextInt());
107 }
108 }
109 }
110
111
112
113
114
115
116
117 private static class BLongRngDataOutput extends RngDataOutput {
118
119
120
121
122 BLongRngDataOutput(OutputStream out, int size) {
123 super(out, size);
124 }
125
126 @Override
127 public void fillBuffer(UniformRandomProvider rng) {
128 for (int i = 0; i < buffer.length; i += 8) {
129 writeLongBE(i, rng.nextLong());
130 }
131 }
132 }
133
134
135
136
137
138
139
140 private static class LLongRngDataOutput extends RngDataOutput {
141
142
143
144
145 LLongRngDataOutput(OutputStream out, int size) {
146 super(out, size);
147 }
148
149 @Override
150 public void fillBuffer(UniformRandomProvider rng) {
151 for (int i = 0; i < buffer.length; i += 8) {
152 writeLongLE(i, rng.nextLong());
153 }
154 }
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 private static class LLongAsIntRngDataOutput extends RngDataOutput {
172
173
174
175
176 LLongAsIntRngDataOutput(OutputStream out, int size) {
177 super(out, size);
178 }
179
180 @Override
181 public void fillBuffer(UniformRandomProvider rng) {
182 for (int i = 0; i < buffer.length; i += 8) {
183 writeLongAsIntLE(i, rng.nextLong());
184 }
185 }
186 }
187
188
189
190
191
192
193
194 RngDataOutput(OutputStream out, int size) {
195 this.out = out;
196 buffer = new byte[size];
197 }
198
199
200
201
202
203
204
205 public void write(UniformRandomProvider rng) throws IOException {
206 fillBuffer(rng);
207 out.write(buffer);
208 }
209
210
211
212
213
214
215 public abstract void fillBuffer(UniformRandomProvider rng);
216
217
218
219
220
221
222
223 final void writeIntBE(int index, int value) {
224 buffer[index ] = (byte) (value >>> 24);
225 buffer[index + 1] = (byte) (value >>> 16);
226 buffer[index + 2] = (byte) (value >>> 8);
227 buffer[index + 3] = (byte) value;
228 }
229
230
231
232
233
234
235
236 final void writeIntLE(int index, int value) {
237 buffer[index ] = (byte) value;
238 buffer[index + 1] = (byte) (value >>> 8);
239 buffer[index + 2] = (byte) (value >>> 16);
240 buffer[index + 3] = (byte) (value >>> 24);
241 }
242
243
244
245
246
247
248
249 final void writeLongBE(int index, long value) {
250 buffer[index ] = (byte) (value >>> 56);
251 buffer[index + 1] = (byte) (value >>> 48);
252 buffer[index + 2] = (byte) (value >>> 40);
253 buffer[index + 3] = (byte) (value >>> 32);
254 buffer[index + 4] = (byte) (value >>> 24);
255 buffer[index + 5] = (byte) (value >>> 16);
256 buffer[index + 6] = (byte) (value >>> 8);
257 buffer[index + 7] = (byte) value;
258 }
259
260
261
262
263
264
265
266 final void writeLongLE(int index, long value) {
267 buffer[index ] = (byte) value;
268 buffer[index + 1] = (byte) (value >>> 8);
269 buffer[index + 2] = (byte) (value >>> 16);
270 buffer[index + 3] = (byte) (value >>> 24);
271 buffer[index + 4] = (byte) (value >>> 32);
272 buffer[index + 5] = (byte) (value >>> 40);
273 buffer[index + 6] = (byte) (value >>> 48);
274 buffer[index + 7] = (byte) (value >>> 56);
275 }
276
277
278
279
280
281
282
283
284 final void writeLongAsIntLE(int index, long value) {
285 buffer[index ] = (byte) (value >>> 32);
286 buffer[index + 1] = (byte) (value >>> 40);
287 buffer[index + 2] = (byte) (value >>> 48);
288 buffer[index + 3] = (byte) (value >>> 56);
289 buffer[index + 4] = (byte) value;
290 buffer[index + 5] = (byte) (value >>> 8);
291 buffer[index + 6] = (byte) (value >>> 16);
292 buffer[index + 7] = (byte) (value >>> 24);
293 }
294
295 @Override
296 public void close() throws IOException {
297 try (OutputStream ostream = out) {
298 ostream.flush();
299 }
300 }
301
302
303
304
305
306
307
308
309
310
311 @SuppressWarnings("resource")
312 static RngDataOutput ofInt(OutputStream out, int size, ByteOrder byteOrder) {
313
314 final int bytes = Math.max(size * 4, 4);
315 return byteOrder == ByteOrder.LITTLE_ENDIAN ?
316 new LIntRngDataOutput(out, bytes) :
317 new BIntRngDataOutput(out, bytes);
318 }
319
320
321
322
323
324
325
326
327
328
329 @SuppressWarnings("resource")
330 static RngDataOutput ofLong(OutputStream out, int size, ByteOrder byteOrder) {
331
332 final int bytes = Math.max(size * 8, 8);
333 return byteOrder == ByteOrder.LITTLE_ENDIAN ?
334 new LLongRngDataOutput(out, bytes) :
335 new BLongRngDataOutput(out, bytes);
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 @SuppressWarnings("resource")
362 static RngDataOutput ofLongAsInt(OutputStream out, int size, ByteOrder byteOrder) {
363
364 final int bytes = Math.max(size * 8, 8);
365 return byteOrder == ByteOrder.LITTLE_ENDIAN ?
366 new LLongAsIntRngDataOutput(out, bytes) :
367 new BLongRngDataOutput(out, bytes);
368 }
369 }