1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.io; 19 20import java.io.BufferedInputStream; 21import java.io.ByteArrayInputStream; 22import java.io.File; 23import java.io.FileInputStream; 24import java.io.FileOutputStream; 25import java.io.IOException; 26import java.io.InputStream; 27import java.io.InputStreamReader; 28import java.io.OutputStream; 29import junit.framework.TestCase; 30import tests.support.Support_PlatformFile; 31 32public class BufferedInputStreamTest extends TestCase { 33 34 public String fileName; 35 36 private BufferedInputStream is; 37 38 private FileInputStream isFile; 39 40 byte[] ibuf = new byte[4096]; 41 42 public String fileString = "Test_All_Tests\nTest_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n"; 43 44 /* 45 * @tests java.io.BufferedInputStream(InputStream) 46 */ 47 public void test_ConstructorLjava_io_InputStream() { 48 try { 49 BufferedInputStream str = new BufferedInputStream(null); 50 str.read(); 51 fail("Expected an IOException"); 52 } catch (IOException e) { 53 // Expected 54 } 55 } 56 57 /* 58 * @tests java.io.BufferedInputStream(InputStream) 59 */ 60 public void test_ConstructorLjava_io_InputStreamI() throws IOException { 61 try { 62 BufferedInputStream str = new BufferedInputStream(null, 1); 63 str.read(); 64 fail("Expected an IOException"); 65 } catch (IOException e) { 66 // Expected 67 } 68 69 // Test for method java.io.BufferedInputStream(java.io.InputStream, int) 70 71 // Create buffer with exact size of file 72 is = new BufferedInputStream(isFile, this.fileString.length()); 73 // Ensure buffer gets filled by evaluating one read 74 is.read(); 75 // Close underlying FileInputStream, all but 1 buffered bytes should 76 // still be available. 77 isFile.close(); 78 // Read the remaining buffered characters, no IOException should 79 // occur. 80 is.skip(this.fileString.length() - 2); 81 is.read(); 82 try { 83 // is.read should now throw an exception because it will have to 84 // be filled. 85 is.read(); 86 fail("Exception should have been triggered by read()"); 87 } catch (IOException e) { 88 // Expected 89 } 90 91 // regression test for harmony-2407 92 new MockBufferedInputStream(null); 93 assertNotNull(MockBufferedInputStream.buf); 94 MockBufferedInputStream.buf = null; 95 new MockBufferedInputStream(null, 100); 96 assertNotNull(MockBufferedInputStream.buf); 97 } 98 99 static class MockBufferedInputStream extends BufferedInputStream { 100 static byte[] buf; 101 102 MockBufferedInputStream(InputStream is) throws IOException { 103 super(is); 104 buf = super.buf; 105 } 106 107 MockBufferedInputStream(InputStream is, int size) throws IOException { 108 super(is, size); 109 buf = super.buf; 110 } 111 } 112 113 /** 114 * @tests java.io.BufferedInputStream#available() 115 */ 116 public void test_available() throws IOException { 117 assertTrue("Returned incorrect number of available bytes", is 118 .available() == fileString.length()); 119 120 // Test that a closed stream throws an IOE for available() 121 BufferedInputStream bis = new BufferedInputStream( 122 new ByteArrayInputStream(new byte[] { 'h', 'e', 'l', 'l', 'o', 123 ' ', 't', 'i', 'm' })); 124 int available = bis.available(); 125 bis.close(); 126 assertTrue(available != 0); 127 128 try { 129 bis.available(); 130 fail("Expected test to throw IOE."); 131 } catch (IOException ex) { 132 // expected 133 } 134 } 135 136 /** 137 * @tests java.io.BufferedInputStream#close() 138 */ 139 public void test_close() throws IOException { 140 new BufferedInputStream(isFile).close(); 141 142 // regression for HARMONY-667 143 BufferedInputStream buf = new BufferedInputStream(null, 5); 144 buf.close(); 145 146 InputStream in = new InputStream() { 147 Object lock = new Object(); 148 149 @Override 150 public int read() { 151 return 1; 152 } 153 154 @Override 155 public int read(byte[] buf, int offset, int length) { 156 synchronized (lock) { 157 try { 158 lock.wait(3000); 159 } catch (InterruptedException e) { 160 // Ignore 161 } 162 } 163 return 1; 164 } 165 166 @Override 167 public void close() { 168 synchronized (lock) { 169 lock.notifyAll(); 170 } 171 } 172 }; 173 final BufferedInputStream bufin = new BufferedInputStream(in); 174 Thread thread = new Thread(new Runnable() { 175 public void run() { 176 try { 177 Thread.sleep(1000); 178 bufin.close(); 179 } catch (Exception e) { 180 // Ignored 181 } 182 } 183 }); 184 thread.start(); 185 try { 186 bufin.read(new byte[100], 0, 99); 187 fail("Should throw IOException"); 188 } catch (IOException e) { 189 // Expected 190 } 191 } 192 193 /** 194 * @tests java.io.BufferedInputStream#mark(int) 195 */ 196 public void test_markI() throws IOException { 197 byte[] buf1 = new byte[100]; 198 byte[] buf2 = new byte[100]; 199 is.skip(3000); 200 is.mark(1000); 201 is.read(buf1, 0, buf1.length); 202 is.reset(); 203 is.read(buf2, 0, buf2.length); 204 is.reset(); 205 assertTrue("Failed to mark correct position", new String(buf1, 0, 206 buf1.length).equals(new String(buf2, 0, buf2.length))); 207 208 byte[] bytes = new byte[256]; 209 for (int i = 0; i < 256; i++) { 210 bytes[i] = (byte) i; 211 } 212 InputStream in = new BufferedInputStream( 213 new ByteArrayInputStream(bytes), 12); 214 in.skip(6); 215 in.mark(14); 216 in.read(new byte[14], 0, 14); 217 in.reset(); 218 assertTrue("Wrong bytes", in.read() == 6 && in.read() == 7); 219 220 in = new BufferedInputStream(new ByteArrayInputStream(bytes), 12); 221 in.skip(6); 222 in.mark(8); 223 in.skip(7); 224 in.reset(); 225 assertTrue("Wrong bytes 2", in.read() == 6 && in.read() == 7); 226 227 BufferedInputStream buf = new BufferedInputStream( 228 new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4 }), 2); 229 buf.mark(3); 230 bytes = new byte[3]; 231 int result = buf.read(bytes); 232 assertEquals(3, result); 233 assertEquals("Assert 0:", 0, bytes[0]); 234 assertEquals("Assert 1:", 1, bytes[1]); 235 assertEquals("Assert 2:", 2, bytes[2]); 236 assertEquals("Assert 3:", 3, buf.read()); 237 238 buf = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0, 239 1, 2, 3, 4 }), 2); 240 buf.mark(3); 241 bytes = new byte[4]; 242 result = buf.read(bytes); 243 assertEquals(4, result); 244 assertEquals("Assert 4:", 0, bytes[0]); 245 assertEquals("Assert 5:", 1, bytes[1]); 246 assertEquals("Assert 6:", 2, bytes[2]); 247 assertEquals("Assert 7:", 3, bytes[3]); 248 assertEquals("Assert 8:", 4, buf.read()); 249 assertEquals("Assert 9:", -1, buf.read()); 250 251 buf = new BufferedInputStream(new ByteArrayInputStream(new byte[] { 0, 252 1, 2, 3, 4 }), 2); 253 buf.mark(Integer.MAX_VALUE); 254 buf.read(); 255 buf.close(); 256 } 257 258 /** 259 * @tests java.io.BufferedInputStream#markSupported() 260 */ 261 public void test_markSupported() { 262 assertTrue("markSupported returned incorrect value", is.markSupported()); 263 } 264 265 /** 266 * @tests java.io.BufferedInputStream#read() 267 */ 268 public void test_read() throws IOException { 269 InputStreamReader isr = new InputStreamReader(is); 270 int c = isr.read(); 271 assertTrue("read returned incorrect char", c == fileString.charAt(0)); 272 273 byte[] bytes = new byte[256]; 274 for (int i = 0; i < 256; i++) { 275 bytes[i] = (byte) i; 276 } 277 InputStream in = new BufferedInputStream( 278 new ByteArrayInputStream(bytes), 12); 279 assertEquals("Wrong initial byte", 0, in.read()); // Fill the 280 // buffer 281 byte[] buf = new byte[14]; 282 in.read(buf, 0, 14); // Read greater than the buffer 283 assertTrue("Wrong block read data", new String(buf, 0, 14) 284 .equals(new String(bytes, 1, 14))); 285 assertEquals("Wrong bytes", 15, in.read()); // Check next byte 286 } 287 288 /** 289 * @tests java.io.BufferedInputStream#read(byte[], int, int) 290 */ 291 public void test_read$BII_Exception() throws IOException { 292 BufferedInputStream bis = new BufferedInputStream(null); 293 try { 294 bis.read(null, -1, -1); 295 fail("should throw NullPointerException"); 296 } catch (NullPointerException e) { 297 // expected 298 } 299 300 try { 301 bis.read(new byte[0], -1, -1); 302 fail("should throw IndexOutOfBoundsException"); 303 } catch (IndexOutOfBoundsException e) { 304 // expected 305 } 306 307 try { 308 bis.read(new byte[0], 1, -1); 309 fail("should throw IndexOutOfBoundsException"); 310 } catch (IndexOutOfBoundsException e) { 311 // expected 312 } 313 314 try { 315 bis.read(new byte[0], 1, 1); 316 fail("should throw IndexOutOfBoundsException"); 317 } catch (IndexOutOfBoundsException e) { 318 // expected 319 } 320 321 bis.close(); 322 323 try { 324 bis.read(null, -1, -1); 325 fail("should throw IOException"); 326 } catch (IOException e) { 327 // expected 328 } 329 } 330 331 /** 332 * @tests java.io.BufferedInputStream#read(byte[], int, int) 333 */ 334 public void test_read$BII() throws IOException { 335 byte[] buf1 = new byte[100]; 336 is.skip(3000); 337 is.mark(1000); 338 is.read(buf1, 0, buf1.length); 339 assertTrue("Failed to read correct data", new String(buf1, 0, 340 buf1.length).equals(fileString.substring(3000, 3100))); 341 342 BufferedInputStream bufin = new BufferedInputStream(new InputStream() { 343 int size = 2, pos = 0; 344 345 byte[] contents = new byte[size]; 346 347 @Override 348 public int read() throws IOException { 349 if (pos >= size) { 350 throw new IOException("Read past end of data"); 351 } 352 return contents[pos++]; 353 } 354 355 @Override 356 public int read(byte[] buf, int off, int len) throws IOException { 357 if (pos >= size) { 358 throw new IOException("Read past end of data"); 359 } 360 int toRead = len; 361 if (toRead > available()) { 362 toRead = available(); 363 } 364 System.arraycopy(contents, pos, buf, off, toRead); 365 pos += toRead; 366 return toRead; 367 } 368 369 @Override 370 public int available() { 371 return size - pos; 372 } 373 }); 374 bufin.read(); 375 int result = bufin.read(new byte[2], 0, 2); 376 assertTrue("Incorrect result: " + result, result == 1); 377 } 378 379 /** 380 * @tests java.io.BufferedInputStream#reset() 381 */ 382 public void test_reset() throws IOException { 383 byte[] buf1 = new byte[10]; 384 byte[] buf2 = new byte[10]; 385 is.mark(2000); 386 is.read(buf1, 0, 10); 387 is.reset(); 388 is.read(buf2, 0, 10); 389 is.reset(); 390 assertTrue("Reset failed", new String(buf1, 0, buf1.length) 391 .equals(new String(buf2, 0, buf2.length))); 392 393 BufferedInputStream bIn = new BufferedInputStream( 394 new ByteArrayInputStream("1234567890".getBytes())); 395 bIn.mark(10); 396 for (int i = 0; i < 11; i++) { 397 bIn.read(); 398 } 399 bIn.reset(); 400 } 401 402 /** 403 * @tests java.io.BufferedInputStream#reset() 404 */ 405 public void test_reset_Exception() throws IOException { 406 BufferedInputStream bis = new BufferedInputStream(null); 407 408 // throws IOException with message "Mark has been invalidated" 409 try { 410 bis.reset(); 411 fail("should throw IOException"); 412 } catch (IOException e) { 413 // expected 414 } 415 416 // does not throw IOException 417 bis.mark(1); 418 bis.reset(); 419 420 bis.close(); 421 422 // throws IOException with message "stream is closed" 423 try { 424 bis.reset(); 425 fail("should throw IOException"); 426 } catch (IOException e) { 427 // expected 428 } 429 } 430 431 /** 432 * @tests java.io.BufferedInputStream#reset() 433 */ 434 public void test_reset_scenario1() throws IOException { 435 byte[] input = "12345678900".getBytes(); 436 BufferedInputStream buffis = new BufferedInputStream( 437 new ByteArrayInputStream(input)); 438 buffis.read(); 439 buffis.mark(5); 440 buffis.skip(5); 441 buffis.reset(); 442 } 443 444 /** 445 * @tests java.io.BufferedInputStream#reset() 446 */ 447 public void test_reset_scenario2() throws IOException { 448 byte[] input = "12345678900".getBytes(); 449 BufferedInputStream buffis = new BufferedInputStream( 450 new ByteArrayInputStream(input)); 451 buffis.mark(5); 452 buffis.skip(6); 453 buffis.reset(); 454 } 455 456 /** 457 * @tests java.io.BufferedInputStream#skip(long) 458 */ 459 public void test_skipJ() throws IOException { 460 byte[] buf1 = new byte[10]; 461 is.mark(2000); 462 is.skip(1000); 463 is.read(buf1, 0, buf1.length); 464 is.reset(); 465 assertTrue("Failed to skip to correct position", new String(buf1, 0, 466 buf1.length).equals(fileString.substring(1000, 1010))); 467 468 // regression for HARMONY-667 469 try { 470 BufferedInputStream buf = new BufferedInputStream(null, 5); 471 buf.skip(10); 472 fail("Should throw IOException"); 473 } catch (IOException e) { 474 // Expected 475 } 476 } 477 478 /** 479 * @tests java.io.BufferedInputStream#skip(long) 480 */ 481 public void test_skip_NullInputStream() throws IOException { 482 BufferedInputStream buf = new BufferedInputStream(null, 5); 483 assertEquals(0, buf.skip(0)); 484 } 485 486 /** 487 * Sets up the fixture, for example, open a network connection. This method 488 * is called before a test is executed. 489 */ 490 @Override 491 protected void setUp() throws IOException { 492 fileName = System.getProperty("user.dir"); 493 String separator = System.getProperty("file.separator"); 494 if (fileName.charAt(fileName.length() - 1) == separator.charAt(0)) { 495 fileName = Support_PlatformFile.getNewPlatformFile(fileName, 496 "input.tst"); 497 } else { 498 fileName = Support_PlatformFile.getNewPlatformFile(fileName 499 + separator, "input.tst"); 500 } 501 OutputStream fos = new FileOutputStream(fileName); 502 fos.write(fileString.getBytes()); 503 fos.close(); 504 isFile = new FileInputStream(fileName); 505 is = new BufferedInputStream(isFile); 506 } 507 508 /** 509 * Tears down the fixture, for example, close a network connection. This 510 * method is called after a test is executed. 511 */ 512 @Override 513 protected void tearDown() { 514 try { 515 is.close(); 516 } catch (Exception e) { 517 } 518 try { 519 File f = new File(fileName); 520 f.delete(); 521 } catch (Exception e) { 522 } 523 } 524} 525