aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/io/ByteStreamsTest.java
blob: ff0497ac556423d226bd77c3e208fae0f5eb222f (plain)
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
 * Copyright (C) 2007 The Guava Authors
 *
 * Licensed 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 com.google.common.io;

import static com.google.common.io.ByteStreams.copy;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;

import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.Bytes;
import com.google.common.testing.TestLogHandler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Arrays;
import java.util.Random;

/**
 * Unit test for {@link ByteStreams}.
 *
 * @author Chris Nokleberg
 */
public class ByteStreamsTest extends IoTestCase {

  /** Provides an InputStream that throws an IOException on every read. */
  static final InputSupplier<InputStream> BROKEN_READ
      = new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() {
          return new InputStream() {
            @Override public int read() throws IOException {
              throw new IOException("broken read");
            }
          };
        }
      };

  /** Provides an OutputStream that throws an IOException on every write. */
  static final OutputSupplier<OutputStream> BROKEN_WRITE
      = new OutputSupplier<OutputStream>() {
        @Override
        public OutputStream getOutput() {
          return new OutputStream() {
            @Override public void write(int b) throws IOException {
              throw new IOException("broken write");
            }
          };
        }
      };

  /** Provides an InputStream that throws an IOException on close. */
  static final InputSupplier<InputStream> BROKEN_CLOSE_INPUT =
      new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() {
          return new FilterInputStream(new ByteArrayInputStream(new byte[10])) {
            @Override public void close() throws IOException {
              throw new IOException("broken close input");
            }
          };
        }
      };

  /** Provides an OutputStream that throws an IOException on every close. */
  static final OutputSupplier<OutputStream> BROKEN_CLOSE_OUTPUT =
      new OutputSupplier<OutputStream>() {
        @Override
        public OutputStream getOutput() {
          return new FilterOutputStream(new ByteArrayOutputStream()) {
            @Override public void close() throws IOException {
              throw new IOException("broken close output");
            }
          };
        }
      };

  /** Throws an IOException from getInput. */
  static final InputSupplier<InputStream> BROKEN_GET_INPUT =
      new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
          throw new IOException("broken get input");
        }
      };

  /** Throws an IOException from getOutput. */
  static final OutputSupplier<OutputStream> BROKEN_GET_OUTPUT =
      new OutputSupplier<OutputStream>() {
        @Override
        public OutputStream getOutput() throws IOException {
          throw new IOException("broken get output");
        }
      };

  private static final ImmutableSet<InputSupplier<InputStream>> BROKEN_INPUTS =
      ImmutableSet.of(BROKEN_CLOSE_INPUT, BROKEN_GET_INPUT, BROKEN_READ);
  private static final ImmutableSet<OutputSupplier<OutputStream>> BROKEN_OUTPUTS
      = ImmutableSet.of(BROKEN_CLOSE_OUTPUT, BROKEN_GET_OUTPUT, BROKEN_WRITE);

  public void testByteSuppliers() throws IOException {
    byte[] range = newPreFilledByteArray(200);
    assertTrue(Arrays.equals(range,
        ByteStreams.toByteArray(ByteStreams.newInputStreamSupplier(range))));

    byte[] subRange = ByteStreams.toByteArray(
        ByteStreams.newInputStreamSupplier(range, 100, 50));
    assertEquals(50, subRange.length);
    assertEquals(100, subRange[0]);
    assertEquals((byte) 149, subRange[subRange.length - 1]);
  }

  public void testEqual() throws IOException {
    equalHelper(false, 0, 1);
    equalHelper(false, 400, 10000);
    equalHelper(false, 0x2000, 0x2001);
    equalHelper(false, new byte[]{ 0 }, new byte[]{ 1 });

    byte[] mutate = newPreFilledByteArray(10000);
    mutate[9000] = 0;
    equalHelper(false, mutate, newPreFilledByteArray(10000));

    equalHelper(true, 0, 0);
    equalHelper(true, 1, 1);
    equalHelper(true, 400, 400);

    final byte[] tenK = newPreFilledByteArray(10000);
    equalHelper(true, tenK, tenK);
    assertTrue(ByteStreams.equal(ByteStreams.newInputStreamSupplier(tenK),
        new InputSupplier<InputStream>() {
          @Override
          public InputStream getInput() {
            return new RandomAmountInputStream(new ByteArrayInputStream(tenK),
                new Random(301));
          }
        }));
  }

  private void equalHelper(boolean expect, int size1, int size2)
      throws IOException {
    equalHelper(expect, newPreFilledByteArray(size1),
        newPreFilledByteArray(size2));
  }

  private void equalHelper(boolean expect, byte[] a, byte[] b)
      throws IOException {
    assertEquals(expect, ByteStreams.equal(
        ByteStreams.newInputStreamSupplier(a),
        ByteStreams.newInputStreamSupplier(b)));
  }

  public void testAlwaysCloses() throws IOException {
    byte[] range = newPreFilledByteArray(100);
    CheckCloseSupplier.Input<InputStream> okRead
        = newCheckInput(ByteStreams.newInputStreamSupplier(range));
    CheckCloseSupplier.Output<OutputStream> okWrite
        = newCheckOutput(new OutputSupplier<OutputStream>() {
          @Override
          public OutputStream getOutput() {
            return new ByteArrayOutputStream();
          }
        });

    CheckCloseSupplier.Input<InputStream> brokenRead
        = newCheckInput(BROKEN_READ);
    CheckCloseSupplier.Output<OutputStream> brokenWrite
        = newCheckOutput(BROKEN_WRITE);

    // copy, both suppliers
    ByteStreams.copy(okRead, okWrite);
    assertTrue(okRead.areClosed());
    assertTrue(okWrite.areClosed());

    try {
      ByteStreams.copy(okRead, brokenWrite);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken write", e.getMessage());
    }
    assertTrue(okRead.areClosed());
    assertTrue(brokenWrite.areClosed());

    try {
      ByteStreams.copy(brokenRead, okWrite);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertTrue(brokenRead.areClosed());
    assertTrue(okWrite.areClosed());

    try {
      ByteStreams.copy(brokenRead, brokenWrite);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertTrue(brokenRead.areClosed());
    assertTrue(brokenWrite.areClosed());

    // copy, input supplier
    OutputStream out = okWrite.getOutput();
    ByteStreams.copy(okRead, out);
    assertTrue(okRead.areClosed());
    assertFalse(okWrite.areClosed());
    out.close();

    out = brokenWrite.getOutput();
    try {
      ByteStreams.copy(okRead, out);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken write", e.getMessage());
    }
    assertTrue(okRead.areClosed());
    assertFalse(brokenWrite.areClosed());
    out.close();

    out = okWrite.getOutput();
    try {
      ByteStreams.copy(brokenRead, out);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertTrue(brokenRead.areClosed());
    assertFalse(okWrite.areClosed());
    out.close();

    out = brokenWrite.getOutput();
    try {
      ByteStreams.copy(brokenRead, out);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertTrue(brokenRead.areClosed());
    assertFalse(brokenWrite.areClosed());
    out.close();

    // copy, output supplier
    InputStream in = okRead.getInput();
    ByteStreams.copy(in, okWrite);
    assertFalse(okRead.areClosed());
    assertTrue(okWrite.areClosed());
    in.close();

    in = okRead.getInput();
    try {
      ByteStreams.copy(in, brokenWrite);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken write", e.getMessage());
    }
    assertFalse(okRead.areClosed());
    assertTrue(brokenWrite.areClosed());
    in.close();

    in = brokenRead.getInput();
    try {
      ByteStreams.copy(in, okWrite);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertFalse(brokenRead.areClosed());
    assertTrue(okWrite.areClosed());
    in.close();

    in = brokenRead.getInput();
    try {
      ByteStreams.copy(in, brokenWrite);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertFalse(brokenRead.areClosed());
    assertTrue(brokenWrite.areClosed());
    in.close();

    // toByteArray
    assertTrue(Arrays.equals(range, ByteStreams.toByteArray(okRead)));
    assertTrue(okRead.areClosed());

    try {
      ByteStreams.toByteArray(brokenRead);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertTrue(brokenRead.areClosed());

    // equal
    try {
      ByteStreams.equal(brokenRead, okRead);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertTrue(brokenRead.areClosed());

    try {
      ByteStreams.equal(okRead, brokenRead);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken read", e.getMessage());
    }
    assertTrue(brokenRead.areClosed());

    // write
    try {
      ByteStreams.write(new byte[10], brokenWrite);
      fail("expected exception");
    } catch (IOException e) {
      assertEquals("broken write", e.getMessage());
    }
    assertTrue(brokenWrite.areClosed());
  }

  private static int getAndResetRecords(TestLogHandler logHandler) {
    int records = logHandler.getStoredLogRecords().size();
    logHandler.clear();
    return records;
  }

  private static void runFailureTest(
      InputSupplier<? extends InputStream> in, OutputSupplier<OutputStream> out) {
    try {
      copy(in, out);
      fail();
    } catch (IOException expected) {
    }
  }

  private static OutputSupplier<OutputStream> newByteArrayOutputStreamSupplier() {
    return new OutputSupplier<OutputStream>() {
      @Override public OutputStream getOutput() {
        return new ByteArrayOutputStream();
      }
    };
  }

  public void testWriteBytes() throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] expected = newPreFilledByteArray(100);
    ByteStreams.write(expected, new OutputSupplier<OutputStream>() {
      @Override public OutputStream getOutput() {
        return out;
      }
    });
    assertTrue(Arrays.equals(expected, out.toByteArray()));
  }

  public void testCopy() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] expected = newPreFilledByteArray(100);
    long num = ByteStreams.copy(new ByteArrayInputStream(expected), out);
    assertEquals(100, num);
    assertTrue(Arrays.equals(expected, out.toByteArray()));
  }

  public void testCopyChannel() throws IOException {
    byte[] expected = newPreFilledByteArray(100);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    WritableByteChannel outChannel = Channels.newChannel(out);

    ReadableByteChannel inChannel =
        Channels.newChannel(new ByteArrayInputStream(expected));
    ByteStreams.copy(inChannel, outChannel);
    assertTrue(Arrays.equals(expected, out.toByteArray()));
  }

  public void testReadFully() throws IOException {
    byte[] b = new byte[10];

    try {
      ByteStreams.readFully(newTestStream(10), null, 0, 10);
      fail("expected exception");
    } catch (NullPointerException e) {
    }

    try {
      ByteStreams.readFully(null, b, 0, 10);
      fail("expected exception");
    } catch (NullPointerException e) {
    }

    try {
      ByteStreams.readFully(newTestStream(10), b, -1, 10);
      fail("expected exception");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      ByteStreams.readFully(newTestStream(10), b, 0, -1);
      fail("expected exception");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      ByteStreams.readFully(newTestStream(10), b, 0, -1);
      fail("expected exception");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      ByteStreams.readFully(newTestStream(10), b, 2, 10);
      fail("expected exception");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      ByteStreams.readFully(newTestStream(5), b, 0, 10);
      fail("expected exception");
    } catch (EOFException e) {
    }

    Arrays.fill(b, (byte) 0);
    ByteStreams.readFully(newTestStream(10), b, 0, 0);
    assertTrue(Arrays.equals(new byte[10], b));

    Arrays.fill(b, (byte) 0);
    ByteStreams.readFully(newTestStream(10), b, 0, 10);
    assertTrue(Arrays.equals(newPreFilledByteArray(10), b));

    Arrays.fill(b, (byte) 0);
    ByteStreams.readFully(newTestStream(10), b, 0, 5);
    assertTrue(Arrays.equals(new byte[]{0, 1, 2, 3, 4, 0, 0, 0, 0, 0}, b));
  }

  public void testSkipFully() throws IOException {
    byte[] bytes = newPreFilledByteArray(100);
    skipHelper(0, 0, new ByteArrayInputStream(bytes));
    skipHelper(50, 50, new ByteArrayInputStream(bytes));
    skipHelper(50, 50, new SlowSkipper(new ByteArrayInputStream(bytes), 1));
    skipHelper(50, 50, new SlowSkipper(new ByteArrayInputStream(bytes), 0));
    skipHelper(100, -1, new ByteArrayInputStream(bytes));
    try {
      skipHelper(101, 0, new ByteArrayInputStream(bytes));
      fail("expected exception");
    } catch (EOFException e) {
    }
  }

  private void skipHelper(long n, int expect, InputStream in)
      throws IOException {
    ByteStreams.skipFully(in, n);
    assertEquals(expect, in.read());
    in.close();
  }

  // TODO(user): rename; violates rule that only immutable things can be all caps
  private static final byte[] BYTES = new byte[] {
      0x12, 0x34, 0x56, 0x78, 0x76, 0x54, 0x32, 0x10 };

  public void testNewDataInput_empty() {
    byte[] b = new byte[0];
    ByteArrayDataInput in = ByteStreams.newDataInput(b);
    try {
      in.readInt();
      fail();
    } catch (IllegalStateException expected) {
    }
  }

  public void testNewDataInput_normal() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES);
    assertEquals(0x12345678, in.readInt());
    assertEquals(0x76543210, in.readInt());
    try {
      in.readInt();
      fail();
    } catch (IllegalStateException expected) {
    }
  }

  public void testNewDataInput_readFully() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES);
    byte[] actual = new byte[BYTES.length];
    in.readFully(actual);
    assertEquals(BYTES, actual);
  }

  public void testNewDataInput_readFullyAndThenSome() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES);
    byte[] actual = new byte[BYTES.length * 2];
    try {
      in.readFully(actual);
      fail();
    } catch (IllegalStateException ex) {
      assertTrue(ex.getCause() instanceof EOFException);
    }
  }

  public void testNewDataInput_readFullyWithOffset() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES);
    byte[] actual = new byte[4];
    in.readFully(actual, 2, 2);
    assertEquals(0, actual[0]);
    assertEquals(0, actual[1]);
    assertEquals(BYTES[0], actual[2]);
    assertEquals(BYTES[1], actual[3]);
  }

  public void testNewDataInput_readLine() {
    ByteArrayDataInput in = ByteStreams.newDataInput(
        "This is a line\r\nThis too\rand this\nand also this".getBytes(Charsets.UTF_8));
    assertEquals("This is a line", in.readLine());
    assertEquals("This too", in.readLine());
    assertEquals("and this", in.readLine());
    assertEquals("and also this", in.readLine());
  }

  public void testNewDataInput_readFloat() {
    byte[] data = {0x12, 0x34, 0x56, 0x78, 0x76, 0x54, 0x32, 0x10};
    ByteArrayDataInput in = ByteStreams.newDataInput(data);
    assertEquals(Float.intBitsToFloat(0x12345678), in.readFloat(), 0.0);
    assertEquals(Float.intBitsToFloat(0x76543210), in.readFloat(), 0.0);
  }

  public void testNewDataInput_readDouble() {
    byte[] data = {0x12, 0x34, 0x56, 0x78, 0x76, 0x54, 0x32, 0x10};
    ByteArrayDataInput in = ByteStreams.newDataInput(data);
    assertEquals(Double.longBitsToDouble(0x1234567876543210L), in.readDouble(), 0.0);
  }

  public void testNewDataInput_readUTF() {
    byte[] data = new byte[17];
    data[1] = 15;
    System.arraycopy("Kilroy was here".getBytes(Charsets.UTF_8), 0, data, 2, 15);
    ByteArrayDataInput in = ByteStreams.newDataInput(data);
    assertEquals("Kilroy was here", in.readUTF());
  }

  public void testNewDataInput_readChar() {
    byte[] data = "qed".getBytes(Charsets.UTF_16BE);
    ByteArrayDataInput in = ByteStreams.newDataInput(data);
    assertEquals('q', in.readChar());
    assertEquals('e', in.readChar());
    assertEquals('d', in.readChar());
  }

  public void testNewDataInput_readUnsignedShort() {
    byte[] data = {0, 0, 0, 1, (byte) 0xFF, (byte) 0xFF, 0x12, 0x34};
    ByteArrayDataInput in = ByteStreams.newDataInput(data);
    assertEquals(0, in.readUnsignedShort());
    assertEquals(1, in.readUnsignedShort());
    assertEquals(65535, in.readUnsignedShort());
    assertEquals(0x1234, in.readUnsignedShort());
  }

  public void testNewDataInput_readLong() {
    byte[] data = {0x12, 0x34, 0x56, 0x78, 0x76, 0x54, 0x32, 0x10};
    ByteArrayDataInput in = ByteStreams.newDataInput(data);
    assertEquals(0x1234567876543210L, in.readLong());
  }

  public void testNewDataInput_readBoolean() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES);
    assertTrue(in.readBoolean());
  }

  public void testNewDataInput_readByte() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES);
    for (int i = 0; i < BYTES.length; i++) {
      assertEquals(BYTES[i], in.readByte());
    }
    try {
      in.readByte();
      fail();
    } catch (IllegalStateException ex) {
      assertTrue(ex.getCause() instanceof EOFException);
    }
  }

  public void testNewDataInput_readUnsignedByte() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES);
    for (int i = 0; i < BYTES.length; i++) {
      assertEquals(BYTES[i], in.readUnsignedByte());
    }
    try {
      in.readUnsignedByte();
      fail();
    } catch (IllegalStateException ex) {
      assertTrue(ex.getCause() instanceof EOFException);
    }
  }

  public void testNewDataInput_offset() {
    ByteArrayDataInput in = ByteStreams.newDataInput(BYTES, 2);
    assertEquals(0x56787654, in.readInt());
    try {
      in.readInt();
      fail();
    } catch (IllegalStateException expected) {
    }
  }

  public void testNewDataInput_skip() {
    ByteArrayDataInput in = ByteStreams.newDataInput(new byte[2]);
    in.skipBytes(2);
    try {
      in.skipBytes(1);
    } catch (IllegalStateException expected) {
    }
  }

  public void testNewDataOutput_empty() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    assertEquals(0, out.toByteArray().length);
  }

  public void testNewDataOutput_writeInt() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeInt(0x12345678);
    out.writeInt(0x76543210);
    assertTrue(Arrays.equals(BYTES, out.toByteArray()));
  }

  public void testNewDataOutput_sized() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput(4);
    out.writeInt(0x12345678);
    out.writeInt(0x76543210);
    assertTrue(Arrays.equals(BYTES, out.toByteArray()));
  }

  public void testNewDataOutput_writeLong() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeLong(0x1234567876543210L);
    assertTrue(Arrays.equals(BYTES, out.toByteArray()));
  }

  public void testNewDataOutput_writeByteArray() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.write(BYTES);
    assertTrue(Arrays.equals(BYTES, out.toByteArray()));
  }

  public void testNewDataOutput_writeByte() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.write(0x12);
    out.writeByte(0x34);
    assertTrue(Arrays.equals(new byte[] {0x12, 0x34}, out.toByteArray()));
  }

  public void testNewDataOutput_writeByteOffset() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.write(BYTES, 4, 2);
    byte[] expected = {BYTES[4], BYTES[5]};
    assertEquals(expected, out.toByteArray());
  }

  public void testNewDataOutput_writeBoolean() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeBoolean(true);
    out.writeBoolean(false);
    byte[] expected = {(byte) 1, (byte) 0};
    assertEquals(expected, out.toByteArray());
  }

  public void testNewDataOutput_writeChar() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeChar('a');
    assertTrue(Arrays.equals(new byte[] {0, 97}, out.toByteArray()));
  }

  public void testNewDataOutput_writeChars() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeChars("r\u00C9sum\u00C9");
    // need to remove byte order mark before comparing
    byte[] expected = Arrays.copyOfRange("r\u00C9sum\u00C9".getBytes(Charsets.UTF_16), 2, 14);
    assertEquals(expected, out.toByteArray());
  }

  public void testNewDataOutput_writeUTF() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeUTF("r\u00C9sum\u00C9");
    byte[] expected ="r\u00C9sum\u00C9".getBytes(Charsets.UTF_8);
    byte[] actual = out.toByteArray();
    // writeUTF writes the length of the string in 2 bytes
    assertEquals(0, actual[0]);
    assertEquals(expected.length, actual[1]);
    assertEquals(expected, Arrays.copyOfRange(actual, 2, actual.length));
  }

  public void testNewDataOutput_writeShort() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeShort(0x1234);
    assertTrue(Arrays.equals(new byte[] {0x12, 0x34}, out.toByteArray()));
  }

  public void testNewDataOutput_writeDouble() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeDouble(Double.longBitsToDouble(0x1234567876543210L));
    assertEquals(BYTES, out.toByteArray());
  }

  public void testNewDataOutput_writeFloat() {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeFloat(Float.intBitsToFloat(0x12345678));
    out.writeFloat(Float.intBitsToFloat(0x76543210));
    assertEquals(BYTES, out.toByteArray());
  }

  public void testLength() throws IOException {
    lengthHelper(Long.MAX_VALUE);
    lengthHelper(7);
    lengthHelper(1);
    lengthHelper(0);

    assertEquals(0, ByteStreams.length(
        ByteStreams.newInputStreamSupplier(new byte[0])));
  }

  private void lengthHelper(final long skipLimit) throws IOException {
    assertEquals(100, ByteStreams.length(new InputSupplier<InputStream>() {
      @Override
      public InputStream getInput() {
        return new SlowSkipper(new ByteArrayInputStream(new byte[100]),
            skipLimit);
      }
    }));
  }

  public void testSlice() throws IOException {
    // Test preconditions
    InputSupplier<? extends InputStream> supplier
        = ByteStreams.newInputStreamSupplier(newPreFilledByteArray(100));
    try {
      ByteStreams.slice(supplier, -1, 10);
      fail("expected exception");
    } catch (IllegalArgumentException expected) {
    }

    try {
      ByteStreams.slice(supplier, 0, -1);
      fail("expected exception");
    } catch (IllegalArgumentException expected) {
    }

    try {
      ByteStreams.slice(null, 0, 10);
      fail("expected exception");
    } catch (NullPointerException expected) {
    }

    sliceHelper(0, 0, 0, 0);
    sliceHelper(0, 0, 1, 0);
    sliceHelper(100, 0, 10, 10);
    sliceHelper(100, 0, 100, 100);
    sliceHelper(100, 5, 10, 10);
    sliceHelper(100, 5, 100, 95);
    sliceHelper(100, 100, 0, 0);
    sliceHelper(100, 100, 10, 0);

    try {
      sliceHelper(100, 101, 10, 0);
      fail("expected exception");
    } catch (EOFException expected) {
    }
  }

  /**
   * @param input the size of the input stream
   * @param offset the first argument to {@link ByteStreams#slice}
   * @param length the second argument to {@link ByteStreams#slice}
   * @param expectRead the number of bytes we expect to read
   */
  private static void sliceHelper(
      int input, int offset, long length, int expectRead) throws IOException {
    Preconditions.checkArgument(expectRead == (int)
        Math.max(0, Math.min(input, offset + length) - offset));
    InputSupplier<? extends InputStream> supplier
        = ByteStreams.newInputStreamSupplier(newPreFilledByteArray(input));
    assertTrue(Arrays.equals(
        newPreFilledByteArray(offset, expectRead),
        ByteStreams.toByteArray(ByteStreams.slice(supplier, offset, length))));
  }

  private static InputStream newTestStream(int n) {
    return new ByteArrayInputStream(newPreFilledByteArray(n));
  }

  private static CheckCloseSupplier.Input<InputStream> newCheckInput(
      InputSupplier<? extends InputStream> delegate) {
    return new CheckCloseSupplier.Input<InputStream>(delegate) {
      @Override protected InputStream wrap(InputStream object,
          final Callback callback) {
        return new FilterInputStream(object) {
          @Override public void close() throws IOException {
            callback.delegateClosed();
            super.close();
          }
        };
      }
    };
  }

  private static CheckCloseSupplier.Output<OutputStream> newCheckOutput(
      OutputSupplier<? extends OutputStream> delegate) {
    return new CheckCloseSupplier.Output<OutputStream>(delegate) {
      @Override protected OutputStream wrap(OutputStream object,
          final Callback callback) {
        return new FilterOutputStream(object) {
          @Override public void close() throws IOException {
            callback.delegateClosed();
            super.close();
          }
        };
      }
    };
  }

  /** Stream that will skip a maximum number of bytes at a time. */
  private static class SlowSkipper extends FilterInputStream {
    private final long max;

    public SlowSkipper(InputStream in, long max) {
      super(in);
      this.max = max;
    }

    @Override public long skip(long n) throws IOException {
      return super.skip(Math.min(max, n));
    }
  }

  public void testByteProcessorStopEarly() throws IOException {
    byte[] array = newPreFilledByteArray(6000);
    assertEquals((Integer) 42,
        ByteStreams.readBytes(ByteStreams.newInputStreamSupplier(array),
            new ByteProcessor<Integer>() {
              @Override
              public boolean processBytes(byte[] buf, int off, int len) {
                assertTrue(Arrays.equals(
                    copyOfRange(buf, off, off + len),
                    newPreFilledByteArray(4096)));
                return false;
              }

              @Override
              public Integer getResult() {
                return 42;
              }
            }));
  }

  private static byte[] copyOfRange(byte[] in, int from, int to) {
    byte[] out = new byte[to - from];
    for (int i = 0; i < to - from; i++) {
      out[i] = in[from + i];
    }
    return out;
  }

  private static void assertEquals(byte[] expected, byte[] actual) {
    assertEquals(Bytes.asList(expected), Bytes.asList(actual));
  }
}