Beispiel #1
0
  // computes simple hash of the given file
  static int computeHash(Path file) throws IOException {
    int h = 0;

    try (InputStream in = newInputStream(file)) {
      byte[] buf = new byte[1024];
      int n;
      do {
        n = in.read(buf);
        for (int i = 0; i < n; i++) {
          h = 31 * h + (buf[i] & 0xff);
        }
      } while (n > 0);
    }
    return h;
  }
Beispiel #2
0
  static void testCopyInputStreamToFile(int size) throws IOException {
    Path tmpdir = createTempDirectory("blah");
    Path source = tmpdir.resolve("source");
    Path target = tmpdir.resolve("target");
    try {
      boolean testReplaceExisting = rand.nextBoolean();

      // create source file
      byte[] b = new byte[size];
      rand.nextBytes(b);
      write(source, b);

      // target file might already exist
      if (testReplaceExisting && rand.nextBoolean()) {
        write(target, new byte[rand.nextInt(512)]);
      }

      // copy from stream to file
      InputStream in = new FileInputStream(source.toFile());
      try {
        long n;
        if (testReplaceExisting) {
          n = copy(in, target, StandardCopyOption.REPLACE_EXISTING);
        } else {
          n = copy(in, target);
        }
        assertTrue(in.read() == -1); // EOF
        assertTrue(n == size);
        assertTrue(size(target) == size);
      } finally {
        in.close();
      }

      // check file
      byte[] read = readAllBytes(target);
      assertTrue(Arrays.equals(read, b));

    } finally {
      deleteIfExists(source);
      deleteIfExists(target);
      delete(tmpdir);
    }
  }