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

📄 cbcache.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ca.commons.cbutil;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * This class creates a cache directory called 'temp' where temporary files are stored.
 * Currently it works for audio, jpegPhoto and odDocumentDOC attributes.  The audio
 * are stored in a sub dir called 'audio'.  It also contains methods
 * for decreasing the cache size, cleaning (or emptying the cache and sorting the
 * temporary files according to their last modified date.
 *
 * @author Trudi.
 */

public class CBCache
{
    private static int counter = 0;               //TE: a counter that is used to but a unique number on the temp files.
    private static int CACHEMAX = 100;            //TE: the maximum size the cache should ever reach before decreasing its size.
    private static int CACHEMIN = 50;             //TE: the size the cache gets minimized to when decreaseing its size.
    private static File fileDir = null;            //TE: the directory that the temp files are stored.
    private static File audioFileDir = null;       //TE: the directory that the audio temp files are stored.
    private static String allFiles[];                //TE: an array to store the files within the temp directory.
    private static String allAudioFiles[];           //TE: an array to store the files within the audio temp directory.
    private static String extension = ".jpg";        //TE: the extension of the temporary file.
    private static String dirPath = "temp";

    private static Logger log = Logger.getLogger(CBCache.class.getName());


    /**
     * Creates a cache directory named 'temp' and adds temporary files to it currently naming them (for example)
     * with the hash of their 'DN + a unique number + .jpg'.
     *
     * @param currentDN the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
     * @param entry     the actual entry that is being displayed.
     * @param type      the attribute type for example: audio or jpegPhoto.
     * @param size      the size of the number of specific attribute values this entry contains (i.e. 3 jpegPhoto attributes).
     */

    public static void createCache(String currentDN, Attributes entry, String type, int size)
    {
        if (type.equalsIgnoreCase("audio"))
        {
            createAudioCache(currentDN, entry, type, size);
            return;
        }

        fileDir = makeDir();
        allFiles = fileDir.list();

        currentDN = Integer.toString(currentDN.hashCode());

        for (int i = 0; i < allFiles.length; i++)    				//TE: don't create temporary files if they already exist for the entry.
        {
            if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("jpegPhoto") && allFiles[i].endsWith(".jpg"))
                return;
            else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odDocumentDOC") && allFiles[i].endsWith(".doc"))
                return;
            else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odSpreadSheetXLS") && allFiles[i].endsWith(".xls"))
                return;
            else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odMovieAVI") && allFiles[i].endsWith(".avi"))
                return;
            else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odSoundWAV") && allFiles[i].endsWith(".wav"))
                return;
            else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odMusicMID") && allFiles[i].endsWith(".mid"))
                return;
        }

        for (int i = 0; i < size; i++)
        {
            byte[] bytes = getAttByteValue(type, entry, i);	//TE: gets the byte[] of the jpegPhoto/audio attribute.

            if (type.equalsIgnoreCase("odDocumentDOC"))
            {
                doNormalCache(currentDN, bytes, ".doc");
            }
            else if (type.equalsIgnoreCase("odSpreadSheetXLS"))
            {
                doNormalCache(currentDN, bytes, ".xls");
            }
            else if (type.equalsIgnoreCase("odMovieAVI"))
            {
                doNormalCache(currentDN, bytes, ".avi");
            }
            else if (type.equalsIgnoreCase("odMusicMID"))
            {
                doNormalCache(currentDN, bytes, ".mid");
            }
            else if (type.equalsIgnoreCase("odSoundWAV"))
            {
                doNormalCache(currentDN, bytes, ".wav");
            }
            else if (type.equalsIgnoreCase("jpegPhoto"))
            {
                doNormalCache(currentDN, bytes, ".jpg");
            }
        }
        if (allFiles.length > CACHEMAX)    	//TE: decrease the size of the cache if the cache contains 100 or more temporary files.
            decreaseCacheSize();
    }


    /**
     * Creates an audio cache directory named 'temp/audio' and adds temporary files to it currently naming
     * them (for example) with the hash of their 'DN + a unique number + .wav'.
     *
     * @param currentDN the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
     * @param entry     the actual entry that is being displayed.
     * @param type      the attribute type for example: audio or jpegPhoto.
     * @param size      the size of the number of specific attribute values this entry contains (i.e. 3 audio attributes).
     */

    public static void createAudioCache(String currentDN, Attributes entry, String type, int size)
    {
        if (!type.equalsIgnoreCase("audio"))
        {
            log.warning("Error - trying to create a audio temporary cache with incorrect data in 'CBCache.createAudioCache'.");
            return;
        }

        audioFileDir = makeAudioDir();
        allAudioFiles = audioFileDir.list();

        currentDN = Integer.toString(currentDN.hashCode());

        for (int i = 0; i < allAudioFiles.length; i++)    				//TE: don't create temporary files if they already exist for the entry.
        {
            if (allAudioFiles[i].startsWith(currentDN))
                return;
        }

        for (int i = 0; i < size; i++)
        {
            byte[] bytes = getAttByteValue(type, entry, i);	//TE: gets the byte[] of the jpegPhoto/audio attribute.

            if (type.equalsIgnoreCase("audio"))
            {
                doAudioCache(currentDN, bytes);
            }
        }
        if (allAudioFiles.length > CACHEMAX)    	//TE: decrease the size of the cache if the cache contains 100 or more temporary files.
            decreaseAudioCacheSize();
    }


    /**
     * Does the actual writing of the cache files.
     *
     * @param currentDN the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
     * @param bytes     the byte[] of the audio attribute.
     * @param extension the extension of the cache file (currently .doc, .jpg).
     */

    public static void doNormalCache(String currentDN, byte[] bytes, String extension)
    {
        String name = currentDN + counter + extension;
        File file = new File(fileDir, name);

        counter++;

        try
        {
            file.deleteOnExit();    //TE: deletes the temporary files when JX is shut down.

            FileOutputStream output = new FileOutputStream(file);
            output.write(bytes);
            output.close();
        }
        catch (Exception e)
        {
            CBUtility.error(CBIntText.get("Problem writing the temporary file: ") + file.toString() + "\n" + e);
        }
    }


    /**
     * Creates the audio temporary files.  First, the type of audio file needs to be determined.
     * This is done by converting the first 100 bytes of the audio file to hex then searching it for an
     * identifier of the audio file (for example in a .wav file the header should contain the word 'WAVE'
     * which in hex is '574156').  If the audio file type is determined the temporary file is created with
     * the current DN + a unique number + the extension for example:
     * <p/>
     * cn=Al,o=DEMOCORP,c=AU9.wav
     * <p/>
     * This method can determine the following audio file types:
     * <p/>
     * .wav
     * .mp3
     * .rmi
     * .ram
     * .aiff
     * .mid
     * .au
     * .stm
     * .voc
     * .xm
     * .s3m
     * .it
     * <p/>
     * if an audio file type is unknown the temporary file is still created but with no extension.
     *
     * @param currentDN the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
     * @param bytes     the byte[] of the audio attribute.
     */

    public static void doAudioCache(String currentDN, byte[] bytes)
    {
        byte[] testBytes = new byte[100];
        if (bytes.length > 100)
            System.arraycopy(bytes, 0, testBytes, 0, 100);
        else
            testBytes = bytes;

        String hexSub = CBParse.bytes2Hex(testBytes);

        //TE: look at the header of the sound files and determine the extension.
        if (hexSub.indexOf("574156") > -1)
            extension = ".wav";
        else if (hexSub.indexOf("494433") > -1)
            extension = ".mp3";
        else if ((hexSub.toLowerCase()).indexOf("fffb9044") > -1)
            extension = ".mp3";
        else if ((hexSub.toLowerCase()).indexOf("fffb300c") > -1)
            extension = ".mp3";
        else if ((hexSub.toLowerCase()).indexOf("fff330c0") > -1)
            extension = ".mp3";
        else if (hexSub.indexOf("524946") > -1)
            extension = ".rmi";
        else if ((hexSub.toLowerCase()).indexOf("524d46") > -1)
            extension = ".ram";
        else if (hexSub.indexOf("41494646") > -1)
            extension = ".aiff";
        else if ((hexSub.toLowerCase()).indexOf("4d546864") > -1)
            extension = ".mid";
        else if ((hexSub.toLowerCase()).indexOf("2e736e64") > -1)
            extension = ".au";
        else if ((hexSub.toLowerCase()).indexOf("636f6f6c") > -1)
            extension = ".stm";		//TE: screamTracker format.
        else if ((hexSub.toLowerCase()).indexOf("437265617469766520566f6963652046696c65") > -1)
            extension = ".voc";		//TE: Creative Voice File.
        else if ((hexSub.toLowerCase()).indexOf("457874656e646564204d6f64756c65") > -1)
            extension = ".xm"; 		//TE: Extended Module format.
        else if ((hexSub.toLowerCase()).indexOf("5343524d") > -1)
            extension = ".s3m";
        else if ((hexSub.toLowerCase()).indexOf("494d50") > -1)
            extension = ".it";
        else
            extension = ".xxx";

        File file = new File(audioFileDir, currentDN + counter + extension);
        counter++;

        file.deleteOnExit();    //TE: deletes the temporary files when JX is shut down.
        try
        {
            FileOutputStream output = new FileOutputStream(file);
            output.write(bytes);
            output.close();
        }
        catch (IOException e)
        {
            CBUtility.error(CBIntText.get("Problem writing the audio temporary file: ") + file.toString() + "\n" + e);
        }
    }


    /**
     * This Comparator compares two Files by their lastModified() date.
     */

    public static class FileComparator implements Comparator
    {
        /**
         * This Comparator compares two Files by their lastModified() date.
         *
         * @param o1 one of the two items to be compared.
         * @param o2 the other of the items to be compared.
         */

        public int compare(Object o1, Object o2)
        {
            long a = ((File) o1).lastModified();
            long b = ((File) o2).lastModified();

            return (a == b) ? 0 : ((a < b) ? -1 : 1);
        }
    }

⌨️ 快捷键说明

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