コード例 #1
0
ファイル: SshScpFile.java プロジェクト: farrault/overthere
 @Override
 public long getLength() {
   return f.length();
 }
コード例 #2
0
  @Test
  public void shouldCreateWriteReadAndRemoveTemporaryFile() throws IOException {
    final String prefix = "prefix";
    final String suffix = "suffix";
    final byte[] contents =
        ("Contents of the temporary file created at "
                + System.currentTimeMillis()
                + "ms since the epoch")
            .getBytes();

    OverthereFile tempFile = connection.getTempFile(prefix, suffix);
    assertThat(
        "Expected a non-null return value from HostConnection.getTempFile()",
        tempFile,
        notNullValue());
    assertThat(
        "Expected name of temporary file to start with the prefix",
        tempFile.getName(),
        startsWith(prefix));
    assertThat(
        "Expected name of temporary file to end with the suffix",
        tempFile.getName(),
        endsWith(suffix));
    assertThat("Expected temporary file to not exist yet", tempFile.exists(), equalTo(false));

    OutputStream out = tempFile.getOutputStream();
    try {
      out.write(contents);
    } finally {
      closeQuietly(out);
    }

    assertThat(
        "Expected temporary file to exist after writing to it", tempFile.exists(), equalTo(true));
    assertThat(
        "Expected temporary file to not be a directory", tempFile.isDirectory(), equalTo(false));
    assertThat(
        "Expected temporary file to have the size of the contents written to it",
        tempFile.length(),
        equalTo((long) contents.length));
    assertThat("Expected temporary file to be readable", tempFile.canRead(), equalTo(true));
    assertThat("Expected temporary file to be writeable", tempFile.canWrite(), equalTo(true));

    // Windows systems don't support the concept of checking for executability
    if (connection.getHostOperatingSystem() == OperatingSystemFamily.UNIX) {
      assertFalse("Expected temporary file to not be executable", tempFile.canExecute());
    }

    DataInputStream in = new DataInputStream(tempFile.getInputStream());
    try {
      final byte[] contentsRead = new byte[contents.length];
      in.readFully(contentsRead);
      assertThat(
          "Expected input stream to be exhausted after reading the full contents",
          in.available(),
          equalTo(0));
      assertThat(
          "Expected contents in temporary file to be identical to data written into it",
          contentsRead,
          equalTo(contents));
    } finally {
      closeQuietly(in);
    }

    tempFile.delete();
    assertThat("Expected temporary file to no longer exist", tempFile.exists(), equalTo(false));
  }