示例#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
 /**
  * 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);
   }
 }
示例#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
 /** 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);
   }
 }