• 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 ByteSource source = newByteSource(0, 50);
50     final int[] counter = new int[1];
51     ByteSource checker = new ByteSource() {
52       @Override
53       public InputStream openStream() throws IOException {
54         if (counter[0]++ != 0) {
55           throw new IllegalStateException("More than one source open");
56         }
57         return new FilterInputStream(source.openStream()) {
58           @Override public void close() throws IOException {
59             super.close();
60             counter[0]--;
61           }
62         };
63       }
64     };
65     byte[] result = ByteSource.concat(checker, checker, checker).read();
66     assertEquals(150, result.length);
67   }
68 
joinHelper(Integer... spans)69   private void joinHelper(Integer... spans) throws Exception {
70     List<ByteSource> sources = Lists.newArrayList();
71     int start = 0;
72     for (Integer span : spans) {
73       sources.add(newByteSource(start, span));
74       start += span;
75     }
76     ByteSource joined = ByteSource.concat(sources);
77     assertTrue(newByteSource(0, start).contentEquals(joined));
78   }
79 
testReadSingleByte()80   public void testReadSingleByte() throws Exception {
81     ByteSource source = newByteSource(0, 10);
82     ByteSource joined = ByteSource.concat(source, source);
83     assertEquals(20, joined.size());
84     InputStream in = joined.openStream();
85     assertFalse(in.markSupported());
86     assertEquals(10, in.available());
87     int total = 0;
88     while (in.read() != -1) {
89       total++;
90     }
91     assertEquals(0, in.available());
92     assertEquals(20, total);
93   }
94 
testSkip()95   public void testSkip() throws Exception {
96     MultiInputStream multi = new MultiInputStream(
97         Collections.singleton(new ByteSource() {
98           @Override
99           public InputStream openStream() {
100             return new ByteArrayInputStream(newPreFilledByteArray(0, 50)) {
101               @Override public long skip(long n) {
102                 return 0;
103               }
104             };
105           }
106         }).iterator());
107     multi.skip(-1);
108     multi.skip(-1);
109     multi.skip(0);
110     ByteStreams.skipFully(multi, 20);
111     assertEquals(20, multi.read());
112   }
113 
newByteSource(final int start, final int size)114   private static ByteSource newByteSource(final int start, final int size) {
115     return new ByteSource() {
116       @Override
117       public InputStream openStream() {
118         return new ByteArrayInputStream(newPreFilledByteArray(start, size));
119       }
120     };
121   }
122 }
123