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.roda.core.storage.fs.FSUtils.java

public static BinaryVersion convertPathToBinaryVersion(Path historyDataPath, Path historyMetadataPath,
        Path path) throws RequestNotValidException, NotFoundException, GenericException {
    DefaultBinaryVersion ret;//ww w. ja va2 s .  c  om

    if (!FSUtils.exists(path)) {
        throw new NotFoundException("Cannot find file version at " + path);
    }

    // storage path
    Path relativePath = historyDataPath.relativize(path);
    String fileName = relativePath.getFileName().toString();
    int lastIndexOfDot = fileName.lastIndexOf(VERSION_SEP);

    if (lastIndexOfDot <= 0 || lastIndexOfDot == fileName.length() - 1) {
        throw new RequestNotValidException("Bad name for versioned file: " + path);
    }

    String id = fileName.substring(lastIndexOfDot + 1);
    String realFileName = fileName.substring(0, lastIndexOfDot);
    Path realFilePath = relativePath.getParent().resolve(realFileName);
    Path metadataPath = historyMetadataPath
            .resolve(relativePath.getParent().resolve(fileName + METADATA_SUFFIX));

    StoragePath storagePath = FSUtils.getStoragePath(realFilePath);

    // construct
    ContentPayload content = new FSPathContentPayload(path);
    long sizeInBytes;
    try {
        sizeInBytes = Files.size(path);
        Map<String, String> contentDigest = null;
        Binary binary = new DefaultBinary(storagePath, content, sizeInBytes, false, contentDigest);

        if (FSUtils.exists(metadataPath)) {
            ret = JsonUtils.readObjectFromFile(metadataPath, DefaultBinaryVersion.class);
            ret.setBinary(binary);
        } else {
            Date createdDate = new Date(
                    Files.readAttributes(path, BasicFileAttributes.class).creationTime().toMillis());
            Map<String, String> defaultProperties = new HashMap<>();
            ret = new DefaultBinaryVersion(binary, id, createdDate, defaultProperties);
        }

    } catch (IOException e) {
        throw new GenericException("Could not get file size", e);
    }

    return ret;
}

From source file:com.spectralogic.ds3client.integration.Smoke_Test.java

@Test
public void testRecoverReadJob()
        throws IOException, XmlProcessingException, JobRecoveryException, URISyntaxException {
    final String bucketName = "test_recover_read_job_bucket";
    final String book1 = "beowulf.txt";
    final String book2 = "ulysses.txt";
    final Path objPath1 = ResourceUtils.loadFileResource(RESOURCE_BASE_NAME + book1);
    final Path objPath2 = ResourceUtils.loadFileResource(RESOURCE_BASE_NAME + book2);
    final Ds3Object obj1 = new Ds3Object(book1, Files.size(objPath1));
    final Ds3Object obj2 = new Ds3Object(book2, Files.size(objPath2));

    final Path dirPath = FileSystems.getDefault().getPath("output");
    if (!Files.exists(dirPath)) {
        Files.createDirectory(dirPath);
    }//  www.j a  v a  2  s  .  com

    try {
        HELPERS.ensureBucketExists(bucketName, envDataPolicyId);

        final Ds3ClientHelpers.Job putJob = HELPERS.startWriteJob(bucketName, Lists.newArrayList(obj1, obj2));
        putJob.transfer(new ResourceObjectPutter(RESOURCE_BASE_NAME));

        final FileChannel channel1 = FileChannel.open(dirPath.resolve(book1), StandardOpenOption.WRITE,
                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

        final Ds3ClientHelpers.Job readJob = HELPERS.startReadJob(bucketName, Lists.newArrayList(obj1, obj2));
        final GetObjectResponse readResponse1 = client
                .getObject(new GetObjectRequest(bucketName, book1, channel1, readJob.getJobId().toString(), 0));

        assertThat(readResponse1, is(notNullValue()));
        assertThat(readResponse1.getStatusCode(), is(equalTo(200)));

        // Interruption...
        final Ds3ClientHelpers.Job recoverJob = HELPERS.recoverReadJob(readJob.getJobId());

        final FileChannel channel2 = FileChannel.open(dirPath.resolve(book2), StandardOpenOption.WRITE,
                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
        final GetObjectResponse readResponse2 = client.getObject(
                new GetObjectRequest(bucketName, book2, channel2, recoverJob.getJobId().toString(), 0));
        assertThat(readResponse2, is(notNullValue()));
        assertThat(readResponse2.getStatusCode(), is(equalTo(200)));

    } finally {
        deleteAllContents(client, bucketName);
        for (final Path tempFile : Files.newDirectoryStream(dirPath)) {
            Files.delete(tempFile);
        }
        Files.delete(dirPath);
    }
}

From source file:com.emc.ecs.smart.SmartUploader.java

/**
 * Does a standard PUT upload using HttpURLConnection.
 *//*from  ww  w  .  j a v a2 s . co  m*/
private void doSimpleUpload() {
    try {
        fileSize = Files.size(fileToUpload);
        HttpURLConnection conn = null;
        long start = System.currentTimeMillis();
        try (DigestInputStream is = new DigestInputStream(Files.newInputStream(fileToUpload),
                MessageDigest.getInstance("MD5"))) {
            conn = (HttpURLConnection) uploadUrl.openConnection();
            conn.setFixedLengthStreamingMode(fileSize);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setInstanceFollowRedirects(false);
            conn.setUseCaches(false);
            conn.setRequestMethod(HttpMethod.PUT);
            conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
            OutputStream os = conn.getOutputStream();
            byte[] buf = new byte[CHUNK_SIZE];
            int len;

            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
                bytesUploaded += len;
                printPercent();
            }
            os.close();

            if (conn.getResponseCode() != ClientResponse.Status.OK.getStatusCode()) {
                throw new RuntimeException("Unable to upload object content: status=" + conn.getResponseCode());
            } else {
                List<String> eTags = conn.getHeaderFields().get(HttpHeaders.ETAG);
                if (eTags == null || eTags.size() < 1) {
                    throw new RuntimeException("Unable to get ETag for uploaded data");
                } else {
                    byte[] sentMD5 = is.getMessageDigest().digest();
                    String eTag = eTags.get(0);
                    byte[] gotMD5 = DatatypeConverter.parseHexBinary(eTag.substring(1, eTag.length() - 1));
                    if (!Arrays.equals(gotMD5, sentMD5)) {
                        throw new RuntimeException("ETag doesn't match streamed data's MD5.");
                    }
                }
            }
        } catch (IOException e) {
            throw new Exception("IOException while uploading object content after writing " + bytesUploaded
                    + " of " + fileSize + " bytes: " + e.getMessage());
        } finally {
            if (conn != null)
                conn.disconnect();
        }

        long elapsed = System.currentTimeMillis() - start;
        printRate(fileSize, elapsed);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.fao.geonet.api.registries.vocabularies.KeywordsApi.java

/**
 * Upload thesaurus./*  w  ww.  jav  a2  s.c  o  m*/
 *
 * @param url the url
 * @param registryUrl the registry url
 * @param registryLanguage the languages to retrieve from the registry
 * @param type the type
 * @param dir the dir
 * @param stylesheet the stylesheet
 * @param request the request
 * @return the element
 * @throws Exception the exception
 */
@ApiOperation(value = "Uploads a new thesaurus from URL or Registry", nickname = "uploadThesaurusFromUrl", notes = "Uploads a new thesaurus.")
@RequestMapping(method = RequestMethod.PUT, produces = MediaType.TEXT_XML_VALUE)
@ApiResponses(value = { @ApiResponse(code = 201, message = "Thesaurus uploaded in SKOS format."),
        @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_REVIEWER) })
@PreAuthorize("hasRole('Reviewer')")
@ResponseBody
@ResponseStatus(value = HttpStatus.CREATED)
public String uploadThesaurusFromUrl(
        @ApiParam(value = "If set, try to download from the Internet.") @RequestParam(value = "url", required = false) String url,
        @ApiParam(value = "If set, try to download from a registry.") @RequestParam(value = "registryUrl", required = false) String registryUrl,
        @ApiParam(value = "Languages to download from a registry.") @RequestParam(value = "registryLanguage", required = false) String[] registryLanguage,
        @ApiParam(value = "Local or external (default).") @RequestParam(value = "type", defaultValue = "external") String type,
        @ApiParam(value = "Type of thesaurus, usually one of the ISO thesaurus type codelist value. Default is theme.") @RequestParam(value = "dir", defaultValue = "theme") String dir,
        @ApiParam(value = "XSL to be use to convert the thesaurus before load. Default _none_.") @RequestParam(value = "stylesheet", defaultValue = "_none_") String stylesheet,
        HttpServletRequest request) throws Exception {

    long start = System.currentTimeMillis();
    ServiceContext context = ApiUtils.createServiceContext(request);

    boolean urlUpload = !StringUtils.isEmpty(url);
    boolean registryUpload = !StringUtils.isEmpty(registryUrl);

    // Upload RDF file
    Path rdfFile = null;
    String fname = null;

    // Specific upload steps
    if (urlUpload) {

        Log.debug(Geonet.THESAURUS, "Uploading thesaurus from URL: " + url);

        rdfFile = getXMLContentFromUrl(url, context);
        fname = url.substring(url.lastIndexOf("/") + 1, url.length()).replaceAll("\\s+", "");

        // File with no extension in URL
        if (fname.lastIndexOf('.') == -1) {
            fname += ".rdf";
        }

    } else if (registryUpload) {
        if (ArrayUtils.isEmpty(registryLanguage)) {
            throw new MissingServletRequestParameterException("Select at least one language.", "language");
        }

        Log.debug(Geonet.THESAURUS, "Uploading thesaurus from registry : " + registryUrl);

        String itemName = registryUrl.substring((registryUrl.lastIndexOf("/") + 1), registryUrl.length());

        rdfFile = extractSKOSFromRegistry(registryUrl, itemName, registryLanguage, context);
        fname = registryUrl.replaceAll("[^A-Za-z]+", "") + "-" + itemName + ".rdf";

    } else {

        Log.debug(Geonet.THESAURUS, "No URL or file name provided for thesaurus upload.");
        throw new MissingServletRequestParameterException("Thesaurus source not provided", "url");

    }

    if (StringUtils.isEmpty(fname)) {
        throw new ResourceNotFoundException("File upload from URL or file return null");
    }

    long fsize;
    if (rdfFile != null && Files.exists(rdfFile)) {
        fsize = Files.size(rdfFile);
    } else {
        throw new ResourceNotFoundException("Thesaurus file doesn't exist");
    }

    // -- check that the archive actually has something in it
    if (fsize == 0) {
        throw new ResourceNotFoundException("Thesaurus file has zero size");
    }

    String extension = FilenameUtils.getExtension(fname);

    if (extension.equalsIgnoreCase("rdf") || extension.equalsIgnoreCase("xml")) {
        Log.debug(Geonet.THESAURUS, "Uploading thesaurus: " + fname);

        // Rename .xml to .rdf for all thesaurus
        fname = fname.replace(extension, "rdf");
        uploadThesaurus(rdfFile, stylesheet, context, fname, type, dir);
    } else {
        Log.debug(Geonet.THESAURUS, "Incorrect extension for thesaurus named: " + fname);
        throw new MissingServletRequestParameterException("Incorrect extension for thesaurus", fname);
    }

    long end = System.currentTimeMillis();
    long duration = (end - start) / 1000;

    return String.format("Thesaurus '%s' loaded in %d sec.", fname, duration);
}

From source file:org.apache.nifi.controller.StandardFlowSynchronizer.java

private byte[] readFlowFromDisk() throws IOException {
    final Path flowPath = nifiProperties.getFlowConfigurationFile().toPath();
    if (!Files.exists(flowPath) || Files.size(flowPath) == 0) {
        return new byte[0];
    }/*from   ww  w .  j a va2s.  co  m*/

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try (final InputStream in = Files.newInputStream(flowPath, StandardOpenOption.READ);
            final InputStream gzipIn = new GZIPInputStream(in)) {
        FileUtils.copy(gzipIn, baos);
    }

    return baos.toByteArray();
}

From source file:org.opencb.cellbase.app.transform.GeneParser.java

private Map<String, Fasta> getCDnaSequencesMap() throws IOException, FileFormatException {
    logger.info("Loading ENSEMBL's cDNA sequences...");
    Map<String, Fasta> cDnaSequencesMap = new HashMap<>();
    if (cDnaFastaFile != null && Files.exists(cDnaFastaFile) && !Files.isDirectory(cDnaFastaFile)
            && Files.size(cDnaFastaFile) > 0) {
        FastaReader fastaReader = new FastaReader(cDnaFastaFile);
        List<Fasta> fastaList = fastaReader.readAll();
        fastaReader.close();//from  ww  w. ja v a  2s.  c  o  m
        for (Fasta fasta : fastaList) {
            cDnaSequencesMap.put(fasta.getId(), fasta);
        }
    } else {
        logger.warn("cDNA fasta file " + cDnaFastaFile + " not found");
        logger.warn("ENSEMBL's cDNA sequences not loaded");
    }
    return cDnaSequencesMap;
}

From source file:org.opencb.cellbase.app.transform.GeneParser.java

private Map<String, Fasta> getProteinSequencesMap() throws IOException, FileFormatException {
    logger.info("Loading ENSEMBL's protein sequences...");
    Map<String, Fasta> proteinSequencesMap = new HashMap<>();
    if (proteinFastaFile != null && Files.exists(proteinFastaFile) && !Files.isDirectory(proteinFastaFile)
            && Files.size(proteinFastaFile) > 0) {
        FastaReader fastaReader = new FastaReader(proteinFastaFile);
        List<Fasta> fastaList = fastaReader.readAll();
        fastaReader.close();/*from  w  w  w.  j  a v  a2s. c om*/
        for (Fasta fasta : fastaList) {
            proteinSequencesMap.put(fasta.getDescription().split("transcript:")[1].split("\\s")[0], fasta);
        }
    } else {
        logger.warn("Protein fasta file " + proteinFastaFile + " not found");
        logger.warn("ENSEMBL's protein sequences not loaded");
    }
    return proteinSequencesMap;
}

From source file:misc.FileHandler.java

/**
 * Transmits the given file over the given output stream. Note that the
 * output stream is not closed by this function.
 * /*from  w  w w.j  ava 2  s . c  o m*/
 * @param file
 *            the file to transmit.
 * @param byteFirst
 *            first byte of the file to transmit. One-based. If
 *            <code>null</code>, transmission starts at the first byte.
 * @param byteLast
 *            the last byte of the file to transmit. Can be the file size at
 *            max. If <code>null</code>, the file size is taken as this
 *            argument.
 * @param out
 *            the output stream over which the file should be sent.
 * @return <code>true</code>, if the file was successfully transmitted.
 *         <code>false</code>, otherwise.
 */
public static boolean transmitFile(Path file, Long byteFirst, Long byteLast, OutputStream out) {
    if ((file == null) || !Files.isReadable(file)) {
        throw new IllegalArgumentException("file must exist and be readable!");
    }
    if (byteFirst == null) {
        byteFirst = 1L;
    } else if (byteFirst < 1) {
        throw new IllegalArgumentException("byteFirst must be at least one!");
    }
    if (byteLast == null) {
        try {
            byteLast = Files.size(file);
        } catch (IOException e) {
            Logger.logError(e);
            return false;
        }
    } else if (byteLast < 1) {
        throw new IllegalArgumentException("byteLast must be at least one!");
    }
    if (out == null) {
        throw new NullPointerException("out may not be null!");
    }

    boolean success = false;

    try (FileInputStream in = new FileInputStream(file.toFile());) {
        byte[] buffer = new byte[BUFFER_SIZE];
        int count = 0;
        int read;

        in.skip(byteFirst - 1L);
        while ((count < byteLast)
                && ((read = in.read(buffer, 0, Math.min(buffer.length, (int) (byteLast - count)))) != -1)) {
            out.write(buffer, 0, read);
            count += read;
        }
        out.flush();
        success = true;
    } catch (IOException e) {
        Logger.logError(e);
    }

    return success;
}

From source file:org.opencb.cellbase.app.transform.GeneParser.java

private Map<String, String> getGeneDescriptionMap() throws IOException {
    logger.info("Loading gene description data...");
    Map<String, String> geneDescriptionMap = new HashMap<>();
    String[] fields;/*from   w  w w.j av a  2  s .c o m*/
    if (geneDescriptionFile != null && Files.exists(geneDescriptionFile)
            && Files.size(geneDescriptionFile) > 0) {
        List<String> lines = Files.readAllLines(geneDescriptionFile, Charset.forName("ISO-8859-1"));
        //            List<String> lines = Files.readAllLines(geneDescriptionFile, Charset.defaultCharset());
        for (String line : lines) {
            fields = line.split("\t", -1);
            geneDescriptionMap.put(fields[0], fields[1]);
        }
    } else {
        logger.warn("Gene description file " + geneDescriptionFile + " not found");
        logger.warn("Gene description data not loaded");
    }
    return geneDescriptionMap;
}

From source file:com.spectralogic.ds3client.integration.GetJobManagement_Test.java

private Ds3ClientHelpers.Job createReadJobWithObjectsReadyToTransfer(
        final Ds3ClientShimFactory.ClientFailureType clientFailureType) throws IOException, URISyntaxException,
        NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final String DIR_NAME = "largeFiles/";
    final String FILE_NAME = "lesmis-copies.txt";

    final Path objPath = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME);
    final long bookSize = Files.size(objPath);
    final Ds3Object obj = new Ds3Object(FILE_NAME, bookSize);

    final Ds3Client ds3Client = Ds3ClientShimFactory.makeWrappedDs3Client(clientFailureType, client);

    final int maxNumBlockAllocationRetries = 3;
    final int maxNumObjectTransferAttempts = 3;
    final Ds3ClientHelpers ds3ClientHelpers = Ds3ClientHelpers.wrap(ds3Client, maxNumBlockAllocationRetries,
            maxNumObjectTransferAttempts);

    final Ds3ClientHelpers.Job readJob = ds3ClientHelpers.startReadJob(BUCKET_NAME, Arrays.asList(obj));

    return readJob;
}