/** * Creates file blah: 0000 0001 0002 0003 . . . 3999 * * <p>Blah extends beyond a single page of memory so that the ability to index into a file of * multiple pages is tested. */ private static void initTestFile(File blah) throws Exception { try (BufferedWriter writer = Files.newBufferedWriter(blah.toPath(), ISO_8859_1)) { for (int i = 0; i < 4000; i++) { String number = new Integer(i).toString(); for (int h = 0; h < 4 - number.length(); h++) writer.write("0"); writer.write("" + i); writer.newLine(); } } }
/** Test exceptions specified by map method */ private static void testExceptions() throws Exception { // check exceptions when channel opened for read access try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) { testExceptions(fc); checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), NonWritableChannelException.class); checkException( fc, MapMode.READ_WRITE, -1L, fc.size(), NonWritableChannelException.class, IllegalArgumentException.class); checkException( fc, MapMode.READ_WRITE, 0L, -1L, NonWritableChannelException.class, IllegalArgumentException.class); checkException(fc, MapMode.PRIVATE, 0L, fc.size(), NonWritableChannelException.class); checkException( fc, MapMode.PRIVATE, -1L, fc.size(), NonWritableChannelException.class, IllegalArgumentException.class); checkException( fc, MapMode.PRIVATE, 0L, -1L, NonWritableChannelException.class, IllegalArgumentException.class); } // check exceptions when channel opened for write access try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) { testExceptions(fc); checkException(fc, MapMode.READ_ONLY, 0L, fc.size(), NonReadableChannelException.class); checkException( fc, MapMode.READ_ONLY, -1L, fc.size(), NonReadableChannelException.class, IllegalArgumentException.class); /* * implementation/spec mismatch, these tests disabled for now */ // checkException(fc, MapMode.READ_WRITE, 0L, fc.size(), // NonWritableChannelException.class); // checkException(fc, MapMode.PRIVATE, 0L, fc.size(), // NonWritableChannelException.class); } // check exceptions when channel opened for read and write access try (FileChannel fc = FileChannel.open(blah.toPath(), READ, WRITE)) { testExceptions(fc); } }