File: ReaderToUTF8StreamTest.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (686 lines) | stat: -rw-r--r-- 24,965 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*

   Derby - Class org.apache.derbyTesting.unitTests.junit.ReaderToUTF8StreamTest

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to you under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.unitTests.junit;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Arrays;
import java.util.Random;
import junit.framework.Test;
import org.apache.derby.iapi.services.io.InputStreamUtil;
import org.apache.derby.iapi.types.ClobStreamHeaderGenerator;
import org.apache.derby.iapi.types.ReaderToUTF8Stream;
import org.apache.derbyTesting.functionTests.util.streams.CharAlphabet;
import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader;
import org.apache.derbyTesting.junit.BaseTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;

/**
 * Unit tests for ReaderToUTF8Stream.
 * <p>
 * Explicit tests for the mark/reset feature start with "testMark".
 */
public class ReaderToUTF8StreamTest
        extends BaseTestCase {

    /**
     * The default size of the internal buffer in ReaderToUTF8Stream. Used to
     * trigger specific events in the reader.
     */
    private static int DEFAULT_INTERNAL_BUFFER_SIZE = 32*1024;

    public ReaderToUTF8StreamTest(String name) {
        super(name);
    }

    public static Test suite() {
        return new BaseTestSuite(ReaderToUTF8StreamTest.class);
    }

    /**
     * Tests a very basic use of the mark/reset mechanism.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkResetSimplePosZero()
            throws IOException {
        InputStream is = getStream(100);
        is.mark(10);
        assertEquals(10, is.read(new byte[10]));
        is.reset();
        checkBeginningOfStream(is);
    }

    /**
     * Tests a very basic use of the mark/reset mechanism.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkResetSimplePosNonZero()
            throws IOException {
        InputStream is = getStream(200);
        assertEquals(127, is.read(new byte[127]));
        is.mark(10);
        byte[] readBeforeReset = new byte[10];
        byte[] readAfterReset = new byte[10];
        assertEquals(10, is.read(readBeforeReset));
        is.reset();
        assertEquals(10, is.read(readAfterReset));
        assertTrue(Arrays.equals(readBeforeReset, readAfterReset));
    }

    /**
     * Tests that shifting of existing bytes works.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkResetShiftBytesFew_Internal()
            throws IOException {
        InputStream is = getStream(128*1024);
        byte[] buf = new byte[DEFAULT_INTERNAL_BUFFER_SIZE - 2*1024];
        fillArray(is, buf);
        // The following mark fits within the existing default buffer, but the
        // bytes after the mark have to be shifted to the left.
        is.mark(4*1024);
        byte[] readBeforeReset = new byte[3*1024];
        byte[] readAfterReset = new byte[3*1024];
        fillArray(is, readBeforeReset);
        // Obtain something to compare with.
        InputStream src = getStream(128*1024);
        InputStreamUtil.skipFully(src, DEFAULT_INTERNAL_BUFFER_SIZE - 2*1024);
        byte[] comparisonRead = new byte[3*1024];
        fillArray(src, comparisonRead);
        // Compare
        assertEquals(new ByteArrayInputStream(comparisonRead),
                     new ByteArrayInputStream(readBeforeReset));
        // Reset the stream.
        is.reset();
        fillArray(is, readAfterReset);
        assertEquals(new ByteArrayInputStream(readBeforeReset),
                     new ByteArrayInputStream(readAfterReset));
    }

    /**
     * Tests that shifting of existing bytes works.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkResetShiftBytesMany_Internal()
            throws IOException {
        InputStream is = getStream(128*1024);
        is.read();
        is.read();
        // The following mark fits within the existing default buffer, but the
        // bytes after the mark have to be shifted to the left.
        is.mark(DEFAULT_INTERNAL_BUFFER_SIZE -6);
        byte[] readBeforeReset = new byte[DEFAULT_INTERNAL_BUFFER_SIZE -6];
        byte[] readAfterReset = new byte[DEFAULT_INTERNAL_BUFFER_SIZE -6];
        fillArray(is, readBeforeReset);
        // Obtain something to compare with.
        InputStream src = getStream(128*1024);
        src.read();
        src.read();
        byte[] comparisonRead = new byte[DEFAULT_INTERNAL_BUFFER_SIZE -6];
        fillArray(src, comparisonRead);
        // Compare
        assertEquals(new ByteArrayInputStream(comparisonRead),
                     new ByteArrayInputStream(readBeforeReset));
        // Reset the stream.
        is.reset();
        fillArray(is, readAfterReset);
        assertEquals(new ByteArrayInputStream(readBeforeReset),
                     new ByteArrayInputStream(readAfterReset));
    }

    /**
     * Tests an implementation specific feature of ReaderToUTF8Stream, which is
     * that the mark isn't invalidated even though we read past the read ahead
     * limit, given that the internal buffer doesn't have to be refilled.
     * <p>
     * <em>WARNING</em>:This implementation specific feature should not be
     * relied on by the production code! It may change at any time.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkResetExceedReadAheadLimitOK_Internal()
            throws IOException {
        InputStream is = getStream(4*1024+17);
        is.mark(10);
        assertEquals(20, is.read(new byte[20]));
        // Note the following is implementation dependent.
        // Since the bytes are already stored in the internal buffer, we won't
        // fail the reset even though we have exceeded the read ahead limit.
        // With a different stream implementation, this may fail!
        is.reset();
    }

    /**
     * Tests that the reset-call will fail we exceed the mark ahead limit and
     * the internal buffer has to be refilled.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkResetExceedReadAheadLimitFail_Internal()
            throws IOException {
        InputStream is = getStream(64*1024+17);
        is.mark(10);
        // The internal buffer is 32 KB (implementation detail).
        int toRead = 38*1024+7;
        int read = 0;
        byte[] buf = new byte[toRead];
        while (read < toRead) {
            read += is.read(buf, read, toRead - read);
        }
        // Note the following is implementation dependent.
        try {
            is.reset();
            fail("reset-call was expected to throw IOException");
        } catch (IOException ioe) {
            // As expected, do nothing
        }
    }

    /**
     * Reads almost enough bytes to read past the read ahead limit, then tests
     * that the reset works. After that, reads past the read ahead limit and
     * tests that the reset fails.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkResetOverflowInternalBufferKeepBytes()
            throws IOException {
        InputStream is = getStream(128*1024);
        is.mark(120*1024);
        byte[] buf = new byte[120*1024-1];
        fillArray(is, buf);
        is.reset();
        checkBeginningOfStream(is);

        // Again, but this time read past the read ahead limit.
        is = getStream(36*1024);
        is.mark(4*1024);
        buf = new byte[36*1024-1];
        fillArray(is, buf);
        try {
            is.reset();
            fail("reset-call was expected to throw IOException");
        }  catch (IOException ioe) {
            // As expected, do nothing
        }
    }

    /**
     * Marks the stream with a read ahead limit larger than the stream itself,
     * then reads until the end of the stream.
     * <p>
     * The current implementation does not allow the stream to be reset after
     * the last byte in the stream has been read once.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkReadUntilEOF()
            throws IOException {
        // Try with a single buffer fill first.
        InputStream is = getStream(4*1024);
        is.mark(8*1024);
        byte[] buf = new byte[8*1024];
        int read = 0;
        while (true) {
            int readNow = is.read(buf, read, buf.length - read);
            if (readNow == -1) {
                break;
            }
            read += readNow;
        }
        try {
            is.reset();
            fail("reset-call was expected to throw IOException");
        } catch (IOException ioe) {
            // The current implementation does not allow resetting the stream
            // when the source stream itself has been drained and all the data
            // has been read once.
        }

        // Now try with multiple buffer fills.
        is = getStream(640*1024);
        is.mark(128*1024);
        buf = new byte[8*1024];
        while (true) {
            // Just drain the stream.
            if (is.read(buf, 0, buf.length) == -1) {
                break;
            }
        }
        try {
            is.reset();
            fail("reset-call was expected to throw IOException");
        } catch (IOException ioe) {
            // The current implementation does not allow resetting the stream
            // when the source stream itself has been drained and all the data
            // has been read once.
        }
    }

    /**
     * Marks the stream with a read ahead limit larger than the stream itself,
     * then reads until just before the end of the stream.
     *
     * @throws IOException if something goes wrong
     */
    public void testMarkReadAlmostUntilEOF()
            throws IOException {
        // Try with a single buffer fill first.
        int limit = 4*1024;
        InputStream is = getStream(limit);
        is.mark(8*1024);
        byte[] buf = new byte[limit*2];
        int read = 0;
        while (read < limit -1) {
            int readNow = is.read(buf, read, (limit -1) - read);
            if (readNow == -1) {
                break;
            }
            read += readNow;
        }
        // EOF has been reached when filling the internal buffer, but we still
        // havent't read it. Therefore, the reset should succeed.
        is.reset();
        checkBeginningOfStream(is);
    }

    /**
     * Makes sure that the header bytes are copied when creating a new buffer
     * to hold all the required bytes when the stream has been marked.
     * This will only happen the first time the buffer is filled, i.e. when the
     * stream is marked before the first read (mark at position zero).
     *
     * @throws IOException if something goes wrong
     */
    public void testHeaderPresentInStream_Internal()
            throws IOException {
        final int valueLen = DEFAULT_INTERNAL_BUFFER_SIZE + 5*1024;
        InputStream is = getStream(valueLen);
        is.mark(valueLen - 1024);
        // Obtain a header generator to compare with.
        ClobStreamHeaderGenerator hdrGen = new ClobStreamHeaderGenerator(false);
        byte[] hdrTmp = new byte[100];
        int headerLen = hdrGen.generateInto(hdrTmp, 0, valueLen);
        byte[] hdr1 = new byte[headerLen];
        System.arraycopy(hdrTmp, 0, hdr1, 0, headerLen);
        byte[] hdr2 = new byte[headerLen];
        // Get the first bytes from the stream being tested.
        assertEquals(headerLen, is.read(hdr2));
        assertEquals(new ByteArrayInputStream(hdr1),
                     new ByteArrayInputStream(hdr2));
    }

    /**
     * Returns a stream to test, loaded with the repeating modern latin
     * lowercase alphabet.
     *
     * @param length the length of the stream in characters
     * @return A stream serving bytes.
     */
    private InputStream getStream(int length) {
        Reader src = new LoopingAlphabetReader(length,
                                        CharAlphabet.modernLatinLowercase());
        InputStream is = new ReaderToUTF8Stream(
                src, length, 0, "CLOB", new ClobStreamHeaderGenerator(false));
        assertTrue("The stream doesn't support mark/reset", is.markSupported());
        return is;
    }

    /**
     * Checks the beginning of the stream, which is expected to consist of five
     * header bytes (skipped) followed by the bytes for the characters 'a' and
     * 'b'.
     *
     * @param is the stream to check
     * @throws IOException if reading from the stream fails
     * @throws AssertionFailedError if the stream content isn't as expected
     */
    private void checkBeginningOfStream(InputStream is)
            throws IOException {
        assertEquals(5, is.skip(5));
        // We should now get the character a, followed by b.
        assertEquals((byte)'a', is.read());
        assertEquals((byte)'b', is.read());
    }

    /**
     * Fills the array by reading from the stream.
     *
     * @param is input stream to read from
     * @param b array to fill with bytes from the stream
     * @throws IOException if reading from the array fails, or the end of the
     *      stream is reached
     */
    private void fillArray(InputStream is, byte[] b)
            throws IOException {
        final int toRead = b.length;
        int read = 0;
        while (read < toRead) {
            int readNow = is.read(b, read, toRead - read);
            assertTrue("reached EOF", readNow != -1);
            read += readNow;
        }
    }

    /**
     * Performs a series of random operations on a {@code ReaderToUTF8Stream},
     * consisting of read, skip, mark, reset and a noop.
     * <p>
     * <em>Note</em>: Turn on debugging (derby.tests.debug=true) to see some
     * information, turn on tracing (derby.tests.trace=true) in addition to see
     * a lot more information.
     * <p>
     * If the test fails, the seed will be reported in the error message, and
     * the load that failed can be rerun.
     *
     * @throws IOException if the test fails
     */
    public void testRandomSequence()
            throws IOException {
        final long seed = System.currentTimeMillis();
        try {
            testRandomSequence(seed);
        } catch (IOException ioe) {
            // Report the seed for repeatability.
            IOException wrapper = new IOException("seed=" + seed);
            wrapper.initCause(ioe);
            throw wrapper;
        }
    }

    /**
     * Performs a series of random operations on a {@code ReaderToUTF8Stream},
     * consisting of read, skip, mark, reset and a noop.
     * <p>
     * Note that this test verifies that executing the operations don't fail,
     * but it doesn't verify that the bytes obtained from the stream are the
     * correct ones.
     *
     * @param seed seed controlling the test load
     * @throws IOException if the test fails
     */
    private void testRandomSequence(long seed)
            throws IOException {
        println("testRandomSequence seed: " + seed);
        final int iterations = 100;
        final Random rng = new Random(seed);
        for (int i=0; i < iterations; i++) {
            // Operation counters.
            int reads = 0, skips = 0, resets = 0, marks = 0, invalidations = 0;
            // Stream length (up to ~1 MB).
            int length = 1024*rng.nextInt(1024) + rng.nextInt(1024);
            boolean rs = rng.nextBoolean();
            println(">>> iteration " + i + ", length=" + length);
            int currentPos = 0;
            int limit = 0;
            int mark = -1;
            InputStream is = getStream(length);
            int ops = 0;
            while (ops < 200 && currentPos < length - 10) {
                if (rng.nextBoolean()) { // Whether to read/skip or mark/reset.
                    int toRead = getRandomLength(currentPos, length, rng, rs);
                    if (rng.nextBoolean()) {
                        // Read
                        mytrace("\treading " + toRead + " bytes");
                        reads++;
                        is.read(new byte[toRead]);
                    } else {
                        // Skip
                        mytrace("\tskipping " + toRead + " bytes");
                        skips++;
                        is.skip(toRead);
                    }
                    currentPos += toRead;
                    if (mark != -1 && (currentPos - mark) > limit) {
                        mytrace("\t\tmark invalidated");
                        invalidations++;
                        mark = -1;
                        limit = 0;
                    }
                }
                if (rng.nextBoolean()) { // Whether to read/skip or mark/reset.
                    // Mark/reset, or do nothing.
                    if (rng.nextBoolean()) {
                        if (rng.nextInt(100) < 40 && mark != -1) {
                            // Reset
                            mytrace("\tresetting to position " + mark);
                            resets++;
                            is.reset();
                            currentPos = mark;
                            mark = -1;
                        } else {
                            // Mark
                            limit = getRandomLength(currentPos, length, rng);
                            mytrace("\tmarking position " + currentPos +
                                    " with limit " + limit);
                            marks++;
                            mark = currentPos;
                            is.mark(limit);
                        }
                    }
                }
                ops++;
            }
            println("ops=" + ops + ", reads=" + reads + ", skips=" + skips +
                    ", marks=" + marks + ", resets=" + resets +
                    ", invalidations=" + invalidations);
        }
    }


    /**
     * Returns a random length within the limits.
     * <p>
     * This call will operate in the full range of the remaining bytes.
     *
     * @param currentPos the current position of the stream
     * @param length the length of the stream
     * @param rng random generator
     * @return A random length within the limits of the stream.
     */
    private int getRandomLength(int currentPos, int length, Random rng) {
        return getRandomLength(currentPos, length, rng, false);
    }

    /**
     * Returns a random length within the limits.
     *
     * @param currentPos the current position of the stream
     * @param length the length of the stream
     * @param rng random generator
     * @param reducedSize whether to return smaller number or not
     *      (setting to true may increase the number of operations that will be
     *      performed on a stream before it is exhausted)
     * @return A random length within the limits of the stream.
     */
    private int getRandomLength(int currentPos, int length, Random rng, boolean reducedSize) {
        int max = length - currentPos;
        if (reducedSize) {
            max = max / 5;
        }
        return (1 + (int)(max * rng.nextFloat()));
    }

    /**
     * Trace only if both trace and verbose is true in the test configuration.
     *
     * @param str the string to print
     */
    private void mytrace(String str) {
        if (getTestConfiguration().isVerbose()) {
            traceit(str);
        }
    }

    /**
     * Tests mark/reset functionality by comparing with
     * {@code ByteArrayInputStream}.
     *
     * @throws IOException if the test fails
     */
    public void testMarkReset1()
            throws IOException {
        InputStream is = getStream(64*1024);
        byte[] srcBuf = new byte[64*1024+5];
        fillArray(is, srcBuf);
        InputStream src = new ByteArrayInputStream(srcBuf);
        // Reinitialize the stream.
        is = getStream(64*1024);

        StreamUtil su = new StreamUtil(is, src);
        su.mark(1024);
        su.skip(17);
        su.reset();
        su.read(1);
        su.read(2133);
        su.mark(1024);
        su.reset();
        su.mark(1024);
        su.skip(18);
        su.read(1024);
    }

    /**
     * Tests mark/reset functionality by comparing with
     * {@code ByteArrayInputStream}. This test relies on knowing the size of
     * the internal buffer to force a shifting of existing bytes to take place.
     *
     * @throws IOException if the test fails
     */
    public void testMarkReset2_Internal()
            throws IOException {
        InputStream is = getStream(128*1024);
        byte[] srcBuf = new byte[128*1024+5];
        fillArray(is, srcBuf);
        InputStream src = new ByteArrayInputStream(srcBuf);
        // Reinitialize the stream.
        is = getStream(128*1024);

        StreamUtil su = new StreamUtil(is, src);
        su.skip(DEFAULT_INTERNAL_BUFFER_SIZE);
        su.mark(DEFAULT_INTERNAL_BUFFER_SIZE + 2*1024);
        su.read(1024);
        su.reset();
        su.read(3*1024);
    }

    /**
     * Utility class executing a few selected method calls on two streams,
     * expecting both of them to behave in the same way.
     */
    private class StreamUtil {
        private final InputStream is1;
        private final InputStream is2;

        StreamUtil(InputStream is1, InputStream is2) {
            assertNotNull(is1);
            assertNotNull(is2);
            this.is1 = is1;
            this.is2 = is2;
        }

        public void mark(int readAheadLimit) {
            is1.mark(readAheadLimit);
            is2.mark(readAheadLimit);
        }

        public void reset()
                throws IOException {
            is1.reset();
            is2.reset();
        }

        public long skip(long skip)
                throws IOException {
            long skip1 = 0;
            long skip2 = 0;
            // Skip data in the first stream.
            while (skip1 < skip) {
                long skippedNow = is1.skip(skip - skip1);
                if (skippedNow == -1) {
                    fail("stream one reached EOF: " + is1.getClass());
                }
                skip1 += skippedNow;
            }
            // Skip data in the second stream.
            while (skip2 < skip) {
                long skippedNow = is2.skip(skip - skip2);
                if (skippedNow == -1) {
                    fail("stream two reached EOF: " + is2.getClass());
                }
                skip2 += skippedNow;
            }
            assertEquals(skip1, skip2);
            return skip1;
        }

        public int read(int toRead)
                throws IOException {
            byte[] b1 = new byte[toRead];
            byte[] b2 = new byte[toRead];
            int read = read(b1, b2, false);
            assertEquals(new ByteArrayInputStream(b1),
                         new ByteArrayInputStream(b2));
            return read;
        }

        public int read(byte[] b1, byte[] b2, boolean expectEOF)
                throws IOException {
            assertEquals("unequal sized arrays", b1.length, b2.length);
            int read1 = 0;
            int read2 = 0;
            final int toRead = b1.length;
            // Read from the first stream.
            while (read1 < toRead) {
                int readNow = is1.read(b1, read1, toRead - read1);
                if (readNow == -1) {
                    if (expectEOF) {
                        break;
                    } else {
                        fail("stream one reached EOF: " + is1.getClass());
                    }
                }
                read1 += readNow;
            }
            // Read from the second stream.
            while (read2 < toRead) {
                int readNow = is2.read(b2, read2, toRead - read2);
                if (readNow == -1) {
                    if (expectEOF) {
                        break;
                    } else {
                        fail("stream two reached EOF: " + is2.getClass());
                    }
                }
                read2 += readNow;
            }
            assertEquals(read1, read2);
            return read1;
        }
    }
}