Thread: Cache Utility

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 35
  1. #1 Cache Utility 
    Retired. Stop PMing me.


    Galkon's Avatar
    Join Date
    Nov 2007
    Age
    17
    Posts
    7,526
    Thanks given
    1,805
    Thanks received
    2,830
    Rep Power
    5000
    Okay, basically this is a rewrite of my previous sprite caches, but I've made it so it can be used to pack any files together into one file. It also compresses each file using GZip to save you space as well.

    Click here to download.

    Example Usages
    You could use this tool to do the following:
    • Create a sprite cache that supports any image format and holds transparency data.
    • Secure your cache by storing parts of it in this (for example, packing loc.dat, loc.idx, obj.dat, obj.idx, into one file so nobody can steal them).
    • Custom cache format for external tools (not limited to sprites or client related stuff).


    How to use the Cache Utility
    To build a cache simply select the folder you wish to pack into a cache. Then you'll have the option to name it (for example if you're packing "newspritefolder" and you wanna name the cache file "sprites.dat" you may do so). If you leave the name field blank it'll automatically generate the "foldername.dat" file. Once the folder is selected and you're ready just hit "Build" and it'll generate the "cachename.dat" file.

    The "Extract" feature has been temporarily disabled.
    If you wish to extract the files from the cache you simple put the cache file in the same folder as the Cache Utility.jar and run it, then enter the name of the cache file, and hit "Extract". It will then extract all files from it to the "out" folder.

    How to read the Cache Utility File
    To read the file, you must use these classes:

    Spoiler for FileChunk.java:
    Code:
    import java.io.File;
    
    public class FileChunk {
    
    	/**
    	 * Creates the file chunk for the path and data.
    	 * @param path
    	 * @param data
    	 */
    	public FileChunk(String path, byte[] data) {
    		this.path = path;
    		this.data = data;
    	}
    
    	/**
    	 * Returns the name of the FileChunk (the file name).
    	 * @return
    	 */
    	public String getName() {
    		String[] directories = null;
    		if (System.getProperty("file.separator").equals("/")) {
    			directories = getPath().split(System.getProperty("file.separator"));
    		} else {
    			directories = getPath().split("\\\\");
    		}
    		if (directories != null) {
    			return directories[directories.length - 1].replaceAll("/", "\\");
    		}
    		return null;
    	}
    
    	/**
    	 * Returns the name of the FileChunk without the extension (the file name without extension).
    	 * @return
    	 */
    	public String getNameWithoutExtension() {
    		return getName().substring(0, getName().indexOf("."));
    	}
    
    	/**
    	 * Gets the extension of the FileChunk.
    	 * @return
    	 */
    	public String getExtension() {
    		return getName().substring(getName().indexOf("."), getName().length());
    	}
    
    	/**
    	 * Returns whether or not the FileChunk is an image.
    	 * @return
    	 */
    	public boolean isImage() {
    		String extension = getExtension().toLowerCase();
    		String[] acceptable = { ".png", ".jpg", ".jpeg", ".bmp", ".gif" };
    		for (String s : acceptable) {
    			if (extension.equals(s)) {
    				return true;
    			}
    		}
    		return false;
    	}
    
    	/**
    	 * Returns the data stored in the byte array.
    	 * @return
    	 */
    	public byte[] getData() {
    		return data;
    	}
    
    	/**
    	 * Returns the path of the file.
    	 * @return
    	 */
    	public String getPath() {
    		return path.replace("." + System.getProperty("file.separator") + "in" + System.getProperty("file.separator"), "");
    	}
    
    	/**
    	 * The byte array that stores the file data.
    	 */
    	private byte[] data;
    
    	/**
    	 * The path of the file.
    	 */
    	private String path;
    
    }

    Spoiler for FileReader.java:
    Code:
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.LinkedList;
    
    import javax.swing.JOptionPane;
    
    public class FileReader {
    
    	/**
    	 * Initializes the FileReader.
    	 */
    	public FileReader() {
    		files = new LinkedList<FileChunk>();
    	}
    
    	/**
    	 * Reads the cache.
    	 * @param name
    	 */
    	public void read(String name) {
    		try {
    			DataInputStream in = new DataInputStream(new FileInputStream(name));
    			int count = in.readShort();
    			for (int index = 0; index < count; index++) {
    				String path = in.readUTF();
    				long length = in.readLong();
    				byte[] data = new byte[(int) length];
    				in.readFully(data);
    				data = DataUtilities.decompress(data);
    				files.add(new FileChunk(path, data));
    			}
    			in.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Gets the specified file.
    	 * @param directories
    	 * @return
    	 */
    	public FileChunk getFile(String ...directories) {
    		if (directories != null) {
    			String path = "";
    			for (int index = 0; index < directories.length; index++) {
    				path += (index > 0 ? "\\" : "") + directories[index];
    			}
    			for (FileChunk file : files) {
    				if (file.getPath().equals(path + file.getExtension())) {
    					return file;
    				}
    			}
    		}
    		return null;
    	}
    
    	/**
    	 * Returns the files as a LinkedList.
    	 * @return
    	 */
    	public LinkedList<FileChunk> getFiles() {
    		return files;
    	}
    
    	/**
    	 * The files in the LinkedList.
    	 */
    	private LinkedList<FileChunk> files;
    
    }

    Spoiler for DataUtilities.java:
    Code:
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    
    
    public class DataUtilities {
    
    	/**
    	 * Returns the file as a byte array.
    	 * @param file
    	 * @return
    	 */
    	public static byte[] getData(File file) {
    		try {
    			byte[] data = new byte[(int) file.length()];
    			FileInputStream fis = new FileInputStream(file);
    			fis.read(data);
    			fis.close();
    			return data;
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	/**
    	 * Compresses the raw data in gzip format.
    	 * @param raw
    	 * @return
    	 */
    	public static byte[] compress(byte[] raw) {
    		try {
    			ByteArrayOutputStream bos = new ByteArrayOutputStream();
    			GZIPOutputStream gzo = new GZIPOutputStream(bos);
    			try {
    				gzo.write(raw, 0, raw.length);
    			} finally {
    				gzo.close();
    				bos.close();
    			}
    			return bos.toByteArray();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	/**
    	 * Decompresses the data from gzip.
    	 * @param data
    	 * @return
    	 * @throws IOException
    	 */
    	public static byte[] decompress(byte[] data) throws IOException {
    		GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(data));
    		ByteArrayOutputStream out = new ByteArrayOutputStream();
    		byte[] buf = new byte[1024];
    		int len;
    		while ((len = gzi.read(buf)) > 0) {
    			out.write(buf, 0, len);
    		}
    		out.close();
    		return out.toByteArray();
    	}
    
    	/**
    	 * Writes data to the specified file.
    	 * @param file
    	 * @param data
    	 */
    	public static void write(String file, byte[] data) {
    		try {
    			if (data != null) {
    				OutputStream out = new FileOutputStream(file);
    				out.write(data);
    				out.close();
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    }


    Now, to read the files you must call the code:
    Code:
    FileReader reader = new FileReader();
    reader.read("yourname.dat");
    And to grab a file from the cache, you can simply do the following:
    Code:
    reader.getFile("directory", "filename");
    And to get the data of a file (byte array, for example for sprites):
    Code:
    reader.getFile("directory", "filename").getData();
    Media

    Attached image
    Reply With Quote  
     


  2. #2  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    Good job on this Galkon, going to be useful.
    Reply With Quote  
     

  3. #3  
    Super Donator


    Join Date
    Jun 2007
    Age
    31
    Posts
    2,157
    Thanks given
    316
    Thanks received
    282
    Rep Power
    779
    How many directory levels does it support?
    Reply With Quote  
     

  4. #4  
    Retired. Stop PMing me.


    Galkon's Avatar
    Join Date
    Nov 2007
    Age
    17
    Posts
    7,526
    Thanks given
    1,805
    Thanks received
    2,830
    Rep Power
    5000
    Quote Originally Posted by HloJustin View Post
    How many directory levels does it support?
    As many as you want. It goes through all folders in the input folder and grabs all the files. And to get the file from the cache file you can do:
    Code:
    reader.getFile(dir, dir, dir, dir, name);
    for as many directories as you need to.
    Attached image
    Reply With Quote  
     

  5. #5  
    Super Donator


    Join Date
    Jun 2007
    Age
    31
    Posts
    2,157
    Thanks given
    316
    Thanks received
    282
    Rep Power
    779
    Quote Originally Posted by Galkon View Post
    As many as you want. It goes through all folders in the input folder and grabs all the files. And to get the file from the cache file you can do:
    Code:
    reader.getFile(dir, dir, dir, dir, name);
    for as many directories as you need to.
    Oo nice
    Reply With Quote  
     

  6. Thankful user:


  7. #6  
    Respected Member

    Revil's Avatar
    Join Date
    Nov 2010
    Age
    30
    Posts
    4,860
    Thanks given
    3,715
    Thanks received
    2,228
    Rep Power
    5000
    Will give this a try later, looks great however.
    Reply With Quote  
     

  8. Thankful user:


  9. #7  
    Extreme Donator


    Join Date
    Dec 2011
    Posts
    323
    Thanks given
    92
    Thanks received
    46
    Rep Power
    81
    Sweet
    Reply With Quote  
     

  10. #8  
    Super Donator

    Batukka's Avatar
    Join Date
    Oct 2011
    Posts
    2,433
    Thanks given
    86
    Thanks received
    342
    Rep Power
    496
    it supports .gif animated?
    Reply With Quote  
     

  11. #9  
    Registered Member

    Join Date
    Mar 2011
    Posts
    1,226
    Thanks given
    245
    Thanks received
    475
    Rep Power
    294
    Quote Originally Posted by Bitj View Post
    it supports .gif animated?
    Obviously "String[] acceptable = { ".png", ".jpg", ".jpeg", ".bmp", ".gif" };"

    And really nice Galkon, thanks!
    Reply With Quote  
     

  12. Thankful user:


  13. #10  
    Retired. Stop PMing me.


    Galkon's Avatar
    Join Date
    Nov 2007
    Age
    17
    Posts
    7,526
    Thanks given
    1,805
    Thanks received
    2,830
    Rep Power
    5000
    Quote Originally Posted by Bitj View Post
    it supports .gif animated?
    It compresses and packs the data of each file into the cache file. It doesn't alter or affect it in any way. The only way a .gif wouldn't work is if your client doesn't support it (e.g. you haven't done Killer 99's snippet for it).
    Attached image
    Reply With Quote  
     

Page 1 of 4 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [Java] MediaFire Utility
    By Emily in forum Application Development
    Replies: 10
    Last Post: 03-29-2012, 03:45 AM
  2. ZodiacsReign Forever V.11 Utility
    By ZodiacsReign in forum Tools
    Replies: 13
    Last Post: 11-06-2010, 12:18 PM
  3. RuntimeMXBean - useful utility things
    By gnarly in forum Application Development
    Replies: 15
    Last Post: 06-02-2009, 04:23 PM
  4. KZip Compression Utility
    By OldMercenary in forum Tools
    Replies: 10
    Last Post: 06-05-2008, 08:58 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •