Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2016-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the license found in the
 * LICENSE-examples file in the root directory of this source tree.
 */

import android.os.StatFs;

import java.io.File;

public class Main {
    /**
     * Helper method to calculate the proper size limit of a cache instance.
     */
    public static long getCacheSizeInBytes(File dir, float cacheSizePercent, long maxSizeInBytes) {
        if (dir == null || (!dir.exists() && !dir.mkdir())) {
            return 0;
        }
        try {
            StatFs stat = new StatFs(dir.getPath());
            long totalBytes = stat.getBlockCountLong() * stat.getBlockSizeLong();
            long freeBytes = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
            long desiredBytes = Math.min((long) (totalBytes * cacheSizePercent), maxSizeInBytes);
            // If free space is less than desired, use half of the free disk space instead.
            desiredBytes = (desiredBytes > freeBytes) ? freeBytes / 2 : desiredBytes;
            return desiredBytes;
        } catch (IllegalArgumentException e) {
            return 0;
        }
    }
}