📄 testcompoundfile.java
字号:
// This read primes the buffer in IndexInput byte b = in.readByte(); // Close the file in.close(); // ERROR: this call should fail, but succeeds because the buffer // is still filled b = in.readByte(); // ERROR: this call should fail, but succeeds for some reason as well in.seek(1099); try { // OK: this call correctly fails. We are now past the 1024 internal // buffer, so an actual IO is attempted, which fails b = in.readByte(); fail("expected readByte() to throw exception"); } catch (IOException e) { // expected exception } } static boolean isCSIndexInput(IndexInput is) { return is instanceof CompoundFileReader.CSIndexInput; } static boolean isCSIndexInputOpen(IndexInput is) throws IOException { if (isCSIndexInput(is)) { CompoundFileReader.CSIndexInput cis = (CompoundFileReader.CSIndexInput) is; return _TestHelper.isFSIndexInputOpen(cis.base); } else { return false; } } public void testClonedStreamsClosing() throws IOException { setUp_2(); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); // basic clone IndexInput expected = dir.openInput("f11"); // this test only works for FSIndexInput if (_TestHelper.isFSIndexInput(expected)) { assertTrue(_TestHelper.isFSIndexInputOpen(expected)); IndexInput one = cr.openInput("f11"); assertTrue(isCSIndexInputOpen(one)); IndexInput two = (IndexInput) one.clone(); assertTrue(isCSIndexInputOpen(two)); assertSameStreams("basic clone one", expected, one); expected.seek(0); assertSameStreams("basic clone two", expected, two); // Now close the first stream one.close(); assertTrue("Only close when cr is closed", isCSIndexInputOpen(one)); // The following should really fail since we couldn't expect to // access a file once close has been called on it (regardless of // buffering and/or clone magic) expected.seek(0); two.seek(0); assertSameStreams("basic clone two/2", expected, two); // Now close the compound reader cr.close(); assertFalse("Now closed one", isCSIndexInputOpen(one)); assertFalse("Now closed two", isCSIndexInputOpen(two)); // The following may also fail since the compound stream is closed expected.seek(0); two.seek(0); //assertSameStreams("basic clone two/3", expected, two); // Now close the second clone two.close(); expected.seek(0); two.seek(0); //assertSameStreams("basic clone two/4", expected, two); } expected.close(); } /** This test opens two files from a compound stream and verifies that * their file positions are independent of each other. */ public void testRandomAccess() throws IOException { setUp_2(); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); // Open two files IndexInput e1 = dir.openInput("f11"); IndexInput e2 = dir.openInput("f3"); IndexInput a1 = cr.openInput("f11"); IndexInput a2 = dir.openInput("f3"); // Seek the first pair e1.seek(100); a1.seek(100); assertEquals(100, e1.getFilePointer()); assertEquals(100, a1.getFilePointer()); byte be1 = e1.readByte(); byte ba1 = a1.readByte(); assertEquals(be1, ba1); // Now seek the second pair e2.seek(1027); a2.seek(1027); assertEquals(1027, e2.getFilePointer()); assertEquals(1027, a2.getFilePointer()); byte be2 = e2.readByte(); byte ba2 = a2.readByte(); assertEquals(be2, ba2); // Now make sure the first one didn't move assertEquals(101, e1.getFilePointer()); assertEquals(101, a1.getFilePointer()); be1 = e1.readByte(); ba1 = a1.readByte(); assertEquals(be1, ba1); // Now more the first one again, past the buffer length e1.seek(1910); a1.seek(1910); assertEquals(1910, e1.getFilePointer()); assertEquals(1910, a1.getFilePointer()); be1 = e1.readByte(); ba1 = a1.readByte(); assertEquals(be1, ba1); // Now make sure the second set didn't move assertEquals(1028, e2.getFilePointer()); assertEquals(1028, a2.getFilePointer()); be2 = e2.readByte(); ba2 = a2.readByte(); assertEquals(be2, ba2); // Move the second set back, again cross the buffer size e2.seek(17); a2.seek(17); assertEquals(17, e2.getFilePointer()); assertEquals(17, a2.getFilePointer()); be2 = e2.readByte(); ba2 = a2.readByte(); assertEquals(be2, ba2); // Finally, make sure the first set didn't move // Now make sure the first one didn't move assertEquals(1911, e1.getFilePointer()); assertEquals(1911, a1.getFilePointer()); be1 = e1.readByte(); ba1 = a1.readByte(); assertEquals(be1, ba1); e1.close(); e2.close(); a1.close(); a2.close(); cr.close(); } /** This test opens two files from a compound stream and verifies that * their file positions are independent of each other. */ public void testRandomAccessClones() throws IOException { setUp_2(); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); // Open two files IndexInput e1 = cr.openInput("f11"); IndexInput e2 = cr.openInput("f3"); IndexInput a1 = (IndexInput) e1.clone(); IndexInput a2 = (IndexInput) e2.clone(); // Seek the first pair e1.seek(100); a1.seek(100); assertEquals(100, e1.getFilePointer()); assertEquals(100, a1.getFilePointer()); byte be1 = e1.readByte(); byte ba1 = a1.readByte(); assertEquals(be1, ba1); // Now seek the second pair e2.seek(1027); a2.seek(1027); assertEquals(1027, e2.getFilePointer()); assertEquals(1027, a2.getFilePointer()); byte be2 = e2.readByte(); byte ba2 = a2.readByte(); assertEquals(be2, ba2); // Now make sure the first one didn't move assertEquals(101, e1.getFilePointer()); assertEquals(101, a1.getFilePointer()); be1 = e1.readByte(); ba1 = a1.readByte(); assertEquals(be1, ba1); // Now more the first one again, past the buffer length e1.seek(1910); a1.seek(1910); assertEquals(1910, e1.getFilePointer()); assertEquals(1910, a1.getFilePointer()); be1 = e1.readByte(); ba1 = a1.readByte(); assertEquals(be1, ba1); // Now make sure the second set didn't move assertEquals(1028, e2.getFilePointer()); assertEquals(1028, a2.getFilePointer()); be2 = e2.readByte(); ba2 = a2.readByte(); assertEquals(be2, ba2); // Move the second set back, again cross the buffer size e2.seek(17); a2.seek(17); assertEquals(17, e2.getFilePointer()); assertEquals(17, a2.getFilePointer()); be2 = e2.readByte(); ba2 = a2.readByte(); assertEquals(be2, ba2); // Finally, make sure the first set didn't move // Now make sure the first one didn't move assertEquals(1911, e1.getFilePointer()); assertEquals(1911, a1.getFilePointer()); be1 = e1.readByte(); ba1 = a1.readByte(); assertEquals(be1, ba1); e1.close(); e2.close(); a1.close(); a2.close(); cr.close(); } public void testFileNotFound() throws IOException { setUp_2(); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); // Open two files try { IndexInput e1 = cr.openInput("bogus"); fail("File not found"); } catch (IOException e) { /* success */ //System.out.println("SUCCESS: File Not Found: " + e); } cr.close(); } public void testReadPastEOF() throws IOException { setUp_2(); CompoundFileReader cr = new CompoundFileReader(dir, "f.comp"); IndexInput is = cr.openInput("f2"); is.seek(is.length() - 10); byte b[] = new byte[100]; is.readBytes(b, 0, 10); try { byte test = is.readByte(); fail("Single byte read past end of file"); } catch (IOException e) { /* success */ //System.out.println("SUCCESS: single byte read past end of file: " + e); } is.seek(is.length() - 10); try { is.readBytes(b, 0, 50); fail("Block read past end of file"); } catch (IOException e) { /* success */ //System.out.println("SUCCESS: block read past end of file: " + e); } is.close(); cr.close(); } /** This test that writes larger than the size of the buffer output * will correctly increment the file pointer. */ public void testLargeWrites() throws IOException { IndexOutput os = dir.createOutput("testBufferStart.txt"); byte[] largeBuf = new byte[2048]; for (int i=0; i<largeBuf.length; i++) { largeBuf[i] = (byte) (Math.random() * 256); } long currentPos = os.getFilePointer(); os.writeBytes(largeBuf, largeBuf.length); try { assertEquals(currentPos + largeBuf.length, os.getFilePointer()); } finally { os.close(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -