Ejemplo n.º 1
1
  /** Test copy from an input stream to a file */
  static void testCopyInputStreamToFile() throws IOException {
    testCopyInputStreamToFile(0);
    for (int i = 0; i < 100; i++) {
      testCopyInputStreamToFile(rand.nextInt(32000));
    }

    // FileAlreadyExistsException
    Path target = createTempFile("blah", null);
    try {
      InputStream in = new ByteArrayInputStream(new byte[0]);
      try {
        copy(in, target);
        throw new RuntimeException("FileAlreadyExistsException expected");
      } catch (FileAlreadyExistsException ignore) {
      }
    } finally {
      delete(target);
    }
    Path tmpdir = createTempDirectory("blah");
    try {
      if (TestUtil.supportsLinks(tmpdir)) {
        Path link = createSymbolicLink(tmpdir.resolve("link"), tmpdir.resolve("target"));
        try {
          InputStream in = new ByteArrayInputStream(new byte[0]);
          try {
            copy(in, link);
            throw new RuntimeException("FileAlreadyExistsException expected");
          } catch (FileAlreadyExistsException ignore) {
          }
        } finally {
          delete(link);
        }
      }
    } finally {
      delete(tmpdir);
    }

    // nulls
    try {
      copy((InputStream) null, target);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException ignore) {
    }
    try {
      copy(new ByteArrayInputStream(new byte[0]), (Path) null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException ignore) {
    }
  }
Ejemplo n.º 2
0
 // create file of random size in given directory
 static Path createSourceFile(Path dir) throws IOException {
   String name = "source" + Integer.toString(rand.nextInt());
   Path file = dir.resolve(name);
   createFile(file);
   byte[] bytes = new byte[rand.nextInt(128 * 1024)];
   rand.nextBytes(bytes);
   try (OutputStream out = newOutputStream(file)) {
     out.write(bytes);
   }
   randomizeAttributes(file);
   return file;
 }
Ejemplo n.º 3
0
  // "randomize" the file attributes of the given file.
  static void randomizeAttributes(Path file) throws IOException {
    String os = System.getProperty("os.name");
    boolean isWindows = os.startsWith("Windows");
    boolean isUnix = os.equals("SunOS") || os.equals("Linux");
    boolean isDirectory = isDirectory(file, NOFOLLOW_LINKS);

    if (isUnix) {
      Set<PosixFilePermission> perms = getPosixFilePermissions(file, NOFOLLOW_LINKS);
      PosixFilePermission[] toChange = {
        PosixFilePermission.GROUP_READ,
        PosixFilePermission.GROUP_WRITE,
        PosixFilePermission.GROUP_EXECUTE,
        PosixFilePermission.OTHERS_READ,
        PosixFilePermission.OTHERS_WRITE,
        PosixFilePermission.OTHERS_EXECUTE
      };
      for (PosixFilePermission perm : toChange) {
        if (heads()) {
          perms.add(perm);
        } else {
          perms.remove(perm);
        }
      }
      setPosixFilePermissions(file, perms);
    }

    if (isWindows) {
      DosFileAttributeView view =
          getFileAttributeView(file, DosFileAttributeView.class, NOFOLLOW_LINKS);
      // only set or unset the hidden attribute
      view.setHidden(heads());
    }

    boolean addUserDefinedFileAttributes =
        heads() && getFileStore(file).supportsFileAttributeView("xattr");

    // remove this when copying a direcory copies its named streams
    if (isWindows && isDirectory) addUserDefinedFileAttributes = false;

    if (addUserDefinedFileAttributes) {
      UserDefinedFileAttributeView view =
          getFileAttributeView(file, UserDefinedFileAttributeView.class);
      int n = rand.nextInt(16);
      while (n > 0) {
        byte[] value = new byte[1 + rand.nextInt(100)];
        view.write("user." + Integer.toString(n), ByteBuffer.wrap(value));
        n--;
      }
    }
  }
Ejemplo n.º 4
0
 // create directory in the given directory
 static Path createSourceDirectory(Path dir) throws IOException {
   String name = "sourcedir" + Integer.toString(rand.nextInt());
   Path subdir = dir.resolve(name);
   createDirectory(subdir);
   randomizeAttributes(subdir);
   return subdir;
 }
Ejemplo n.º 5
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);
    }
  }
Ejemplo n.º 6
0
  /** Test copy from file to output stream */
  static void testCopyFileToOuputStream() throws IOException {
    testCopyFileToOuputStream(0);
    for (int i = 0; i < 100; i++) {
      testCopyFileToOuputStream(rand.nextInt(32000));
    }

    // nulls
    try {
      copy((Path) null, new ByteArrayOutputStream());
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException ignore) {
    }
    try {
      Path source = createTempFile("blah", null);
      delete(source);
      copy(source, (OutputStream) null);
      throw new RuntimeException("NullPointerException expected");
    } catch (NullPointerException ignore) {
    }
  }
Ejemplo n.º 7
0
  static void testCopyFileToOuputStream(int size) throws IOException {
    Path source = createTempFile("blah", null);
    try {
      byte[] b = new byte[size];
      rand.nextBytes(b);
      write(source, b);

      ByteArrayOutputStream out = new ByteArrayOutputStream();

      long n = copy(source, out);
      assertTrue(n == size);
      assertTrue(out.size() == size);

      byte[] read = out.toByteArray();
      assertTrue(Arrays.equals(read, b));

      // check output stream is open
      out.write(0);
      assertTrue(out.size() == size + 1);
    } finally {
      delete(source);
    }
  }
Ejemplo n.º 8
0
 static boolean heads() {
   return rand.nextBoolean();
 }
Ejemplo n.º 9
0
 // create name for file in given directory
 static Path getTargetFile(Path dir) throws IOException {
   String name = "target" + Integer.toString(rand.nextInt());
   return dir.resolve(name);
 }