示例#1
1
  /**
   * Maps blah file with a random offset and checks to see if read from the ByteBuffer gets the
   * right line number
   */
  private static void testRead() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x = 0; x < 1000; x++) {
      try (FileInputStream fis = new FileInputStream(blah)) {
        FileChannel fc = fis.getChannel();

        long offset = generator.nextInt(10000);
        long expectedResult = offset / CHARS_PER_LINE;
        offset = expectedResult * CHARS_PER_LINE;

        MappedByteBuffer b = fc.map(MapMode.READ_ONLY, offset, 100);

        for (int i = 0; i < 4; i++) {
          byte aByte = b.get(i);
          sb.setCharAt(i, (char) aByte);
        }

        int result = Integer.parseInt(sb.toString());
        if (result != expectedResult) {
          err.println("I expected " + expectedResult);
          err.println("I got " + result);
          throw new Exception("Read test failed");
        }
      }
    }
  }
示例#2
1
  /**
   * Maps blah file with a random offset and checks to see if data written out to the file can be
   * read back in
   */
  private static void testWrite() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x = 0; x < 1000; x++) {
      try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
        FileChannel fc = raf.getChannel();

        long offset = generator.nextInt(1000);
        MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100);

        for (int i = 0; i < 4; i++) {
          b.put(i, (byte) ('0' + i));
        }

        for (int i = 0; i < 4; i++) {
          byte aByte = b.get(i);
          sb.setCharAt(i, (char) aByte);
        }
        if (!sb.toString().equals("0123")) throw new Exception("Write test failed");
      }
    }
  }
示例#3
0
  private static void testExceptions(FileChannel fc) throws IOException {
    checkException(fc, null, 0L, fc.size(), NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, -1L, fc.size(), IllegalArgumentException.class);

    checkException(
        fc, null, -1L, fc.size(), IllegalArgumentException.class, NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, 0L, -1L, IllegalArgumentException.class);

    checkException(fc, null, 0L, -1L, IllegalArgumentException.class, NullPointerException.class);

    checkException(
        fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L, IllegalArgumentException.class);

    checkException(
        fc,
        null,
        0L,
        Integer.MAX_VALUE + 1L,
        IllegalArgumentException.class,
        NullPointerException.class);

    checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L, IllegalArgumentException.class);

    checkException(
        fc, null, Long.MAX_VALUE, 1L, IllegalArgumentException.class, NullPointerException.class);
  }
示例#4
0
  private static void testHighOffset() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.setLength(4);

    for (int x = 0; x < 1000; x++) {
      try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
        FileChannel fc = raf.getChannel();
        long offset = 66000;
        MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100);
      }
    }
  }
示例#5
0
 /**
  * Checks that FileChannel map throws one of the expected exceptions when invoked with the given
  * inputs.
  */
 private static void checkException(
     FileChannel fc, MapMode mode, long position, long size, Class<?>... expected)
     throws IOException {
   Exception exc = null;
   try {
     fc.map(mode, position, size);
   } catch (Exception actual) {
     exc = actual;
   }
   if (exc != null) {
     for (Class<?> clazz : expected) {
       if (clazz.isInstance(exc)) {
         return;
       }
     }
   }
   System.err.println("Expected one of");
   for (Class<?> clazz : expected) {
     System.out.println(clazz);
   }
   if (exc == null) {
     throw new RuntimeException("No expection thrown");
   } else {
     throw new RuntimeException("Unexpected exception thrown", exc);
   }
 }
示例#6
0
 /** Tests zero size file mapping */
 private static void testZero() throws Exception {
   try (FileInputStream fis = new FileInputStream(blah)) {
     FileChannel fc = fis.getChannel();
     MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0);
   }
 }
示例#7
0
  /** 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);
    }
  }