Example usage for org.apache.commons.io FileUtils byteCountToDisplaySize

List of usage examples for org.apache.commons.io FileUtils byteCountToDisplaySize

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils byteCountToDisplaySize.

Prototype

public static String byteCountToDisplaySize(long size) 

Source Link

Document

Returns a human-readable version of the file size, where the input represents a specific number of bytes.

Usage

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Display a list of the jar dependencies twice. The first display lists the
 * jars, which can be useful when assembling information for the LICENSE
 * file. The second display lists the jars and their sizes, which is useful
 * for gauging the size decrease offered by the lite jar file.
 *//*w w w .  j  ava  2 s . c o  m*/
private static void displayJarSizes() {
    System.out.println("\nIndividual Jar Dependencies (for Comparison):");
    Set<String> jarNames = jarSizes.keySet();

    for (String jarName : jarNames) {
        System.out.println(jarName);
    }
    System.out.println();

    Long totalSize = 0L;
    int count = 0;
    for (String jarName : jarNames) {
        Long jarSize = jarSizes.get(jarName);
        String jarSizeDisplay = FileUtils.byteCountToDisplaySize(jarSize);
        System.out.println(" #" + ++count + ": " + jarName + " (" + jarSizeDisplay + " ["
                + NumberFormat.getInstance().format(jarSize) + " bytes])");
        totalSize = totalSize + jarSize;
    }
    String totalSizeDisplay = FileUtils.byteCountToDisplaySize(totalSize);
    System.out.println("Total Size of Jar Dependencies: " + totalSizeDisplay + " ["
            + NumberFormat.getInstance().format(totalSize) + " bytes]");
}

From source file:org.apache.tajo.worker.RemoteFetcher.java

@Override
public List<FileChunk> get() throws IOException {
    List<FileChunk> fileChunks = new ArrayList<>();

    if (state == FetcherState.FETCH_INIT) {
        ChannelInitializer<Channel> initializer = new HttpClientChannelInitializer(fileChunk.getFile());
        bootstrap.handler(initializer);//w  w  w  .j av a2s  .c  om
    }

    this.startTime = System.currentTimeMillis();
    this.state = FetcherState.FETCH_DATA_FETCHING;
    ChannelFuture future = null;
    try {
        future = bootstrap.clone().connect(new InetSocketAddress(host, port))
                .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);

        // Wait until the connection attempt succeeds or fails.
        Channel channel = future.awaitUninterruptibly().channel();
        if (!future.isSuccess()) {
            state = TajoProtos.FetcherState.FETCH_FAILED;
            throw new IOException(future.cause());
        }

        String query = uri.getPath() + (uri.getRawQuery() != null ? "?" + uri.getRawQuery() : "");
        // Prepare the HTTP request.
        HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query);
        request.headers().set(HttpHeaders.Names.HOST, host);
        request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Status: " + getState() + ", URI:" + uri);
        }
        // Send the HTTP request.
        channel.writeAndFlush(request);

        // Wait for the server to close the connection. throw exception if failed
        channel.closeFuture().syncUninterruptibly();

        fileChunk.setLength(fileChunk.getFile().length());

        long start = 0;
        for (Long eachChunkLength : chunkLengths) {
            if (eachChunkLength == 0)
                continue;
            FileChunk chunk = new FileChunk(fileChunk.getFile(), start, eachChunkLength);
            chunk.setEbId(fileChunk.getEbId());
            chunk.setFromRemote(true);
            fileChunks.add(chunk);
            start += eachChunkLength;
        }
        return fileChunks;

    } finally {
        if (future != null && future.channel().isOpen()) {
            // Close the channel to exit.
            future.channel().close().awaitUninterruptibly();
        }

        this.finishTime = System.currentTimeMillis();
        long elapsedMills = finishTime - startTime;
        String transferSpeed;
        if (elapsedMills > 1000) {
            long bytePerSec = (fileChunk.length() * 1000) / elapsedMills;
            transferSpeed = FileUtils.byteCountToDisplaySize(bytePerSec);
        } else {
            transferSpeed = FileUtils.byteCountToDisplaySize(Math.max(fileChunk.length(), 0));
        }

        LOG.info(String.format("Fetcher :%d ms elapsed. %s/sec, len:%d, state:%s, URL:%s", elapsedMills,
                transferSpeed, fileChunk.length(), getState(), uri));
    }
}

From source file:org.artifactory.webapp.wicket.page.logs.SystemLogsViewPanel.java

/**
 * Sets the attributes of the log size, download links and update times according to the log availability
 *//*w  w  w . ja  va2  s  .co m*/
private void setLogInfo() {
    if (!systemLogFile.exists()) {
        sizeLabel.setDefaultModelObject("");
        linkLabel.setDefaultModelObject("");
        lastUpdateLabel.setDefaultModelObject("");
    } else {
        sizeLabel.setDefaultModelObject("(" + FileUtils.byteCountToDisplaySize(systemLogFile.length()) + ")");
        linkLabel.setDefaultModelObject("Download");
        StringBuilder sb = new StringBuilder();
        Date logLastModified = new Date(systemLogFile.lastModified());
        Date viewLastUpdate = new Date(System.currentTimeMillis());
        sb.append("File last modified: ").append(logLastModified).append(". ");
        sb.append("View last updated: ").append(viewLastUpdate).append(".");
        lastUpdateLabel.setDefaultModelObject(sb.toString());
        downloadLink.setDefaultModel(new Model<>(systemLogFile));
    }
}

From source file:org.cloudifysource.dsl.internal.ServiceReader.java

/**
 *
 * @param serviceDirOrFile/* w w w  .  j av  a2s  .c o  m*/
 *            .
 * @param maxJarSizePermitted
 *            .
 * @throws PackagingException .
 */
public static void validateFolderSize(final File serviceDirOrFile, final long maxJarSizePermitted)
        throws PackagingException {
    File folder = serviceDirOrFile;
    if (folder.isFile()) {
        folder = folder.getParentFile();
    }
    final long folderSize = FileUtils.sizeOfDirectory(folder);
    if (folderSize == 0) {
        throw new PackagingException("folder " + folder.getAbsolutePath() + " is empty");
    }
    final long maxJarSize = maxJarSizePermitted;
    if (folderSize > maxJarSize || folderSize == 0) {
        throw new PackagingException("folder " + folder.getAbsolutePath() + "size is: "
                + FileUtils.byteCountToDisplaySize(folderSize) + ", it must be smaller than: "
                + FileUtils.byteCountToDisplaySize(maxJarSize));
    }

}

From source file:org.codelibs.fess.util.MemoryUtil.java

public static String byteCountToDisplaySize(final long size) {
    return FileUtils.byteCountToDisplaySize(size).replace(" ", StringUtil.EMPTY);
}

From source file:org.commonwl.view.cwl.CWLService.java

/**
 * Gets a list of workflows from a packed CWL file
 * @param packedFile The packed CWL file
 * @return The list of workflow overviews
 *//*w  w  w .ja  v a 2  s .c  o  m*/
public List<WorkflowOverview> getWorkflowOverviewsFromPacked(File packedFile) throws IOException {
    if (packedFile.length() <= singleFileSizeLimit) {
        List<WorkflowOverview> overviews = new ArrayList<>();

        JsonNode packedJson = yamlStringToJson(readFileToString(packedFile));

        if (packedJson.has(DOC_GRAPH)) {
            for (JsonNode jsonNode : packedJson.get(DOC_GRAPH)) {
                if (extractProcess(jsonNode) == CWLProcess.WORKFLOW) {
                    WorkflowOverview overview = new WorkflowOverview(jsonNode.get(ID).asText(),
                            extractLabel(jsonNode), extractDoc(jsonNode));
                    overviews.add(overview);
                }
            }
        } else {
            throw new IOException("The file given was not recognised as a packed CWL file");
        }

        return overviews;

    } else {
        throw new IOException("File '" + packedFile.getName() + "' is over singleFileSizeLimit - "
                + FileUtils.byteCountToDisplaySize(packedFile.length()) + "/"
                + FileUtils.byteCountToDisplaySize(singleFileSizeLimit));
    }
}

From source file:org.commonwl.view.cwl.CWLService.java

/**
 * Gets the Workflow object from internal parsing
 * @param workflowFile The workflow file to be parsed
 * @param packedWorkflowId The ID of the workflow object if the file is packed
 * @return The constructed workflow object
 *///from   w  w  w. jav a  2s. c  o m
public Workflow parseWorkflowNative(Path workflowFile, String packedWorkflowId) throws IOException {

    // Check file size limit before parsing
    long fileSizeBytes = Files.size(workflowFile);
    if (fileSizeBytes <= singleFileSizeLimit) {

        // Parse file as yaml
        JsonNode cwlFile = yamlStringToJson(readFileToString(workflowFile.toFile()));

        // Check packed workflow occurs
        if (packedWorkflowId != null) {
            boolean found = false;
            if (cwlFile.has(DOC_GRAPH)) {
                for (JsonNode jsonNode : cwlFile.get(DOC_GRAPH)) {
                    if (extractProcess(jsonNode) == CWLProcess.WORKFLOW) {
                        String currentId = jsonNode.get(ID).asText();
                        if (currentId.startsWith("#")) {
                            currentId = currentId.substring(1);
                        }
                        if (currentId.equals(packedWorkflowId)) {
                            cwlFile = jsonNode;
                            found = true;
                            break;
                        }
                    }
                }
            }
            if (!found)
                throw new WorkflowNotFoundException();
        } else {
            // Check the current json node is a workflow
            if (extractProcess(cwlFile) != CWLProcess.WORKFLOW) {
                throw new WorkflowNotFoundException();
            }
        }

        // Use filename for label if there is no defined one
        String label = extractLabel(cwlFile);
        if (label == null) {
            label = workflowFile.getFileName().toString();
        }

        // Construct the rest of the workflow model
        Workflow workflowModel = new Workflow(label, extractDoc(cwlFile), getInputs(cwlFile),
                getOutputs(cwlFile), getSteps(cwlFile));

        workflowModel.setCwltoolVersion(cwlTool.getVersion());

        // Generate DOT graph
        StringWriter graphWriter = new StringWriter();
        ModelDotWriter dotWriter = new ModelDotWriter(graphWriter);
        try {
            dotWriter.writeGraph(workflowModel);
            workflowModel.setVisualisationDot(graphWriter.toString());
        } catch (IOException ex) {
            logger.error("Failed to create DOT graph for workflow: " + ex.getMessage());
        }

        return workflowModel;

    } else {
        throw new IOException("File '" + workflowFile.getFileName() + "' is over singleFileSizeLimit - "
                + FileUtils.byteCountToDisplaySize(fileSizeBytes) + "/"
                + FileUtils.byteCountToDisplaySize(singleFileSizeLimit));
    }

}

From source file:org.commonwl.view.cwl.CWLService.java

/**
 * Get an overview of a workflow//from w  ww  . j av  a 2  s . c  o  m
 * @param file A file, potentially a workflow
 * @return A constructed WorkflowOverview of the workflow
 * @throws IOException Any API errors which may have occurred
 */
public WorkflowOverview getWorkflowOverview(File file) throws IOException {

    // Get the content of this file from Github
    long fileSizeBytes = file.length();

    // Check file size limit before parsing
    if (fileSizeBytes <= singleFileSizeLimit) {

        // Parse file as yaml
        JsonNode cwlFile = yamlStringToJson(readFileToString(file));

        // If the CWL file is packed there can be multiple workflows in a file
        int packedCount = 0;
        if (cwlFile.has(DOC_GRAPH)) {
            // Packed CWL, find the first subelement which is a workflow and take it
            for (JsonNode jsonNode : cwlFile.get(DOC_GRAPH)) {
                if (extractProcess(jsonNode) == CWLProcess.WORKFLOW) {
                    cwlFile = jsonNode;
                    packedCount++;
                }
            }
            if (packedCount > 1) {
                return new WorkflowOverview("/" + file.getName(), "Packed file",
                        "contains " + packedCount + " workflows");
            }
        }

        // Can only make an overview if this is a workflow
        if (extractProcess(cwlFile) == CWLProcess.WORKFLOW) {
            // Use filename for label if there is no defined one
            String label = extractLabel(cwlFile);
            if (label == null) {
                label = file.getName();
            }

            // Return the constructed overview
            return new WorkflowOverview("/" + file.getName(), label, extractDoc(cwlFile));
        } else {
            // Return null if not a workflow file
            return null;
        }
    } else {
        throw new IOException("File '" + file.getName() + "' is over singleFileSizeLimit - "
                + FileUtils.byteCountToDisplaySize(fileSizeBytes) + "/"
                + FileUtils.byteCountToDisplaySize(singleFileSizeLimit));
    }

}

From source file:org.commonwl.view.researchobject.ROBundleService.java

/**
 * Add files to this bundle from a list of repository contents
 * @param gitDetails The Git information for the repository
 * @param bundle The RO bundle to add files/directories to
 * @param bundlePath The current path within the RO bundle
 * @param gitRepo The Git repository/*w w w .  j  a v a  2  s  .c  o  m*/
 * @param repoPath The current path within the Git repository
 * @param authors The combined set of authors for al the files
 */
private void addFilesToBundle(GitDetails gitDetails, Bundle bundle, Path bundlePath, Git gitRepo, Path repoPath,
        Set<HashableAgent> authors, Workflow workflow) throws IOException {
    File[] files = repoPath.toFile().listFiles();
    for (File file : files) {
        if (!file.getName().equals(".git")) {
            if (file.isDirectory()) {

                // Create a new folder in the RO for this directory
                Path newBundlePath = bundlePath.resolve(file.getName());
                Files.createDirectory(newBundlePath);

                // Create git details object for subfolder
                GitDetails subfolderGitDetails = new GitDetails(gitDetails.getRepoUrl(), gitDetails.getBranch(),
                        Paths.get(gitDetails.getPath()).resolve(file.getName()).toString());

                // Add all files in the subdirectory to this new folder
                addFilesToBundle(subfolderGitDetails, bundle, newBundlePath, gitRepo,
                        repoPath.resolve(file.getName()), authors, workflow);

            } else {
                try {
                    // Where to store the new file
                    Path bundleFilePath = bundlePath.resolve(file.getName());
                    Path gitFolder = Paths.get(gitDetails.getPath());
                    String relativePath = gitFolder.resolve(file.getName()).toString();
                    Path gitPath = bundlePath.getRoot().resolve(relativePath); // would start with /

                    // Get direct URL permalink
                    URI rawURI = new URI("https://w3id.org/cwl/view/git/" + workflow.getLastCommit() + gitPath
                            + "?format=raw");

                    // Variable to store file contents and aggregation
                    String fileContent = null;
                    PathMetadata aggregation;

                    // Download or externally link if oversized
                    if (file.length() <= singleFileSizeLimit) {
                        // Save file to research object bundle
                        fileContent = readFileToString(file);
                        Bundles.setStringValue(bundleFilePath, fileContent);

                        // Set retrieved information for this file in the manifest
                        aggregation = bundle.getManifest().getAggregation(bundleFilePath);
                        aggregation.setRetrievedFrom(rawURI);
                        aggregation.setRetrievedBy(appAgent);
                        aggregation.setRetrievedOn(aggregation.getCreatedOn());
                    } else {
                        logger.info("File " + file.getName() + " is too large to download - "
                                + FileUtils.byteCountToDisplaySize(file.length()) + "/"
                                + FileUtils.byteCountToDisplaySize(singleFileSizeLimit)
                                + ", linking externally to RO bundle");

                        // Set information for this file in the manifest
                        aggregation = bundle.getManifest().getAggregation(rawURI);
                        Proxy bundledAs = new Proxy();
                        bundledAs.setURI();
                        bundledAs.setFolder(repoPath);
                        aggregation.setBundledAs(bundledAs);
                    }

                    // Special handling for cwl files
                    boolean cwl = FilenameUtils.getExtension(file.getName()).equals("cwl");
                    if (cwl) {
                        // Correct mime type (no official standard for yaml)
                        aggregation.setMediatype("text/x-yaml");

                        // Add conformsTo for version extracted from regex
                        if (fileContent != null) {
                            Matcher m = cwlVersionPattern.matcher(fileContent);
                            if (m.find()) {
                                aggregation.setConformsTo(new URI("https://w3id.org/cwl/" + m.group(1)));
                            }
                        }
                    }

                    try {
                        // Add authors from git commits to the file
                        Set<HashableAgent> fileAuthors = gitService.getAuthors(gitRepo, gitPath.toString());

                        if (cwl) {
                            // Attempt to get authors from cwl description - takes priority
                            ResultSet descAuthors = rdfService.getAuthors(
                                    bundlePath.resolve(file.getName()).toString().substring(10),
                                    workflow.getIdentifier());
                            if (descAuthors.hasNext()) {
                                QuerySolution authorSoln = descAuthors.nextSolution();
                                HashableAgent newAuthor = new HashableAgent();
                                if (authorSoln.contains("name")) {
                                    newAuthor.setName(authorSoln.get("name").toString());
                                }
                                if (authorSoln.contains("email")) {
                                    newAuthor.setUri(new URI(authorSoln.get("email").toString()));
                                }
                                if (authorSoln.contains("orcid")) {
                                    newAuthor.setOrcid(new URI(authorSoln.get("orcid").toString()));
                                }
                                fileAuthors.remove(newAuthor);
                                fileAuthors.add(newAuthor);
                            }
                        }

                        authors.addAll(fileAuthors);
                        aggregation.setAuthoredBy(new ArrayList<>(fileAuthors));
                    } catch (GitAPIException ex) {
                        logger.error("Could not get commits for file " + repoPath, ex);
                    }

                    // Set retrieved information for this file in the manifest
                    aggregation.setRetrievedFrom(rawURI);
                    aggregation.setRetrievedBy(appAgent);
                    aggregation.setRetrievedOn(aggregation.getCreatedOn());

                } catch (URISyntaxException ex) {
                    logger.error("Error creating URI for RO Bundle", ex);
                }
            }
        }
    }
}

From source file:org.craftercms.commons.monitoring.MemoryMonitor.java

/**
 * Private Constructor of the MemoryMonitor POJO
 * @param memName Type of MemoryMonitor to get the information.
 * @param memoryUsage {@link MemoryUsage} bean where the information is taken from.
 *//*from  w ww  . j  a va2  s  . com*/
private MemoryMonitor(String memName, MemoryUsage memoryUsage) {
    this.name = memName;
    this.init = FileUtils.byteCountToDisplaySize(memoryUsage.getInit());
    this.used = FileUtils.byteCountToDisplaySize(memoryUsage.getUsed());
    this.committed = FileUtils.byteCountToDisplaySize(memoryUsage.getCommitted());
    this.max = FileUtils.byteCountToDisplaySize(memoryUsage.getMax());
}