Example usage for java.nio.file Files size

List of usage examples for java.nio.file Files size

Introduction

In this page you can find the example usage for java.nio.file Files size.

Prototype

public static long size(Path path) throws IOException 

Source Link

Document

Returns the size of a file (in bytes).

Usage

From source file:org.savantbuild.io.tar.TarBuilderTest.java

private static void assertTarFileEquals(Path tarFile, String entry, Path original) throws IOException {
    InputStream is = Files.newInputStream(tarFile);
    if (tarFile.toString().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    }//w  w w . j a  va  2 s. c  o  m

    try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
        TarArchiveEntry tarArchiveEntry = tis.getNextTarEntry();
        while (tarArchiveEntry != null && !tarArchiveEntry.getName().equals(entry)) {
            tarArchiveEntry = tis.getNextTarEntry();
        }

        if (tarArchiveEntry == null) {
            fail("Tar [" + tarFile + "] is missing entry [" + entry + "]");
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int length;
        while ((length = tis.read(buf)) != -1) {
            baos.write(buf, 0, length);
        }

        assertEquals(Files.readAllBytes(original), baos.toByteArray());
        assertEquals(tarArchiveEntry.getSize(), Files.size(original));
        assertEquals(tarArchiveEntry.getUserName(), Files.getOwner(original).getName());
        assertEquals(tarArchiveEntry.getGroupName(),
                Files.readAttributes(original, PosixFileAttributes.class).group().getName());
    }
}

From source file:com.fizzed.blaze.util.Streamables.java

static public StreamableInput input(Path path) {
    Objects.requireNonNull(path, "path cannot be null");

    if (!Files.exists(path)) {
        throw new FileNotFoundException("Path " + path + " not found");
    }/*from  w w  w .  j  av a 2s . c  om*/

    long size;
    try {
        size = Files.size(path);
    } catch (IOException e) {
        throw new BlazeException(e.getMessage(), e);
    }

    return new StreamableInput(new DeferredFileInputStream(path), path.getFileName().toString(), path, size);
}

From source file:org.n52.geolabel.formats.PngTest.java

@Test
public void saveImageFromSVG() throws IOException {
    InputStream svg = getClass().getResourceAsStream("/label.svg");

    InputStream in = this.encoder.encode(svg);

    File temp = File.createTempFile("geolabel_", ".png");
    try (FileOutputStream tempFile = new FileOutputStream(temp);) {
        IOUtils.copy(in, tempFile);/*from   w w w .  ja  v a2  s .com*/
    }

    // System.out.println("Saved geolabel as " + temp.getAbsolutePath());

    Long actual = Long.valueOf(Files.size(temp.toPath()));
    assertThat("file is larger than 0 KB", actual, is(greaterThan(Long.valueOf(0l))));

    temp.deleteOnExit();
}

From source file:uk.ac.ebi.eva.pipeline.jobs.deciders.EmptyFileDecider.java

private long getFileSize() {
    long fileSize;

    try {/*from  ww  w .j  av a2s  .com*/
        fileSize = Files.size(Paths.get(file));
    } catch (IOException e) {
        throw new RuntimeException("File {} is not readable", e);
    }
    return fileSize;
}

From source file:fr.duminy.jbackup.core.archive.Decompressor.java

public void decompress(Path archive, Path targetDirectory, TaskListener listener, Cancellable cancellable)
        throws ArchiveException {
    if (listener != null) {
        try {/*from ww  w  . j a va 2 s  . c  om*/
            listener.totalSizeComputed(Files.size(archive));
        } catch (IOException ioe) {
            throw new ArchiveException(ioe);
        }
    }

    targetDirectory = (targetDirectory == null) ? Paths.get(".") : targetDirectory;
    if (!Files.exists(targetDirectory)) {
        throw new IllegalArgumentException(
                String.format("The target directory '%s' doesn't exist.", targetDirectory));
    }

    MutableLong processedSize = new MutableLong();

    try (InputStream archiveStream = Files.newInputStream(archive);
            ArchiveInputStream input = factory.create(archiveStream)) {
        ArchiveInputStream.Entry entry = getNextEntryIfNotCancelled(input, cancellable);
        while (entry != null) {
            InputStream entryStream = createCountingInputStream(listener, processedSize, entry.getInput());
            try {
                Path file = targetDirectory.resolve(entry.getName());
                Files.createDirectories(file.getParent());
                Files.copy(entryStream, file);
            } finally {
                entry.close();
            }

            entry = getNextEntryIfNotCancelled(input, cancellable);
        }
    } catch (IOException e) {
        throw new ArchiveException(e);
    } catch (Exception e) {
        throw new ArchiveException(e);
    }
}

From source file:onl.area51.httpd.util.PathEntity.java

@Override
public long getContentLength() {
    try {/*from w w w.  j  a v a2  s  . c o  m*/
        return Files.size(file);
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}

From source file:de.unirostock.sems.cbarchive.web.dataholder.ArchiveEntryDataholder.java

public ArchiveEntryDataholder(ArchiveEntry archiveEntry) {
    this.archiveEntry = archiveEntry;
    this.metaDataHolder = archiveEntry;

    // imports data to dataholder
    filePath = archiveEntry.getFilePath();
    fileName = archiveEntry.getFileName();
    format = archiveEntry.getFormat();//from  w  w  w.  jav a  2 s. c o m
    id = Tools.generateHashId(filePath);

    try {
        fileSize = Files.size(archiveEntry.getPath());
    } catch (IOException e) {
        LOGGER.warn(e, "Cannot determine file size for ", filePath);
        fileSize = 0;
    }

    copyMetaData(archiveEntry);
}

From source file:org.fao.geonet.utils.nio.PathHttpEntity.java

public long getContentLength() {
    try {/*from   w ww  .  j  a va 2 s  . com*/
        return Files.size(this.file);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.bimserver.tools.ifcloader.BulkLoader.java

private byte[] extractHead(Path path) {
    try (InputStream inputStream = Files.newInputStream(path)) {
        byte[] buffer = new byte[(int) Math.min(4096, Files.size(path))];
        IOUtils.readFully(inputStream, buffer);
        return buffer;
    } catch (IOException e) {
        return null;
    }// w  w  w . j av  a2s .  co  m
}

From source file:org.n52.geolabel.formats.PngTest.java

@Test
public void saveEmptyLabel() throws IOException {
    Label l = new Label();

    InputStream in = this.encoder.encode(l);

    File temp = File.createTempFile("geolabel_", ".png");
    try (FileOutputStream tempFile = new FileOutputStream(temp);) {
        IOUtils.copy(in, tempFile);//from w  w w  .  j a  va2 s  .com
    }

    // System.out.println("Saved empty geolabel as " + temp.getAbsolutePath());

    Long actual = Long.valueOf(Files.size(temp.toPath()));
    assertThat("file is larger than 0 KB", actual, is(greaterThan(Long.valueOf(0l))));

    temp.deleteOnExit();
}