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:info.magnolia.ui.form.field.upload.basic.BasicUploadProgressIndicator.java

@Override
public void refreshLayout(long readBytes, long contentLength, String fileName) {
    progressIndicator.setValue(Float.valueOf(readBytes / (float) contentLength));

    uploadFileLocation.setValue(i18n.translate(this.inProgressCaption, fileName));

    uploadFileProgress.setValue(createPercentage(readBytes, contentLength));

    String bytesRead = FileUtils.byteCountToDisplaySize(readBytes);
    String totalBytes = FileUtils.byteCountToDisplaySize(contentLength);
    uploadFileRatio.setValue(i18n.translate(this.inProgressRatioCaption, bytesRead, totalBytes));
}

From source file:at.becast.youploader.youtube.GuiUploadEvent.java

@Override
public void onRead(long length, long position, long size) {
    long now = System.currentTimeMillis();
    if (this.step < now - 2000) {
        frame.getProgressBar().setString(String.format("%6.2f%%", (float) position / size * 100));
        frame.getProgressBar().setValue((int) ((float) position / size * 100));
        frame.getProgressBar().revalidate();
        frame.revalidate();/*  w  ww  .  j a  va 2 s . com*/
        frame.repaint();
        if (lastdb < now - 10000) {
            SQLite.updateUploadProgress(frame.upload_id, position);
            this.lastdb = now;
        }
        this.step = now;
        this.dataDelta = position - this.lastdata;
        this.timeDelta = this.step - this.lasttime;
        this.lasttime = this.step;
        this.lastdata = position;
        long speed = this.dataDelta / (this.timeDelta + 1) * 1000 + 1;
        long duration = ((size - position) / speed) * 1000;
        String time = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(duration),
                TimeUnit.MILLISECONDS.toMinutes(duration)
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration)),
                TimeUnit.MILLISECONDS.toSeconds(duration)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
        frame.getLblKbs().setText(FileUtils.byteCountToDisplaySize(speed) + "/s");
        frame.getLblETA().setText(time);
        if (Main.debug) {
            LOG.debug("Took {} ms to refresh, Uploaded {} bytes, Speed {} ", System.currentTimeMillis() - now,
                    this.dataDelta, FileUtils.byteCountToDisplaySize(speed));
        }
    }
}

From source file:net.sf.firemox.tools.Picture.java

/**
 * Download a file from the specified URL to the specified local file.
 * /*w w w.  j  a v  a  2 s .com*/
 * @param localFile
 *          is the new card's picture to try first
 * @param remoteFile
 *          is the URL where this picture will be downloaded in case of the
 *          specified card name has not been found locally.
 * @param listener
 *          the component waiting for this picture.
 * @since 0.83 Empty file are deleted to force file to be downloaded.
 */
public static synchronized void download(String localFile, URL remoteFile, MonitoredCheckContent listener) {
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    File toDownload = new File(localFile);
    if (toDownload.exists() && toDownload.length() == 0 && toDownload.canWrite()) {
        toDownload.delete();
    }
    if (!toDownload.exists() || (toDownload.length() == 0 && toDownload.canWrite())) {
        // the file has to be downloaded
        try {
            if ("file".equals(remoteFile.getProtocol())) {
                File localRemoteFile = MToolKit
                        .getFile(remoteFile.toString().substring(7).replaceAll("%20", " "), false);
                int contentLength = (int) localRemoteFile.length();
                Log.info("Copying from " + localRemoteFile.getAbsolutePath());
                LoaderConsole.beginTask(
                        LanguageManager.getString("downloading") + " " + localRemoteFile.getAbsolutePath() + "("
                                + FileUtils.byteCountToDisplaySize(contentLength) + ")");

                // Copy file
                in = new BufferedInputStream(new FileInputStream(localRemoteFile));
                byte[] buf = new byte[2048];
                int currentLength = 0;
                boolean succeed = false;
                for (int bufferLen = in.read(buf); bufferLen >= 0; bufferLen = in.read(buf)) {
                    if (!succeed) {
                        toDownload.getParentFile().mkdirs();
                        out = new BufferedOutputStream(new FileOutputStream(localFile));
                        succeed = true;
                    }
                    currentLength += bufferLen;
                    if (out != null) {
                        out.write(buf, 0, bufferLen);
                    }
                    if (listener != null) {
                        listener.updateProgress(contentLength, currentLength);
                    }
                }

                // Step 3: close streams
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
                in = null;
                out = null;
                return;
            }

            // Testing mode?
            if (!MagicUIComponents.isUILoaded()) {
                return;
            }

            // Step 1: open streams
            final URLConnection connection = MToolKit.getHttpConnection(remoteFile);
            int contentLength = connection.getContentLength();
            in = new BufferedInputStream(connection.getInputStream());
            Log.info("Download from " + remoteFile + "(" + FileUtils.byteCountToDisplaySize(contentLength)
                    + ")");
            LoaderConsole.beginTask(LanguageManager.getString("downloading") + " " + remoteFile + "("
                    + FileUtils.byteCountToDisplaySize(contentLength) + ")");

            // Step 2: read and write until done
            byte[] buf = new byte[2048];
            int currentLength = 0;
            boolean succeed = false;
            for (int bufferLen = in.read(buf); bufferLen >= 0; bufferLen = in.read(buf)) {
                if (!succeed) {
                    toDownload.getParentFile().mkdirs();
                    out = new BufferedOutputStream(new FileOutputStream(localFile));
                    succeed = true;
                }
                currentLength += bufferLen;
                if (out != null) {
                    out.write(buf, 0, bufferLen);
                }
                if (listener != null) {
                    listener.updateProgress(contentLength, currentLength);
                }
            }

            // Step 3: close streams
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
            in = null;
            out = null;
            return;
        } catch (IOException e1) {
            if (MToolKit.getFile(localFile) != null) {
                MToolKit.getFile(localFile).delete();
            }
            if (remoteFile.getFile().equals(remoteFile.getFile().toLowerCase())) {
                Log.fatal("could not load picture " + localFile + " from URL " + remoteFile + ", "
                        + e1.getMessage());
            }
            String tmpRemote = remoteFile.toString().toLowerCase();
            try {
                download(localFile, new URL(tmpRemote), listener);
            } catch (MalformedURLException e) {
                Log.fatal("could not load picture " + localFile + " from URL " + tmpRemote + ", "
                        + e.getMessage());
            }
        }
    }
}

From source file:mitm.djigzo.web.pages.portal.pdf.PDFAttachments.java

public String getMaxAttachmentSizeReadable() {
    return FileUtils.byteCountToDisplaySize(getMaxAttachmentSize());
}

From source file:com.linkedin.drelephant.mapreduce.heuristics.MapperSpeedHeuristic.java

@Override
public HeuristicResult apply(MapReduceApplicationData data) {

    if (!data.getSucceeded()) {
        return null;
    }//from   ww w .j a  v a 2  s  .c  o m

    MapReduceTaskData[] tasks = data.getMapperData();

    List<Long> inputByteSizes = new ArrayList<Long>();
    List<Long> speeds = new ArrayList<Long>();
    List<Long> runtimesMs = new ArrayList<Long>();

    for (MapReduceTaskData task : tasks) {

        if (task.isSampled()) {
            long inputBytes = task.getCounters().get(MapReduceCounterData.CounterName.HDFS_BYTES_READ);
            long runtimeMs = task.getTotalRunTimeMs();
            inputByteSizes.add(inputBytes);
            runtimesMs.add(runtimeMs);
            //Speed is bytes per second
            speeds.add((1000 * inputBytes) / (runtimeMs));
        }
    }

    long medianSpeed;
    long medianSize;
    long medianRuntimeMs;

    if (tasks.length != 0) {
        medianSpeed = Statistics.median(speeds);
        medianSize = Statistics.median(inputByteSizes);
        medianRuntimeMs = Statistics.median(runtimesMs);
    } else {
        medianSpeed = 0;
        medianSize = 0;
        medianRuntimeMs = 0;
    }

    Severity severity = getDiskSpeedSeverity(medianSpeed);

    //This reduces severity if task runtime is insignificant
    severity = Severity.min(severity, getRuntimeSeverity(medianRuntimeMs));

    HeuristicResult result = new HeuristicResult(_heuristicConfData.getClassName(),
            _heuristicConfData.getHeuristicName(), severity, Utils.getHeuristicScore(severity, tasks.length));

    result.addResultDetail("Number of tasks", Integer.toString(tasks.length));
    result.addResultDetail("Median task input size", FileUtils.byteCountToDisplaySize(medianSize));
    result.addResultDetail("Median task runtime", Statistics.readableTimespan(medianRuntimeMs));
    result.addResultDetail("Median task speed", FileUtils.byteCountToDisplaySize(medianSpeed) + "/s");

    return result;
}

From source file:com.linkedin.drelephant.tez.heuristics.MapperSpeedHeuristic.java

public HeuristicResult apply(TezApplicationData data) {

    if (!data.getSucceeded()) {
        return null;
    }/* w  w  w .j  a v a2  s. c o m*/

    TezTaskData[] tasks = data.getMapTaskData();

    List<Long> inputSizes = new ArrayList<Long>();
    List<Long> speeds = new ArrayList<Long>();
    List<Long> runtimesMs = new ArrayList<Long>();

    for (TezTaskData task : tasks) {

        if (task.isSampled()) {

            long inputBytes = 0;

            for (TezCounterData.CounterName counterName : _counterNames) {
                inputBytes += task.getCounters().get(counterName);
            }

            long runtimeMs = task.getTotalRunTimeMs();
            inputSizes.add(inputBytes);
            runtimesMs.add(runtimeMs);
            //Speed is records per second
            speeds.add((1000 * inputBytes) / (runtimeMs));
        }
    }

    long medianSpeed;
    long medianSize;
    long medianRuntimeMs;

    if (tasks.length != 0) {
        medianSpeed = Statistics.median(speeds);
        medianSize = Statistics.median(inputSizes);
        medianRuntimeMs = Statistics.median(runtimesMs);
    } else {
        medianSpeed = 0;
        medianSize = 0;
        medianRuntimeMs = 0;
    }

    Severity severity = getDiskSpeedSeverity(medianSpeed);

    //This reduces severity if task runtime is insignificant
    severity = Severity.min(severity, getRuntimeSeverity(medianRuntimeMs));

    HeuristicResult result = new HeuristicResult(_heuristicConfData.getClassName(),
            _heuristicConfData.getHeuristicName(), severity, Utils.getHeuristicScore(severity, tasks.length));

    result.addResultDetail("Number of tasks", Integer.toString(tasks.length));
    result.addResultDetail("Median task input ", FileUtils.byteCountToDisplaySize(medianSize));
    result.addResultDetail("Median task runtime", Statistics.readableTimespan(medianRuntimeMs));
    result.addResultDetail("Median task speed", FileUtils.byteCountToDisplaySize(medianSpeed) + "/s");

    return result;
}

From source file:it.anyplace.sync.bep.BlockPusher.java

public FileUploadObserver pushFile(InputStream inputStream, @Nullable FileInfo fileInfo, final String folder,
        final String path) {
    try {/*  w  w  w. j  a  va2s .  c om*/
        File tempFile = createTempFile(configuration);
        FileUtils.copyInputStreamToFile(inputStream, tempFile);
        logger.debug("use temp file = {} {}", tempFile, FileUtils.byteCountToDisplaySize(tempFile.length()));
        return pushFile(new FileDataSource(tempFile), fileInfo, folder, path); //TODO temp file cleanup on complete
        //TODO use mem source on small file
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.linkedin.drelephant.mapreduce.heuristics.MapperTimeHeuristic.java

@Override
public HeuristicResult apply(MapReduceApplicationData data) {

    if (!data.getSucceeded()) {
        return null;
    }//from  w w w  .  j  av a 2 s . co  m

    MapReduceTaskData[] tasks = data.getMapperData();

    List<Long> inputBytes = new ArrayList<Long>();
    List<Long> runtimesMs = new ArrayList<Long>();
    long taskMinMs = Long.MAX_VALUE;
    long taskMaxMs = 0;

    for (MapReduceTaskData task : tasks) {

        if (task.isSampled()) {
            inputBytes.add(task.getCounters().get(MapReduceCounterData.CounterName.HDFS_BYTES_READ));
            long taskTime = task.getTotalRunTimeMs();
            runtimesMs.add(taskTime);
            taskMinMs = Math.min(taskMinMs, taskTime);
            taskMaxMs = Math.max(taskMaxMs, taskTime);
        }
    }

    if (taskMinMs == Long.MAX_VALUE) {
        taskMinMs = 0;
    }

    long averageSize = Statistics.average(inputBytes);
    long averageTimeMs = Statistics.average(runtimesMs);

    Severity shortTaskSeverity = shortTaskSeverity(tasks.length, averageTimeMs);
    Severity longTaskSeverity = longTaskSeverity(tasks.length, averageTimeMs);
    Severity severity = Severity.max(shortTaskSeverity, longTaskSeverity);

    HeuristicResult result = new HeuristicResult(_heuristicConfData.getClassName(),
            _heuristicConfData.getHeuristicName(), severity, Utils.getHeuristicScore(severity, tasks.length));

    result.addResultDetail("Number of tasks", Integer.toString(tasks.length));
    result.addResultDetail("Average task input size", FileUtils.byteCountToDisplaySize(averageSize));
    result.addResultDetail("Average task runtime", Statistics.readableTimespan(averageTimeMs));
    result.addResultDetail("Max task runtime", Statistics.readableTimespan(taskMaxMs));
    result.addResultDetail("Min task runtime", Statistics.readableTimespan(taskMinMs));

    return result;
}

From source file:de.interactive_instruments.etf.testrunner.basex.BasexTestRunTask.java

@Override
protected TestReport doStartTestRunnner() throws IOException, QueryException, InvalidParameterException,
        InterruptedException, InvalidStateTransitionException, SAXException {
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

    this.projDir.expectDirIsReadable();
    this.projectFile.expectIsReadable();

    final IFile testDataDirDir = new IFile(testRun.getTestObject()
            .getResourcById(EidFactory.getDefault().createFromStrAsStr("data")).getURI().getPath(),
            this.dbName);
    testDataDirDir.expectDirIsReadable();

    getBsxTaskProgress().advance();//from   w ww.j  av  a  2 s  .  c o m
    checkUserParameters();

    final String regex = testRun.getTestObject().getProperty("regex");

    ((AbstractTestReport) testRun.getReport()).setTestRunProperties(testRun);

    PathFilter filter;
    if (regex != null && !regex.isEmpty()) {
        filter = new BasexFileFilter(new RegexFileFilter(regex));
    } else {
        filter = new BasexFileFilter();
    }

    final BasexDbPartitioner partitioner = new BasexDbPartitioner(maxDbChunkSize, this.taskProgress.getLogger(),
            testDataDirDir.toPath(), this.dbName, filter);
    String skippedFiles = "";
    if (testRun.isTestObjectResourceUpdateRequired()) {
        logInfo("The test object was just created or the resources of the test object "
                + " changed: creating new tests databases!");

        for (int i = 0; i < 10000; i++) {
            boolean dropped = Boolean.valueOf(new DropDB(this.dbName + "-" + i).execute(ctx));
            if (dropped) {
                logInfo("Database " + i + " dropped");
            } else {
                break;
            }
        }
        skippedFiles = createDatabases(partitioner);
    } else {
        partitioner.dryRun();
        logInfo("Reusing existing database with " + partitioner.getFileCount() + " indexed files ("
                + FileUtils.byteCountToDisplaySize(partitioner.getSize()) + ")");
    }
    for (int i = 0; i < partitioner.getDbCount(); i++) {
        new Open(this.dbName + "-" + i).execute(ctx);
    }
    fireInitialized();

    checkCancelStatus();
    getBsxTaskProgress().advance();
    getBsxTaskProgress().advance();

    fireRunning();
    // Validate against schema if schema file is set
    String schemaFilePath = this.testRun.getProperty("Schema_file");
    if (SUtils.isNullOrEmpty(schemaFilePath)) {
        // STD fallback: check for a schema.xsd named file
        final String stdSchemaFile = "schema.xsd";
        if (projDir.secureExpandPathDown(stdSchemaFile).exists()) {
            schemaFilePath = stdSchemaFile;
        } else {
            // project specific fallback
            // TODO: remove in future
            final String lodLevel = this.testRun.getProperty("Level_of_Detail");
            if (!SUtils.isNullOrEmpty(lodLevel)) {
                schemaFilePath = "schema/citygml/CityGML_LOD" + lodLevel + ".xsd";
            }
        }
    }
    final MultiThreadedSchemaValidator mtsv;
    if (!SUtils.isNullOrEmpty(schemaFilePath)) {
        final IFile schemaFile = projDir.secureExpandPathDown(schemaFilePath);
        schemaFile.expectIsReadable();
        logInfo("Starting parallel schema validation");
        mtsv = new MultiThreadedSchemaValidator(testDataDirDir, filter, schemaFile);
        mtsv.validate();
        logInfo("Validation ended with " + mtsv.getErrorCount() + " error(s)");
        if (mtsv.getErrorCount() > 0) {
            logInfo("Non-schema-compliant files can not be tested and are therefore excluded from further testing:");
            for (final File file : mtsv.getInvalidFiles()) {
                logInfo(" - " + file.getName());
                new Delete(file.getName()).execute(ctx);
            }
            new Flush().execute(ctx);
        }
    } else {
        mtsv = null;
        logInfo("Skipping validation due to no schema file was set.");
    }
    getBsxTaskProgress().advance();
    getBsxTaskProgress().advance();
    checkCancelStatus();

    // Load the test project as XQuery
    proc = new QueryProcessor(projectFile.readContent().toString(), ctx);
    getBsxTaskProgress().setQueryProc(proc);

    // Bind script variables
    // Workaround: Wrap File around URI for a clean path or basex will
    // throw an exception
    proc.bind("outputFile", new File(testRun.getReport().getPublicationLocation().getPath()).getPath());
    proc.bind("projDir", projDir);
    proc.bind("dbBaseName", this.dbName);
    proc.bind("dbDir", testDataDirDir.getPath());
    proc.bind("dbCount", partitioner.getDbCount());
    proc.bind("reportId", testRun.getReport().getId());
    proc.bind("reportLabel", testRun.getReport().getLabelOrId());
    proc.bind("reportStartTimestamp", taskProgress.getStartDate().getTime());
    proc.bind("testObjectId", testRun.getTestObject().getId());

    // Add errors about not well-formed or invalid XML data
    final String validationErrors;
    if (mtsv != null) {
        // Add Schema errors + not well-formed xml file errors (if applicable)
        validationErrors = skippedFiles + mtsv.getErrorMessages();
        mtsv.release();
    } else if (!SUtils.isNullOrEmpty(skippedFiles)) {
        // No schema file found, but not well-formed files
        validationErrors = skippedFiles;
    } else {
        // No errors
        validationErrors = "";
    }
    proc.bind("validationErrors", validationErrors);

    setUserParameters();

    logInfo("Compiling test script");
    proc.compile();
    getBsxTaskProgress().advance();
    checkCancelStatus();

    logInfo("Starting xquery tests");
    // Execute the XQuery script
    // Version 8
    // final Value result = proc.value();
    proc.execute();

    return testRun.getReport();
}