Java Code Examples for java.nio.file.Files#size()

The following examples show how to use java.nio.file.Files#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmokeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void test11(String target) throws Exception {
    System.out.print("test11: " + target);
    URI uri = new URI(target);

    HttpRequest request = HttpRequest.newBuilder(uri)
                                     .POST(fromInputStream(SmokeTest::newStream))
                                     .build();

    Path download = Paths.get("test11.txt");

    HttpResponse<Path> response = client.send(request, asFile(download));

    if (response.statusCode() != 200) {
        throw new RuntimeException("Wrong response code");
    }

    download.toFile().delete();
    response.body();

    if (Files.size(download) != Files.size(smallFile)) {
        System.out.println("Original size: " + Files.size(smallFile));
        System.out.println("Downloaded size: " + Files.size(download));
        throw new RuntimeException("Size mismatch");
    }
    System.out.println(" OK");
}
 
Example 2
Source File: SubmitAndSyncUnicodeFileTypeOnUnicodeEnabledServerTest.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodedByeGb18030WithUnixLineEndingFileWhenClientCharsetIsMatchUnderServerUnicodeEnabled_UnixClientLineEnding() throws Exception{
    String perforceCharset = "cp936";

    login(perforceCharset);
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/gb18030.txt");

    long originalFileSize = Files.size(testResourceFile.toPath());
    Charset matchCharset = Charset.forName(PerforceCharsets.getJavaCharsetName(perforceCharset));
    long utf8EncodedOriginalFileSize = getUnicodeFileSizeAfterEncodedByUtf8(
            testResourceFile,
            matchCharset);

    testSubmitFile(
            "p4TestUnixLineend",
            testResourceFile,
            "unicode",
            utf8EncodedOriginalFileSize,
            originalFileSize
    );
}
 
Example 3
Source File: SubmitAndSyncUnicodeFileTypeOnUnicodeEnabledServerTest.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodedByeGb18030WithWinLineEndingFileWhenClientCharsetIsMatchUnderServerUnicodeEnabled_UnixClientLineEnding() throws Exception{
    String perforceCharset = "cp936";
    login(perforceCharset);

    File testResourceFile = loadFileFromClassPath("com/perforce/p4java/common/io/gb18030_win_line_endings.txt");
    long originalFileSize = Files.size(testResourceFile.toPath());
    Charset matchCharset = Charset.forName(PerforceCharsets.getJavaCharsetName(perforceCharset));
    long utf8EncodedOriginalFileSize = getUnicodeFileSizeAfterEncodedByUtf8(
            testResourceFile,
            matchCharset);

    testSubmitFile(
            "p4TestUnixLineend",
            testResourceFile,
            "unicode",
            utf8EncodedOriginalFileSize,
            originalFileSize
    );
}
 
Example 4
Source File: TestJcmdDumpLimited.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
TestRecording(int id, int events) throws IOException, InterruptedException {
    r = new Recording();
    r.start();
    for (int i = 0; i < events; i++) {
        TestEvent event = new TestEvent();
        event.id = id;
        event.number = i;
        event.commit();
        if (i == events / 2) {
            time = Instant.now();
        }
    }
    r.stop();
    Thread.sleep(1);
    path = Paths.get("dump-" + id + ".jfr");
    r.dump(path);
    size = (int) Files.size(path);
    this.id = id;
    this.now = Instant.now();
}
 
Example 5
Source File: SubmitAndSyncUnicodeFileTypeOnNonUnicodeEnabledServerTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeByEncJpWithUnixLineEndingUnicodeFileUnderServerUnicodeNotEnabledExpectedTradeAsText_WinClientLineEnding() throws Exception {
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/euc-jp.txt");
    int totalUnixLineEndings = 10;
    long originalFileSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestWinLineend",
            testResourceFile,
            "text",
            originalFileSize,
            originalFileSize + totalUnixLineEndings
    );
}
 
Example 6
Source File: PartImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getSize() {
    try {
        if (formValue.isFile()) {
            return Files.size(formValue.getPath());
        } else {
            return formValue.getValue().length();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: SubmitAndSyncUnicodeFileTypeOnNonUnicodeEnabledServerTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeByJpShiftJisWithUnixLineEndingUnicodeFileUnderServerUnicodeNotEnabledExpectedTradeAsText_WinClientLineEnding() throws Exception {
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/shift_jis.txt");
    int totalUnixLineEndings = 1;
    long originalFileSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestWinLineend",
            testResourceFile,
            "text",
            originalFileSize,
            originalFileSize + totalUnixLineEndings
    );
}
 
Example 8
Source File: SubmitAndSyncUtf16FileTypeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidUtf16LEWithBomWithWinLineEnding_UnixClientLineEnding() throws Exception{
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/utf_16LE_with_bom_WIN_line_end.txt");
    long utf8EncodedOriginalFileSize = getUtf16FileSizeAfterRemoveBomAndEncodedByUtf8(testResourceFile, UTF_16LE);
    long originalFileSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestUnixLineend",
            testResourceFile,
            "utf16",
            utf8EncodedOriginalFileSize,
            originalFileSize
    );
}
 
Example 9
Source File: PythonResolvedPackageComponents.java    From buck with Apache License 2.0 5 votes vote down vote up
private boolean areFilesTheSame(Path a, Path b) throws IOException {
  if (a.equals(b)) {
    return true;
  }

  final long totalSize = Files.size(a);
  if (totalSize != Files.size(b)) {
    return false;
  }

  try (InputStream ia = Files.newInputStream(a);
      InputStream ib = Files.newInputStream(b)) {
    final int bufSize = 8192;
    final byte[] aBuf = new byte[bufSize];
    final byte[] bBuf = new byte[bufSize];
    for (int toRead = (int) totalSize; toRead > 0; ) {
      final int chunkSize = Integer.min(toRead, bufSize);
      ByteStreams.readFully(ia, aBuf, 0, chunkSize);
      ByteStreams.readFully(ib, bBuf, 0, chunkSize);
      for (int idx = 0; idx < chunkSize; idx++) {
        if (aBuf[idx] != bBuf[idx]) {
          return false;
        }
      }
      toRead -= chunkSize;
    }
  }

  return true;
}
 
Example 10
Source File: FileSystemRepository.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream read(final ContentClaim claim) throws IOException {
    if (claim == null) {
        return new ByteArrayInputStream(new byte[0]);
    }
    final Path path = getPath(claim, true);
    final FileInputStream fis = new FileInputStream(path.toFile());
    if (claim.getOffset() > 0L) {
        try {
            StreamUtils.skip(fis, claim.getOffset());
        } catch (final EOFException eof) {
            final long resourceClaimBytes;
            try {
                resourceClaimBytes = Files.size(path);
            } catch (final IOException e) {
                throw new ContentNotFoundException(claim, "Content Claim has an offset of " + claim.getOffset()
                    + " but Resource Claim has fewer than this many bytes (actual length of the resource claim could not be determined)");
            }

            throw new ContentNotFoundException(claim, "Content Claim has an offset of " + claim.getOffset() + " but Resource Claim " + path + " is only " + resourceClaimBytes + " bytes");
        } catch (final IOException ioe) {
            IOUtils.closeQuietly(fis);
            throw ioe;
        }
    }

    // A claim length of -1 indicates that the claim is still being written to and we don't know
    // the length. In this case, we don't limit the Input Stream. If the Length has been populated, though,
    // it is possible that the Length could then be extended. However, we do want to avoid ever allowing the
    // stream to read past the end of the Content Claim. To accomplish this, we use a LimitedInputStream but
    // provide a LongSupplier for the length instead of a Long value. this allows us to continue reading until
    // we get to the end of the Claim, even if the Claim grows. This may happen, for instance, if we obtain an
    // InputStream for this claim, then read from it, write more to the claim, and then attempt to read again. In
    // such a case, since we have written to that same Claim, we should still be able to read those bytes.
    if (claim.getLength() >= 0) {
        return new LimitedInputStream(fis, claim::getLength);
    } else {
        return fis;
    }
}
 
Example 11
Source File: SubmitAndSyncUtf8FileTypeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubmitTextFileWithClassicMacLineEnding_MacClientLineEnding() throws Exception {
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/text_file_with_classic_mac_cr_lineend.txt");
    long originalSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestMacLineend",
            testResourceFile,
            "text",
            originalSize,
            originalSize
    );
}
 
Example 12
Source File: SubmitAndSyncUtf16FileTypeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidUtf16LEWithoutBomWithWinLineEnding_WinClientLineEnding() throws Exception {
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/utf_16LE_without_bom.txt");
    long originalFileSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestWinLineend",
            testResourceFile,
            "binary",
            originalFileSize,
            originalFileSize
    );
}
 
Example 13
Source File: Stat.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/**
 * Print the length of the indicated file.
 *
 * <p>This uses the normal Java NIO Api, so it can take advantage of any installed NIO Filesystem
 * provider without any extra effort.
 */
private static void statFile(String fname) {
  try {
    Path path = Paths.get(new URI(fname));
    long size = Files.size(path);
    System.out.println(fname + ": " + size + " bytes.");
  } catch (Exception ex) {
    System.out.println(fname + ": " + ex.toString());
  }
}
 
Example 14
Source File: HttpSendFileTests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Test
public void sendZipFileCompressionSize_3() throws IOException {
	Path path = Files.createTempFile(null, ".zip");
	Files.copy(this.getClass().getResourceAsStream("/zipFile.zip"), path, StandardCopyOption.REPLACE_EXISTING);
	path.toFile().deleteOnExit();

	try (FileSystem zipFs = FileSystems.newFileSystem(path, (ClassLoader) null)) {
		Path fromZipFile = zipFs.getPath("/largeFile.txt");
		long fileSize = Files.size(fromZipFile);

		assertSendFile(out -> out.addHeader(HttpHeaderNames.CONTENT_LENGTH, "1245")
		                         .sendFile(fromZipFile, 0, fileSize), true, 512, null);
	}
}
 
Example 15
Source File: SubmitAndSyncUtf8FileTypeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubmitValidUtf8FileWithWinLineEndingButWithouBOM_UnixClientLineEnding() throws Exception {
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/utf8_with_bom_win_line_ending.txt");
    long originalSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestUnixLineend",
            testResourceFile,
            "utf8",
            originalSize - UTF_8_BOM_SIZE,
            originalSize
    );
}
 
Example 16
Source File: SubmitAndSyncUtf8FileTypeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubmitValidUtf8FileWithUnixLineEndingButWithoutBOM_UnixClientLineEnding() throws Exception {
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/has_utf8_bom_but_its_text.txt");

    long originalSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestUnixLineend",
            testResourceFile,
            "text",
            originalSize,
            originalSize
    );
}
 
Example 17
Source File: ZipFileStore.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public ZipFileStoreAttributes(ZipFileStore fileStore)
    throws IOException
{
    Path path = FileSystems.getDefault().getPath(fileStore.name());
    this.size = Files.size(path);
    this.fstore = Files.getFileStore(path);
}
 
Example 18
Source File: SubmitAndSyncUtf16FileTypeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotValidUtf16LEFileEventItHasBomButItsActuallyAnAudioFile() throws Exception{
    File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/file_has_utf_16LE_bom_but_its_actually_a_audio_file.mp1");
    long originalFileSize = Files.size(testResourceFile.toPath());
    testSubmitFile(
            "p4TestUnixLineend",
            testResourceFile,
            "binary",
            originalFileSize,
            originalFileSize
    );
}
 
Example 19
Source File: ParallelCopyGCSDirectoryIntoHDFSSparkIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(groups = {"spark", "bucket"})
public void testCopyLargeFile() throws Exception {
    MiniDFSCluster cluster = null;
    try {
        final Configuration conf = new Configuration();
        // set the minicluster to have a very low block size so that we can test transferring a file in chunks without actually needing to move a big file
        conf.set("dfs.blocksize", "1048576");
        cluster = MiniClusterUtils.getMiniCluster(conf);

        // copy a multi-block file
        final Path tempPath = MiniClusterUtils.getTempPath(cluster, "test", "dir");
        final String gcpInputPath = getGCPTestInputPath() + "huge/CEUTrio.HiSeq.WGS.b37.NA12878.chr1_4.bam.bai";
        String args =
                "--" + ParallelCopyGCSDirectoryIntoHDFSSpark.INPUT_GCS_PATH_LONG_NAME + " " + gcpInputPath +
                        " --" + ParallelCopyGCSDirectoryIntoHDFSSpark.OUTPUT_HDFS_DIRECTORY_LONG_NAME + " " + tempPath;
        ArgumentsBuilder ab = new ArgumentsBuilder().addRaw(args);
        IntegrationTestSpec spec = new IntegrationTestSpec(
                ab.getString(),
                Collections.emptyList());
        spec.executeTest("testCopyLargeFile-" + args, this);

        final long fileSizeOnGCS = Files.size(IOUtils.getPath(gcpInputPath));


        final String hdfsPath = tempPath + "/" + "CEUTrio.HiSeq.WGS.b37.NA12878.chr1_4.bam.bai";

        org.apache.hadoop.fs.Path outputHdfsDirectoryPath = new org.apache.hadoop.fs.Path(tempPath.toUri());

        try(FileSystem fs = outputHdfsDirectoryPath.getFileSystem(conf)) {
            long chunkSize = ParallelCopyGCSDirectoryIntoHDFSSpark.getChunkSize(fs);
            Assert.assertTrue(fileSizeOnGCS > chunkSize);
        }

        Assert.assertEquals(BucketUtils.fileSize(hdfsPath),
                fileSizeOnGCS);

        final File tempDir = createTempDir("ParallelCopy");

        BucketUtils.copyFile(hdfsPath, tempDir + "fileFromHDFS.bam.bai");
        Assert.assertEquals(Utils.calculateFileMD5(new File(tempDir + "fileFromHDFS.bam.bai")), "1a6baa5332e98ef1358ac0fb36f46aaf");
    } finally {
        MiniClusterUtils.stopCluster(cluster);
    }
}
 
Example 20
Source File: JournalDebug.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
public void debug(WriteStream out, Path path)
  throws IOException
{
  try (ReadStream is = new ReadStream(Files.newInputStream(path))) {
    long length = Files.size(path);
    
    long magic = BitsUtil.readLong(is);
    
    if (magic != JournalStore.JOURNAL_MAGIC) {
      out.println("Mismatched journal magic number for " + path);
      return;
    }
    
    _segmentLength = BitsUtil.readLong(is);
    _segmentTail = BitsUtil.readLong(is);
    _keyLength = BitsUtil.readInt(is);
    
    if (_segmentLength % JournalStore.BLOCK_SIZE != 0
        || _segmentLength <= 0) {
      out.println("Invalid segment length " + _segmentLength + " for " + path);
      return;
    }
    
    if (_keyLength != JournalStore.KEY_LENGTH) {
      out.println("Invalid key length " + _keyLength + " for " + path);
      return;
    }
    
    out.println("Journal: " + path);
    out.println("  segment-length : 0x" + Long.toHexString(_segmentLength));
    out.println("  file-length    : 0x" + Long.toHexString(length));
    
    for (long ptr = _segmentLength; ptr < length; ptr += _segmentLength) {
      int segment = (int) (ptr / _segmentLength);
      
      is.position(ptr + _segmentTail);
      
      long initSeq = BitsUtil.readLong(is);
      
      if (initSeq <= 0) {
        /*
        out.println();
        out.println("Segment: " + segment + " free");
        */
        continue;
      }
      
      byte []key = new byte[_keyLength];
      
      is.readAll(key, 0, key.length);
      
      long seq = BitsUtil.readLong(is);
      long checkpointBegin = BitsUtil.readLong(is);
      long checkpointEnd = BitsUtil.readLong(is);
    
      out.println();
      out.println("Segment: " + segment + " key=" + Hex.toHex(key, 0, 4)
                  + " (seg-off: 0x" + Long.toHexString(ptr) + ")");
      out.println("  init-sequence    : " + initSeq);
      out.println("  sequence         : " + seq);
      out.println("  checkpoint-begin : " + Long.toHexString(checkpointBegin));
      out.println("  checkpoint-end   : " + Long.toHexString(checkpointEnd));
    }
  }
}