Android Open Source - abstract-art Cache






From Project

Back to project page abstract-art.

License

The source code is released under:

GNU General Public License

If you think the Android project abstract-art listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.georgewhiteside.android.abstractart;
//from  w w w  .j  a v a2s . c o m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.InflaterInputStream;

import android.util.Log;

public final class Cache
{
  public static final String TAG = "Cache";
  
  /**
   * Reads a cached file of arbitrary size into a new buffer. Not 100% foolproof; there's
   * a remote possibility of mobile platform unexpectedness, but then that's always the
   * case isn't it? :)
   * 
   * @param cacheFile {@code File} object representing the cached data on the file system
   * @return a byte array containing the cached file
   */
  public static byte[] read(File cacheFile)
  {
    byte[] output = null;
    try {
      FileInputStream fileInputStream = new FileInputStream(cacheFile);
          int bytesRead = 0;
          int count = (int)cacheFile.length(); // assuming no jokers try to read a 2GB cache file...
          output = new byte[count];
          while(bytesRead < count) {
            bytesRead += fileInputStream.read(output, bytesRead, count - bytesRead);
          }
          fileInputStream.close();
    } catch (FileNotFoundException e) {
      Log.e(TAG, String.format("Cache file %s could not be found", cacheFile.getPath()));
      e.printStackTrace();
    } catch (IOException e) {
      Log.e(TAG, String.format("There was a problem reading cache file %s", cacheFile.getPath()));
      e.printStackTrace();
    }
    
    return output;
  }
  
  /**
   * Reads a cached file of known size directly into a preallocated byte array. Not 100%
   * foolproof, etc.
   * 
   * @param cacheFile {@code File} object representing the cached data on the file system
   * @param output a preallocated byte array of {@code count} bytes
   * @param count the exact length of the data being read
   */
  public static void read(File cacheFile, byte[] output, int count)
  {
    try {
      FileInputStream fileInputStream = new FileInputStream(cacheFile);
          int bytesRead = 0;
          while(bytesRead < count) {
            bytesRead += fileInputStream.read(output, bytesRead, count);
          }
          fileInputStream.close();
    } catch (FileNotFoundException e) {
      Log.e(TAG, String.format("Cache file %s could not be found", cacheFile.getPath()));
      e.printStackTrace();
    } catch (IOException e) {
      Log.e(TAG, String.format("There was a problem reading cache file %s", cacheFile.getPath()));
      e.printStackTrace();
    }
  }
  
  /**
   * Writes data to the specified file location, overwriting any existing data.
   * 
   * @param cacheFile {@code File} object representing the output file path
   * @param input the byte array to write to {@code cacheFile}
   */
  public static void write(File cacheFile, byte[] input)
  {
    write(cacheFile, input, input.length);
  }
  
  public static void write(File cacheFile, byte[] input, int length)
  {
    File parentFile = cacheFile.getParentFile();
    
    if(parentFile != null) parentFile.mkdirs(); // safely does nothing if path exists
    
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(cacheFile);
      fileOutputStream.write(input, 0, length);
      fileOutputStream.close();
    } catch (FileNotFoundException e) {
      Log.e(TAG, String.format("Cache file %s could not be found", cacheFile.getPath()));
      e.printStackTrace();
    } catch (IOException e) {
      Log.e(TAG, String.format("There was a problem writing to cache file %s", cacheFile.getPath()));
      e.printStackTrace();
    }
  }
  
  
  
  public static void readCompressed(File cacheFile, byte[] output, int count)
  {
    
    try {
      FileInputStream fileInputStream = new FileInputStream(cacheFile);
      InflaterInputStream inflaterInputStream = new InflaterInputStream(fileInputStream);
          int bytesRead = 0;
          while(bytesRead < count) {
            bytesRead += inflaterInputStream.read(output, bytesRead, count - bytesRead);
          }
          inflaterInputStream.close();
    } catch (FileNotFoundException e) {
      Log.e(TAG, String.format("Cache file %s could not be found", cacheFile.getPath()));
      e.printStackTrace();
    } catch (IOException e) {
      Log.e(TAG, String.format("There was a problem reading cache file %s", cacheFile.getPath()));
      e.printStackTrace();
    }
  }
  
  public static void writeCompressed(File cacheFile, byte[] input)
  {
    cacheFile.getParentFile().mkdirs(); // safely does nothing if path exists
    
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(cacheFile);
      DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(fileOutputStream);
      deflaterOutputStream.write(input, 0, input.length);
      deflaterOutputStream.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}




Java Source Code List

net.georgewhiteside.android.abstractart.AbstractArt.java
net.georgewhiteside.android.abstractart.BattleBackground.java
net.georgewhiteside.android.abstractart.BattleGroup.java
net.georgewhiteside.android.abstractart.Cache.java
net.georgewhiteside.android.abstractart.Distortion.java
net.georgewhiteside.android.abstractart.Enemy.java
net.georgewhiteside.android.abstractart.FPSCounter.java
net.georgewhiteside.android.abstractart.GLOffscreenSurface.java
net.georgewhiteside.android.abstractart.ImageLoader.java
net.georgewhiteside.android.abstractart.Layer.java
net.georgewhiteside.android.abstractart.Renderer.java
net.georgewhiteside.android.abstractart.RomUtil.java
net.georgewhiteside.android.abstractart.ServiceDialog.java
net.georgewhiteside.android.abstractart.Settings.java
net.georgewhiteside.android.abstractart.ShaderFactory.java
net.georgewhiteside.android.abstractart.Translation.java
net.georgewhiteside.android.abstractart.UniformGridView.java
net.georgewhiteside.android.abstractart.Wallpaper.java
net.georgewhiteside.android.abstractart.settings.BackgroundSelector.java
net.georgewhiteside.android.abstractart.settings.ClearCachePreference.java
net.georgewhiteside.android.abstractart.settings.CreateImageCachePreference.java
net.georgewhiteside.android.abstractart.settings.FrameRatePreference.java
net.georgewhiteside.android.abstractart.settings.ThumbnailAdapter.java
net.georgewhiteside.utility.Dimension.java
net.starmen.pkhack.HackModule.java
org.jf.GLWallpaper.GLWallpaperService.java
sheetrock.panda.changelog.ChangeLog.java