2929
3030public class CharSequenceInputStreamTest {
3131
32- private static final String TEST_STRING = "\u00e0 peine arriv \u00e9 s nous entr \u00e2 mes dans sa chambre " ;
32+ private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
3333 private static final String LARGE_TEST_STRING ;
3434
35+ private static final String TEST_STRING = "\u00e0 peine arriv\u00e9 s nous entr\u00e2 mes dans sa chambre" ;
36+
3537 static {
3638 StringBuilder buffer = new StringBuilder ();
3739 for (int i =0 ; i <100 ; i ++) {
3840 buffer .append (TEST_STRING );
3941 }
4042 LARGE_TEST_STRING = buffer .toString ();
4143 }
42-
43- private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
4444
4545 private Random random = new Random ();
4646
47- private void testWithSingleByteRead (String testString , String charsetName ) throws IOException {
48- byte [] bytes = testString .getBytes (charsetName );
49- InputStream in = new CharSequenceInputStream (testString , charsetName , 512 );
50- for (byte b : bytes ) {
51- int read = in .read ();
52- assertTrue ("read " +read +" >=0 " , read >= 0 );
53- assertTrue ("read " +read +" <= 255" , read <= 255 );
54- assertEquals ("Should agree with input" , b , (byte )read );
47+ @ Test
48+ public void testCharsetMismatchInfiniteLoop () throws IOException {
49+ // Input is UTF-8 bytes: 0xE0 0xB2 0xA0
50+ char [] inputChars = new char [] { (char ) 0xE0 , (char ) 0xB2 , (char ) 0xA0 };
51+ // Charset charset = Charset.forName("UTF-8"); // works
52+ Charset charset = Charset .forName ("ASCII" ); // infinite loop
53+ InputStream stream = new CharSequenceInputStream (new String (inputChars ), charset , 512 );
54+ try {
55+ while (stream .read () != -1 ) {
56+ }
57+ } finally {
58+ stream .close ();
5559 }
56- assertEquals (-1 , in .read ());
5760 }
5861
59- private void testWithBufferedRead (String testString , String charsetName ) throws IOException {
60- byte [] expected = testString .getBytes (charsetName );
61- InputStream in = new CharSequenceInputStream (testString , charsetName , 512 );
62- byte [] buffer = new byte [128 ];
63- int offset = 0 ;
64- while (true ) {
65- int bufferOffset = random .nextInt (64 );
66- int bufferLength = random .nextInt (64 );
67- int read = in .read (buffer , bufferOffset , bufferLength );
68- if (read == -1 ) {
69- assertEquals ("EOF: offset should equal length" , expected .length , offset );
70- break ;
71- } else {
72- assertTrue ("Read " +read +" <= " +bufferLength , read <= bufferLength );
73- while (read > 0 ) {
74- assertTrue ("offset " +offset +" < " +expected .length , offset < expected .length );
75- assertEquals ("bytes should agree" , expected [offset ], buffer [bufferOffset ]);
76- offset ++;
77- bufferOffset ++;
78- read --;
79- }
62+ @ Test
63+ public void testIO_356 () throws Exception {
64+ testIO_356 (10 , 10 ); // OK
65+ testIO_356 (10 , 20 ); // OK
66+ testIO_356 (10 , 13 ); // fails
67+ for (int bs = 1 ; bs <= 10 ; bs ++) {
68+ for (int ds = 1 ; ds <= 20 ; ds ++) {
69+ testIO_356 (bs , ds );
8070 }
8171 }
8272 }
8373
84- @ Test
85- public void testUTF8WithSingleByteRead () throws IOException {
86- testWithSingleByteRead (TEST_STRING , "UTF-8" );
74+ private void testIO_356 (int bufferSize , int dataSize ) throws Exception {
75+ CharSequenceInputStream is = new CharSequenceInputStream (ALPHABET , "UTF-8" , bufferSize );
76+
77+ is .mark (dataSize );
78+
79+ byte [] data1 = new byte [dataSize ];
80+ is .read (data1 );
81+
82+ is .reset (); // should allow data to be re-read
83+
84+ byte [] data2 = new byte [dataSize ];
85+ is .read (data2 );
86+
87+ is .close ();
88+ assertArrayEquals ("bufferSize=" +bufferSize +" dataSize=" +dataSize , data1 , data2 ); // data buffers should be identical
8789 }
8890
8991 @ Test
90- public void testLargeUTF8WithSingleByteRead () throws IOException {
91- testWithSingleByteRead (LARGE_TEST_STRING , "UTF-8" );
92+ public void testLargeUTF8WithBufferedRead () throws IOException {
93+ testWithBufferedRead (LARGE_TEST_STRING , "UTF-8" );
9294 }
9395
9496 @ Test
95- public void testUTF8WithBufferedRead () throws IOException {
96- testWithBufferedRead ( TEST_STRING , "UTF-8" );
97+ public void testLargeUTF8WithSingleByteRead () throws IOException {
98+ testWithSingleByteRead ( LARGE_TEST_STRING , "UTF-8" );
9799 }
98100
99101 @ Test
100- public void testLargeUTF8WithBufferedRead () throws IOException {
101- testWithBufferedRead (LARGE_TEST_STRING , "UTF-8" );
102+ public void testMarkReset () throws Exception {
103+ InputStream r = new CharSequenceInputStream ("test" , "UTF-8" );
104+ r .skip (2 );
105+ r .mark (0 );
106+ assertEquals ('s' , r .read ());
107+ assertEquals ('t' , r .read ());
108+ assertEquals (-1 , r .read ());
109+ r .reset ();
110+ assertEquals ('s' , r .read ());
111+ assertEquals ('t' , r .read ());
112+ assertEquals (-1 , r .read ());
113+ r .reset ();
114+ r .reset ();
102115 }
103116
104117 @ Test
105- public void testUTF16WithSingleByteRead () throws IOException {
106- testWithSingleByteRead (TEST_STRING , "UTF-16" );
118+ public void testMarkSupported () throws Exception {
119+ InputStream r = new CharSequenceInputStream ("test" , "UTF-8" );
120+ assertTrue (r .markSupported ());
107121 }
108122
109123 @ Test
@@ -120,21 +134,6 @@ public void testReadZeroEmptyString() throws Exception {
120134 assertEquals (0 , r .read (bytes , 0 , 0 ));
121135 }
122136
123- @ Test
124- public void testCharsetMismatchInfiniteLoop () throws IOException {
125- // Input is UTF-8 bytes: 0xE0 0xB2 0xA0
126- char [] inputChars = new char [] { (char ) 0xE0 , (char ) 0xB2 , (char ) 0xA0 };
127- // Charset charset = Charset.forName("UTF-8"); // works
128- Charset charset = Charset .forName ("ASCII" ); // infinite loop
129- InputStream stream = new CharSequenceInputStream (new String (inputChars ), charset , 512 );
130- try {
131- while (stream .read () != -1 ) {
132- }
133- } finally {
134- stream .close ();
135- }
136- }
137-
138137 @ Test
139138 public void testSkip () throws Exception {
140139 InputStream r = new CharSequenceInputStream ("test" , "UTF-8" );
@@ -146,53 +145,54 @@ public void testSkip() throws Exception {
146145 }
147146
148147 @ Test
149- public void testMarkReset () throws Exception {
150- InputStream r = new CharSequenceInputStream ("test" , "UTF-8" );
151- r .skip (2 );
152- r .mark (0 );
153- assertEquals ('s' , r .read ());
154- assertEquals ('t' , r .read ());
155- assertEquals (-1 , r .read ());
156- r .reset ();
157- assertEquals ('s' , r .read ());
158- assertEquals ('t' , r .read ());
159- assertEquals (-1 , r .read ());
160- r .reset ();
161- r .reset ();
148+ public void testUTF16WithSingleByteRead () throws IOException {
149+ testWithSingleByteRead (TEST_STRING , "UTF-16" );
162150 }
163151
164152 @ Test
165- public void testMarkSupported () throws Exception {
166- InputStream r = new CharSequenceInputStream ("test" , "UTF-8" );
167- assertTrue (r .markSupported ());
153+ public void testUTF8WithBufferedRead () throws IOException {
154+ testWithBufferedRead (TEST_STRING , "UTF-8" );
168155 }
169156
170- private void testIO_356 (int bufferSize , int dataSize ) throws Exception {
171- CharSequenceInputStream is = new CharSequenceInputStream (ALPHABET , "UTF-8" , bufferSize );
172-
173- is .mark (dataSize );
174-
175- byte [] data1 = new byte [dataSize ];
176- is .read (data1 );
177-
178- is .reset (); // should allow data to be re-read
179-
180- byte [] data2 = new byte [dataSize ];
181- is .read (data2 );
157+ @ Test
158+ public void testUTF8WithSingleByteRead () throws IOException {
159+ testWithSingleByteRead (TEST_STRING , "UTF-8" );
160+ }
182161
183- is .close ();
184- assertArrayEquals ("bufferSize=" +bufferSize +" dataSize=" +dataSize , data1 , data2 ); // data buffers should be identical
162+ private void testWithBufferedRead (String testString , String charsetName ) throws IOException {
163+ byte [] expected = testString .getBytes (charsetName );
164+ InputStream in = new CharSequenceInputStream (testString , charsetName , 512 );
165+ byte [] buffer = new byte [128 ];
166+ int offset = 0 ;
167+ while (true ) {
168+ int bufferOffset = random .nextInt (64 );
169+ int bufferLength = random .nextInt (64 );
170+ int read = in .read (buffer , bufferOffset , bufferLength );
171+ if (read == -1 ) {
172+ assertEquals ("EOF: offset should equal length" , expected .length , offset );
173+ break ;
174+ } else {
175+ assertTrue ("Read " +read +" <= " +bufferLength , read <= bufferLength );
176+ while (read > 0 ) {
177+ assertTrue ("offset " +offset +" < " +expected .length , offset < expected .length );
178+ assertEquals ("bytes should agree" , expected [offset ], buffer [bufferOffset ]);
179+ offset ++;
180+ bufferOffset ++;
181+ read --;
182+ }
183+ }
184+ }
185185 }
186186
187- @ Test
188- public void testIO_356 () throws Exception {
189- testIO_356 (10 , 10 ); // OK
190- testIO_356 (10 , 20 ); // OK
191- testIO_356 (10 , 13 ); // fails
192- for (int bs = 1 ; bs <= 10 ; bs ++) {
193- for (int ds = 1 ; ds <= 20 ; ds ++) {
194- testIO_356 (bs , ds );
195- }
187+ private void testWithSingleByteRead (String testString , String charsetName ) throws IOException {
188+ byte [] bytes = testString .getBytes (charsetName );
189+ InputStream in = new CharSequenceInputStream (testString , charsetName , 512 );
190+ for (byte b : bytes ) {
191+ int read = in .read ();
192+ assertTrue ("read " +read +" >=0 " , read >= 0 );
193+ assertTrue ("read " +read +" <= 255" , read <= 255 );
194+ assertEquals ("Should agree with input" , b , (byte )read );
196195 }
196+ assertEquals (-1 , in .read ());
197197 }
198198}
0 commit comments