⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cbcache.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


    /**
     * Sorts an array of files in order of lastModified (oldest to newest).
     *
     * @param files the array of files to be sorted.
     * @return the sorted file array.
     */

    public static File[] sortFiles(File[] files)
    {
        Arrays.sort(files, new FileComparator());
        return files;
    }


    /**
     * Returns the directory that is being used for the caching.
     *
     * @return the directory.
     */

    public static File getCacheDirectory()
    {
        return fileDir;
    }


    /**
     * Returns the directory that is being used for the audio caching.
     *
     * @return the directory.
     */

    public static File getAudioCacheDirectory()
    {
        return audioFileDir;
    }


    /**
     * Decreases the size of the cache (usually if the cache contains 100 or more temporary files),
     * by deleting the oldest files until there is only 50 temporary files left in the cache.
     * It also checks that it deletes all the files pertaining to one entry i.e. if an entry has
     * four temporary files, this method will try to delete all four even if the minimum level of 50 is
     * reached (this ensures that the templates refresh correctly).
     */

    public static void decreaseCacheSize()
    {
        File[] fileArray = fileDir.listFiles();

        fileArray = sortFiles(fileArray);    					//TE: sort the files according to there last modified date (oldest to newest).

        for (int i = 0; i < (fileArray.length - CACHEMIN); i++)    	//TE: do the following until there is only 50 temp files left.
        {
            String fileName = fileArray[i].getName();

            for (int x = 0; x < allFiles.length; x++)    			//TE: make sure that when deleting a temp file, that all the temp files pertaining to the same entry are deleted also.
            {
                if (allFiles[x].startsWith(fileName.substring(0, ((fileArray[i].toString()).length()) - 15)))
                {
                    //TE: if the temp file in the directory at position 'x' starts with the the same name as the temp file listed at
                    //    position i of the sorted array (sorted by last modified), minus the last 15 characters (to ensure that the
                    //    'digit+.extension' is cut off the temp name), then delete it!...phew

                    File tempFile = new File(makeDir(), allFiles[x].toString());
                    tempFile.delete();
                }
            }
        }
        decreaseAudioCacheSize();
    }


    /**
     * Decreases the size of the audio cache (usually if the cache contains 100 or more temporary files),
     * by deleting the oldest files until there is only 50 temporary files left in the cache.
     * It also checks that it deletes all the files pertaining to one entry i.e. if an entry has
     * four temporary files, this method will try to delete all four even if the minimum level of 50 is
     * reached (this ensures that the templates refresh correctly).
     */

    public static void decreaseAudioCacheSize()
    {
        File[] fileArray = audioFileDir.listFiles();

        fileArray = sortFiles(fileArray);    					//TE: sort the files according to there last modified date (oldest to newest).

        for (int i = 0; i < (fileArray.length - CACHEMIN); i++)    	//TE: do the following until there is only 50 temp files left.
        {
            String fileName = fileArray[i].getName();

            for (int x = 0; x < allAudioFiles.length; x++)    		//TE: make sure that when deleting a temp file, that all the temp files pertaining to the same entry are deleted also.
            {
                if (allAudioFiles[x].startsWith(fileName.substring(0, ((fileArray[i].toString()).length()) - 15)))
                {
                    //TE: if the temp file in the directory at position 'x' starts with the the same name as the temp file listed at
                    //    position i of the sorted array (sorted by last modified), minus the last 15 characters (to ensure that the
                    //    'digit+.extension' is cut off the temp name), then delete it!...phew

                    File tempFile = new File(makeAudioDir(), allAudioFiles[x].toString());
                    tempFile.delete();
                }
            }
        }
    }


    /**
     * Creates the temporary directory, calling it 'temp'.
     *
     * @return the directory.
     */

    private static File makeDir()
    {
        File dir = new File(dirPath);
        dir.mkdir();
        dir.deleteOnExit();

        return dir;
    }


    /**
     * Creates the temporary audio directory, calling it 'temp/audio'.
     *
     * @return the directory.
     */

    private static File makeAudioDir()
    {
        File dir = new File(dirPath + File.separator + "audio");
        dir.mkdir();
        dir.deleteOnExit();

        return dir;
    }


    /**
     * Returns the absolute path of the temp directory.
     *
     * @return the path of the temp directroy.
     */

    public static String getDirPath()
    {
        return makeDir().getAbsolutePath();
    }


    /**
     * Sets the path of the temp directory.
     *
     * @param path the path of where to make the temp directory
     */

    public static String setDirPath(String path)
    {
        return dirPath = path;
    }


    /**
     * Returns the absolute path of the audio temp directory.
     *
     * @return the path of the temp directroy.
     */

    public static String getAudioDirPath()
    {
        return makeAudioDir().getAbsolutePath();
    }


    /**
     * Gets the byte[] value of a specified attribute value.
     *
     * @param name    the name of the attribute value e.g. 'jpegPhoto'.
     * @param entry   the entry that is to be displayed.
     * @param entries the number of attributes values (e.g. 'jpegPhoto') pertaining to the entry.
     * @return the byte array of the attribute value.
     */

    private static byte[] getAttByteValue(String name, Attributes entry, int entries)
    {
        try
        {
            Attribute a = entry.get(name);
            if (a == null) return null;    // no pre-existing value, so nothing to do.
            if (a.size() == 0 || a.get() == null) return null;    // no pre-existing value, so nothing to do.

            Object o = a.get(entries);    	//TE: gets the attribute value at a certain position i.e. if 3 attribute values it will get the one at the position stored in entires.

            if (o instanceof byte[])
                return (byte[]) o;

            return null;
        }
        catch (NamingException e)
        {
            log.log(Level.WARNING, "Form Value Error getting value for " + name + " value :\n ", e);    // not a terminal error.
            return null;
        }
    }


    /**
     * Cleans the cache of entries that start with the dn of the entry being modified.
     *
     * @param currentDN the dn of the entry being modified.
     */

    public static void cleanCache(String currentDN)
    {
        currentDN = Integer.toString(currentDN.hashCode());

        allFiles = makeDir().list();

        for (int i = 0; i < allFiles.length; i++)
        {
            if (allFiles[i].startsWith(currentDN.toString())) //&& allFiles[i].endsWith(extension))    //TE: check that the temp files are of the entry being modified.
            {
                File tempFile = new File(fileDir, allFiles[i].toString());
                tempFile.delete();
            }
        }

        allAudioFiles = makeAudioDir().list();

        for (int i = 0; i < allAudioFiles.length; i++)
        {
            if (allAudioFiles[i].startsWith(currentDN.toString())) //&& allFiles[i].endsWith(extension))    //TE: check that the temp files are of the entry being modified.
            {
                File tempFile = new File(audioFileDir, allAudioFiles[i].toString());
                tempFile.delete();
            }
        }
    }


    /**
     * Cleans the cache of all temporary entries by deleting them.
     */

    public static void cleanCache()
    {
        allFiles = makeDir().list();

        if (allFiles == null)
            return;

        for (int i = 0; i < allFiles.length; i++)
        {
            File tempFile = new File(makeDir(), allFiles[i].toString());
            tempFile.delete();
        }

        allAudioFiles = makeAudioDir().list();

        for (int i = 0; i < allAudioFiles.length; i++)
        {
            File tempFile = new File(makeAudioDir(), allAudioFiles[i].toString());
            tempFile.delete();
        }
    }


    /**
     * Sets the maximum cache size @CACHEMAX@
     *
     * @param size the maximum cache size.
     */

    public static void setMaxCacheSize(int size)
    {
        CACHEMAX = size;
    }


    /**
     * Sets the minimum cache size @CACHEMAX@
     *
     * @param size the minimum cache size.
     */

    public static void setMinCacheSize(int size)
    {
        CACHEMIN = size;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -