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:nl.strohalm.cyclos.controls.BasePublicAction.java

/**
 * The Struts standard execute method is reserved, being the executeAction the one that subclasses must implement
 *///from www.java  2s .c o  m
@Override
public final ActionForward execute(final ActionMapping mapping, final ActionForm actionForm,
        final HttpServletRequest request, final HttpServletResponse response) throws Exception {

    // Check for uploads that exceeded the max length
    final Boolean maxLengthExceeded = (Boolean) request
            .getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
    if (maxLengthExceeded != null && maxLengthExceeded) {
        final LocalSettings settings = settingsService.getLocalSettings();
        return ActionHelper.sendError(mapping, request, response, "error.maxUploadSizeExceeded",
                FileUtils.byteCountToDisplaySize(settings.getMaxUploadBytes()));
    }

    // Create an action context
    request.setAttribute("formAction", mapping.getPath());

    try {
        // Process the action
        return executeAction(mapping, actionForm, request, response);
    } catch (final PermissionDeniedException e) {
        return ActionHelper.sendError(mapping, request, response, "error.permissionDenied");
    } catch (final QueryParseException e) {
        return ActionHelper.sendError(mapping, request, response, "error.queryParse");
    } catch (final ImageHelper.UnknownImageTypeException e) {
        final String recognizedTypes = StringUtils.join(ImageType.values(), ", ");
        return ActionHelper.sendError(mapping, request, response, "error.unknownImageType", recognizedTypes);
    } catch (final ValidationException e) {
        return ActionHelper.handleValidationException(mapping, request, response, e);
    } catch (final ExternalException e) {
        return ActionHelper.sendErrorWithMessage(mapping, request, response, e.getMessage());
    } catch (final Exception e) {
        actionHelper.generateLog(request, getServlet().getServletContext(), e);
        LOG.error("Application error on " + getClass().getName(), e);
        return ActionHelper.sendError(mapping, request, response, null);
    }
}

From source file:nl.strohalm.cyclos.taglibs.FormatTag.java

@Override
public int doEndTag() throws JspException {
    String out = "";
    final LocalSettings localSettings = getLocalSettings();
    if (number != null) {
        if (number instanceof Double || number instanceof BigDecimal || number instanceof Float) {
            final BigDecimal theNumber = CoercionHelper.coerce(BigDecimal.class, number);
            NumberConverter<BigDecimal> numberConverter;
            if (StringUtils.isNotEmpty(unitsPattern)) {
                numberConverter = localSettings.getUnitsConverter(unitsPattern);
            } else if (precision == null) {
                numberConverter = localSettings.getNumberConverter();
            } else {
                numberConverter = localSettings.getNumberConverterForPrecision(precision);
            }/* w  ww  .  j  a v a  2 s.c  om*/
            out = numberConverter.toString(theNumber);
            if (forceSignal && theNumber.compareTo(BigDecimal.ZERO) > 0) {
                out = "+" + out;
            }
        } else if (number instanceof StatisticalNumber) {
            out = convertStatisticalNumber((StatisticalNumber) number);
        } else if (number instanceof BigInteger && cardNumberPattern != null) {
            out = new CardNumberConverter(cardNumberPattern).toString((BigInteger) number);
        } else {
            final NumberConverter<Long> longConverter = localSettings.getLongConverter();
            final Long theNumber = CoercionHelper.coerce(Long.class, number);
            out = longConverter.toString(theNumber);
        }
    } else if (amount != null) {
        Converter<Amount> converter;
        if (StringUtils.isEmpty(unitsPattern)) {
            converter = localSettings.getAmountConverter();
        } else {
            converter = localSettings.getAmountConverter(unitsPattern);
        }
        out = converter.toString(amount);
    } else if (rawDate != null) {
        out = localSettings.getRawDateConverter().toString(rawDate);
    } else if (date != null) {
        out = localSettings.getDateConverter().toString(date);
    } else if (dateTime != null) {
        out = localSettings.getDateTimeConverter().toString(dateTime);
    } else if (time != null) {
        out = localSettings.getTimeConverter().toString(time);
    } else if (bytes != null) {
        out = FileUtils.byteCountToDisplaySize(bytes);
    } else {
        out = defaultValue == null ? null : defaultValue.toString();
    }
    try {
        if (StringUtils.isNotEmpty(out)) {
            pageContext.getOut().print(out);
        }
    } catch (final IOException e) {
        throw new JspException(e);
    } finally {
        release();
    }
    return EVAL_PAGE;
}

From source file:ome.formats.importer.gui.DebugMessenger.java

public void update(IObservable importLibrary, ImportEvent event) {
    if (event instanceof ImportEvent.FILE_SIZE_STEP) {
        ImportEvent.FILE_SIZE_STEP ev = (ImportEvent.FILE_SIZE_STEP) event;

        total_files = ev.total_files;//  ww  w .  jav a2s.co m
        total_file_length = ev.total_files_length;

        file_info = "Total files: " + total_files + " (" + FileUtils.byteCountToDisplaySize(total_file_length)
                + ")";
        if (ev.total_files > 100 || ev.total_files_length >= 104857600)
            file_info = "<html>Send the image files for these errors. <font color='AA0000'>" + file_info
                    + "</font></html>";
        else
            file_info = "Send the image files for these errors. " + file_info;

        //System.out.println(file_info);
        uploadCheckmark.setText(file_info);
        uploadCheckmark.repaint();
    }
}

From source file:omero.gateway.model.ImportCallback.java

/**
 * Sets the collection of files to import.
 * //from  ww  w  .  j  a  v a 2 s .  co m
 * @param usedFiles The value to set.
 */
public void setUsedFiles(String[] usedFiles) {
    if (usedFiles == null)
        return;
    for (int i = 0; i < usedFiles.length; i++) {
        sizeUpload += (new File(usedFiles[i])).length();
    }
    fileSize = FileUtils.byteCountToDisplaySize(sizeUpload);
    String[] values = fileSize.split(" ");
    if (values.length > 1)
        units = values[1];
}

From source file:org.apache.activemq.broker.jmx.ConcurrentMoveTest.java

public void testConcurrentMove() throws Exception {

    // Send some messages
    connection = connectionFactory.createConnection();
    connection.start();/*w w  w  .  jav  a  2 s .  co m*/
    Session session = connection.createSession(transacted, authMode);
    destination = createDestination();
    MessageProducer producer = session.createProducer(destination);
    for (int i = 0; i < messageCount; i++) {
        Message message = session.createTextMessage("Message: " + i);
        producer.send(message);
    }

    long usageBeforMove = broker.getPersistenceAdapter().size();
    LOG.info("Store usage:" + usageBeforMove);

    // Now get the QueueViewMBean and purge
    String objectNameStr = broker.getBrokerObjectName().toString();
    objectNameStr += ",destinationType=Queue,destinationName=" + getDestinationString();
    ObjectName queueViewMBeanName = assertRegisteredObjectName(objectNameStr);
    final QueueViewMBean proxy = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
            queueViewMBeanName, QueueViewMBean.class, true);

    final ActiveMQQueue to = new ActiveMQQueue("TO");
    ((RegionBroker) broker.getRegionBroker()).addDestination(broker.getAdminConnectionContext(), to, false);

    ExecutorService executorService = Executors.newCachedThreadPool();
    for (int i = 0; i < 50; i++) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    proxy.moveMatchingMessagesTo(null, to.getPhysicalName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    executorService.shutdown();
    executorService.awaitTermination(5, TimeUnit.MINUTES);

    long count = proxy.getQueueSize();
    assertEquals("Queue size", count, 0);
    assertEquals("Browse size", proxy.browseMessages().size(), 0);

    objectNameStr = broker.getBrokerObjectName().toString();
    objectNameStr += ",destinationType=Queue,destinationName=" + to.getQueueName();
    queueViewMBeanName = assertRegisteredObjectName(objectNameStr);
    QueueViewMBean toProxy = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
            queueViewMBeanName, QueueViewMBean.class, true);

    count = toProxy.getQueueSize();
    assertEquals("Queue size", count, messageCount);

    long usageAfterMove = broker.getPersistenceAdapter().size();
    LOG.info("Store usage, before: " + usageBeforMove + ", after:" + usageAfterMove);
    LOG.info("Store size increase:" + FileUtils.byteCountToDisplaySize(usageAfterMove - usageBeforMove));

    assertTrue("Usage not more than doubled", usageAfterMove < (usageBeforMove * 3));

    producer.close();
}

From source file:org.apache.drill.exec.physical.impl.join.HashJoinBatch.java

/**
 * Tunes the number of partitions used by {@link HashJoinBatch}. If it is not possible to spill it gives up and reverts
 * to unbounded in memory operation./*from   w  w w. jav  a  2  s.co m*/
 * @param maxBatchSize
 * @param buildCalc
 * @return
 */
private HashJoinMemoryCalculator.BuildSidePartitioning partitionNumTuning(int maxBatchSize,
        HashJoinMemoryCalculator.BuildSidePartitioning buildCalc) {
    // Get auto tuning result
    numPartitions = buildCalc.getNumPartitions();

    if (logger.isDebugEnabled()) {
        logger.debug(buildCalc.makeDebugString());
    }

    if (buildCalc.getMaxReservedMemory() > allocator.getLimit()) {
        // We don't have enough memory to do any spilling. Give up and do no spilling and have no limits

        // TODO dirty hack to prevent regressions. Remove this once batch sizing is implemented.
        // We don't have enough memory to do partitioning, we have to do everything in memory
        final String message = String.format(
                "When using the minimum number of partitions %d we require %s memory but only have %s available. "
                        + "Forcing legacy behavoir of using unbounded memory in order to prevent regressions.",
                numPartitions, FileUtils.byteCountToDisplaySize(buildCalc.getMaxReservedMemory()),
                FileUtils.byteCountToDisplaySize(allocator.getLimit()));
        logger.warn(message);

        // create a Noop memory calculator
        final HashJoinMemoryCalculator calc = getCalculatorImpl();
        calc.initialize(false);
        buildCalc = calc.next();

        buildCalc.initialize(true, true, // TODO Fix after growing hash values bug fixed
                buildBatch, probeBatch, buildJoinColumns, allocator.getLimit(), numPartitions,
                RECORDS_PER_BATCH, RECORDS_PER_BATCH, maxBatchSize, maxBatchSize,
                batchMemoryManager.getOutputRowCount(), batchMemoryManager.getOutputBatchSize(),
                HashTable.DEFAULT_LOAD_FACTOR);

        disableSpilling(null);
    }

    return buildCalc;
}

From source file:org.apache.drill.exec.physical.impl.join.HashJoinBatch.java

/**
 *  The constructor/* w ww.  ja  va  2 s . c  om*/
 *
 * @param popConfig
 * @param context
 * @param left  -- probe/outer side incoming input
 * @param right -- build/iner side incoming input
 * @throws OutOfMemoryException
 */
public HashJoinBatch(HashJoinPOP popConfig, FragmentContext context,
        RecordBatch left, /*Probe side record batch*/
        RecordBatch right /*Build side record batch*/
) throws OutOfMemoryException {
    super(popConfig, context, true, left, right);
    this.buildBatch = right;
    this.probeBatch = left;
    joinType = popConfig.getJoinType();
    conditions = popConfig.getConditions();
    this.popConfig = popConfig;

    rightExpr = new ArrayList<>(conditions.size());
    buildJoinColumns = Sets.newHashSet();

    for (int i = 0; i < conditions.size(); i++) {
        final SchemaPath rightPath = (SchemaPath) conditions.get(i).getRight();
        final PathSegment.NameSegment nameSegment = (PathSegment.NameSegment) rightPath.getLastSegment();
        buildJoinColumns.add(nameSegment.getPath());
        final String refName = "build_side_" + i;
        rightExpr.add(new NamedExpression(conditions.get(i).getRight(), new FieldReference(refName)));
    }

    this.allocator = oContext.getAllocator();

    numPartitions = (int) context.getOptions().getOption(ExecConstants.HASHJOIN_NUM_PARTITIONS_VALIDATOR);
    if (numPartitions == 1) { //
        disableSpilling("Spilling is disabled due to configuration setting of num_partitions to 1");
    }

    numPartitions = BaseAllocator.nextPowerOfTwo(numPartitions); // in case not a power of 2

    final long memLimit = context.getOptions().getOption(ExecConstants.HASHJOIN_MAX_MEMORY_VALIDATOR);

    if (memLimit != 0) {
        allocator.setLimit(memLimit);
    }

    RECORDS_PER_BATCH = (int) context.getOptions()
            .getOption(ExecConstants.HASHJOIN_NUM_ROWS_IN_BATCH_VALIDATOR);
    maxBatchesInMemory = (int) context.getOptions()
            .getOption(ExecConstants.HASHJOIN_MAX_BATCHES_IN_MEMORY_VALIDATOR);

    logger.info("Memory limit {} bytes", FileUtils.byteCountToDisplaySize(allocator.getLimit()));
    spillSet = new SpillSet(context, popConfig);

    // Create empty partitions (in the ctor - covers the case where right side is empty)
    partitions = new HashPartition[0];

    // get the output batch size from config.
    int configuredBatchSize = (int) context.getOptions().getOption(ExecConstants.OUTPUT_BATCH_SIZE_VALIDATOR);
    batchMemoryManager = new JoinBatchMemoryManager(configuredBatchSize, left, right);
}

From source file:org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreHelper.java

private static Iterable<BlobReferences> scan(DocumentNodeStore store, Comparator<BlobReferences> comparator,
        int num) {
    long totalGarbage = 0;
    Iterable<NodeDocument> docs = getDocuments(store.getDocumentStore());
    PriorityQueue<BlobReferences> queue = new PriorityQueue<BlobReferences>(num, comparator);
    List<Blob> blobs = Lists.newArrayList();
    long docCount = 0;
    for (NodeDocument doc : docs) {
        if (++docCount % 10000 == 0) {
            System.out.print(".");
        }//from  www  . j  a v  a  2 s.  c  o m
        blobs.clear();
        BlobReferences refs = collectReferences(doc, store);
        totalGarbage += refs.garbageSize;
        queue.add(refs);
        if (queue.size() > num) {
            queue.remove();
        }
    }

    System.out.println();
    List<BlobReferences> refs = Lists.newArrayList();
    refs.addAll(queue);
    Collections.sort(refs, Collections.reverseOrder(comparator));
    System.out.println("Total garbage size: " + FileUtils.byteCountToDisplaySize(totalGarbage));
    System.out.println("Total number of nodes with blob references: " + docCount);
    System.out.println("total referenced / old referenced / # blob references / path");
    return refs;
}

From source file:org.apache.james.cli.ServerCmd.java

private String formatStorageValue(long value) {
    if (value == Quota.UNKNOWN) {
        return ValueWithUnit.UNKNOWN;
    }// w  ww. j  av a2  s  . co m
    if (value == Quota.UNLIMITED) {
        return ValueWithUnit.UNLIMITED;
    }
    return FileUtils.byteCountToDisplaySize(value);
}

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

/**
 * Statistics about the lite jar such as jar size.
 *//*from  w ww  .j a  va  2s  .  c o m*/
private static void liteJarStats() {
    File f = new File(liteJarLocation);
    if (f.exists()) {
        Long jarSize = f.length();
        String jarSizeDisplay = FileUtils.byteCountToDisplaySize(jarSize);
        System.out.println("\nFinished creating " + liteJarLocation + " (" + jarSizeDisplay + " ["
                + NumberFormat.getInstance().format(jarSize) + " bytes])");
    } else {
        System.out.println(liteJarLocation + " could not be found");
    }
}