• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.io;
18 
19 import com.google.common.collect.Lists;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.FilterInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Collections;
26 import java.util.List;
27 
28 /**
29  * Test class for {@link MultiInputStream}.
30  *
31  * @author Chris Nokleberg
32  */
33 public class MultiInputStreamTest extends IoTestCase {
34 
testJoin()35   public void testJoin() throws Exception {
36     joinHelper(0);
37     joinHelper(1);
38     joinHelper(0, 0, 0);
39     joinHelper(10, 20);
40     joinHelper(10, 0, 20);
41     joinHelper(0, 10, 20);
42     joinHelper(10, 20, 0);
43     joinHelper(10, 20, 1);
44     joinHelper(1, 1, 1, 1, 1, 1, 1, 1);
45     joinHelper(1, 0, 1, 0, 1, 0, 1, 0);
46   }
47 
testOnlyOneOpen()48   public void testOnlyOneOpen() throws Exception {
49     final InputSupplier<InputStream> supplier = newByteSupplier(0, 50);
50     final int[] counter = new int[1];
51     InputSupplier<InputStream> checker = new InputSupplier<InputStream>() {
52       @Override
53       public InputStream getInput() throws IOException {
54         if (counter[0]++ != 0) {
55           throw new IllegalStateException("More than one supplier open");
56         }
57         return new FilterInputStream(supplier.getInput()) {
58           @Override public void close() throws IOException {
59             super.close();
60             counter[0]--;
61           }
62         };
63       }
64     };
65     @SuppressWarnings("unchecked")
66     byte[] result = ByteStreams.toByteArray(
67         ByteStreams.join(checker, checker, checker));
68     assertEquals(150, result.length);
69   }
70 
joinHelper(Integer... spans)71   private void joinHelper(Integer... spans) throws Exception {
72     List<InputSupplier<InputStream>> suppliers = Lists.newArrayList();
73     int start = 0;
74     for (Integer span : spans) {
75       suppliers.add(newByteSupplier(start, span));
76       start += span;
77     }
78     InputSupplier<InputStream> joined = ByteStreams.join(suppliers);
79     assertTrue(ByteStreams.equal(newByteSupplier(0, start), joined));
80   }
81 
testReadSingleByte()82   public void testReadSingleByte() throws Exception {
83     InputSupplier<InputStream> supplier = newByteSupplier(0, 10);
84     @SuppressWarnings("unchecked")
85     InputSupplier<InputStream> joined = ByteStreams.join(supplier, supplier);
86     assertEquals(20, ByteStreams.length(joined));
87     InputStream in = joined.getInput();
88     assertFalse(in.markSupported());
89     assertEquals(10, in.available());
90     int total = 0;
91     while (in.read() != -1) {
92       total++;
93     }
94     assertEquals(0, in.available());
95     assertEquals(20, total);
96   }
97 
testSkip()98   public void testSkip() throws Exception {
99     MultiInputStream multi = new MultiInputStream(
100         Collections.singleton(new InputSupplier<InputStream>() {
101           @Override
102           public InputStream getInput() {
103             return new ByteArrayInputStream(newPreFilledByteArray(0, 50)) {
104               @Override public long skip(long n) {
105                 return 0;
106               }
107             };
108           }
109         }).iterator());
110     multi.skip(-1);
111     multi.skip(-1);
112     multi.skip(0);
113     ByteStreams.skipFully(multi, 20);
114     assertEquals(20, multi.read());
115   }
116 
newByteSupplier(final int start, final int size)117   private static InputSupplier<InputStream> newByteSupplier(final int start, final int size) {
118     return new InputSupplier<InputStream>() {
119       @Override
120       public InputStream getInput() {
121         return new ByteArrayInputStream(newPreFilledByteArray(start, size));
122       }
123     };
124   }
125 }
126