<commit_before>package net.acuttone.reddimg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class ImageCache {

	private static final int MAX_IMAGE_SIZE = 1000000;
	public static final int IN_MEM_CACHE_SIZE = 3;
	private File reddimgDir;
	private LinkedHashMap<String, Bitmap> inMemCache;

	private ImageResizer imgResizer;

	public ImageCache(ImageResizer imgResizer) {
		this.imgResizer = imgResizer;
		// TODO: use http://developer.android.com/reference/android/content/Context.html#getExternalCacheDir()
		File sdCard = Environment.getExternalStorageDirectory();
		reddimgDir = new File(sdCard.getAbsolutePath() + "/Reddimg");
		if(reddimgDir.exists() == false) {
			reddimgDir.mkdir();
		}
		inMemCache = new LinkedHashMap<String, Bitmap>();
		
	}

	public Bitmap getFromMem(String url) {
		synchronized (inMemCache) {
			if (inMemCache.containsKey(url)) {
				Bitmap bitmap = inMemCache.get(url);
				// bump the image in the cache
//				inMemCache.remove(url);
//				inMemCache.put(url, bitmap);
				return bitmap;
			} else {
				return null;
			}
		}
	}
	
	public boolean prepareImage(String url) {
		Log.d(MainActivity.APP_NAME, "Preparing " + url);
		Bitmap result = getFromDisk(url);
		if (result != null) {
			Log.d(MainActivity.APP_NAME, url + " found on disk cache");
			return true;
		}
		
		result = getFromWeb(url);
		if(result != null) {
			Log.d(MainActivity.APP_NAME, url + " dl from web");
			return true;
		}
		
		Log.w(MainActivity.APP_NAME, url + " could not be dl from web");
		return false;
	}

	// TODO: add max disk cache size
	private Bitmap getFromDisk(String url) {
		Bitmap result = null;
		File img = new File(reddimgDir, urlToFilename(url));
		if (img.exists()) {
			try {<commit_msg>max disk space todo<commit_after>package net.acuttone.reddimg;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class ImageCache {

	private static final int MAX_IMAGE_SIZE = 1000000;
	public static final int IN_MEM_CACHE_SIZE = 3;
	private File reddimgDir;
	private LinkedHashMap<String, Bitmap> inMemCache;

	private ImageResizer imgResizer;

	public ImageCache(ImageResizer imgResizer) {
		this.imgResizer = imgResizer;
		// TODO: use http://developer.android.com/reference/android/content/Context.html#getExternalCacheDir()
		File sdCard = Environment.getExternalStorageDirectory();
		reddimgDir = new File(sdCard.getAbsolutePath() + "/Reddimg");
		if(reddimgDir.exists() == false) {
			reddimgDir.mkdir();
		}
		inMemCache = new LinkedHashMap<String, Bitmap>();
		
	}

	public Bitmap getFromMem(String url) {
		synchronized (inMemCache) {
			if (inMemCache.containsKey(url)) {
				Bitmap bitmap = inMemCache.get(url);
				// bump the image in the cache
//				inMemCache.remove(url);
//				inMemCache.put(url, bitmap);
				return bitmap;
			} else {
				return null;
			}
		}
	}
	
	public boolean prepareImage(String url) {
		Log.d(MainActivity.APP_NAME, "Preparing " + url);
		Bitmap result = getFromDisk(url);
		if (result != null) {
			Log.d(MainActivity.APP_NAME, url + " found on disk cache");
			return true;
		}
		
		result = getFromWeb(url);
		if(result != null) {
			Log.d(MainActivity.APP_NAME, url + " dl from web");
			return true;
		}
		
		Log.w(MainActivity.APP_NAME, url + " could not be dl from web");
		return false;
	}

	// TODO: add max disk cache size
	// TODO: check for disk space
	private Bitmap getFromDisk(String url) {
		Bitmap result = null;
		File img = new File(reddimgDir, urlToFilename(url));
		if (img.exists()) {
			try {
{'fraction_non_alphanumeric': 0.11426639621365788, 'fraction_numerical': 0.003606040117196304, 'mean_word_length': 4.227326266195524, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5443687', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1424, 'n_words': 350}