• 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 java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 
23 /**
24  * @author Charles Fry
25  */
26 public class LimitInputStreamTest extends IoTestCase {
27 
testLimit()28   public void testLimit() throws Exception {
29     byte[] big = newPreFilledByteArray(5);
30     InputStream bin = new ByteArrayInputStream(big);
31     InputStream lin = new LimitInputStream(bin, 2);
32 
33     // also test available
34     lin.mark(2);
35     assertEquals(2, lin.available());
36     int read = lin.read();
37     assertEquals(big[0], read);
38     assertEquals(1, lin.available());
39     read = lin.read();
40     assertEquals(big[1], read);
41     assertEquals(0, lin.available());
42     read = lin.read();
43     assertEquals(-1, read);
44 
45     lin.reset();
46     byte[] small = new byte[5];
47     read = lin.read(small);
48     assertEquals(2, read);
49     assertEquals(big[0], small[0]);
50     assertEquals(big[1], small[1]);
51 
52     lin.reset();
53     read = lin.read(small, 2, 3);
54     assertEquals(2, read);
55     assertEquals(big[0], small[2]);
56     assertEquals(big[1], small[3]);
57   }
58 
testMark()59   public void testMark() throws Exception {
60     byte[] big = newPreFilledByteArray(5);
61     InputStream bin = new ByteArrayInputStream(big);
62     InputStream lin = new LimitInputStream(bin, 2);
63 
64     int read = lin.read();
65     assertEquals(big[0], read);
66     lin.mark(2);
67 
68     read = lin.read();
69     assertEquals(big[1], read);
70     read = lin.read();
71     assertEquals(-1, read);
72 
73     lin.reset();
74     read = lin.read();
75     assertEquals(big[1], read);
76     read = lin.read();
77     assertEquals(-1, read);
78   }
79 
testSkip()80   public void testSkip() throws Exception {
81     byte[] big = newPreFilledByteArray(5);
82     InputStream bin = new ByteArrayInputStream(big);
83     InputStream lin = new LimitInputStream(bin, 2);
84 
85     // also test available
86     lin.mark(2);
87     assertEquals(2, lin.available());
88     lin.skip(1);
89     assertEquals(1, lin.available());
90 
91     lin.reset();
92     assertEquals(2, lin.available());
93     lin.skip(3);
94     assertEquals(0, lin.available());
95   }
96 
97 
testMarkNotSet()98   public void testMarkNotSet() {
99     byte[] big = newPreFilledByteArray(5);
100     InputStream bin = new ByteArrayInputStream(big);
101     InputStream lin = new LimitInputStream(bin, 2);
102 
103     try {
104       lin.reset();
105       fail();
106     } catch (IOException expected) {
107       assertEquals("Mark not set", expected.getMessage());
108     }
109   }
110 
testMarkNotSupported()111   public void testMarkNotSupported() {
112     InputStream lin = new LimitInputStream(new UnmarkableInputStream(), 2);
113 
114     try {
115       lin.reset();
116       fail();
117     } catch (IOException expected) {
118       assertEquals("Mark not supported", expected.getMessage());
119     }
120   }
121 
122   private static class UnmarkableInputStream extends InputStream {
123     @Override
read()124     public int read() throws IOException {
125       return 0;
126     }
127 
128     @Override
markSupported()129     public boolean markSupported() {
130       return false;
131     }
132   }
133 }
134