Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,83 +1,87 @@
/* Copyright (c) 2009 Matthias Kaeppler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.ignition.support.cache;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
* Implements a cache capable of caching image files. It exposes helper methods to immediately
* access binary image data as {@link Bitmap} objects.
*
* @author Matthias Kaeppler
*
*/
public class ImageCache extends AbstractCache<String, byte[]> {

public ImageCache(int initialCapacity, long expirationInMinutes, int maxConcurrentThreads) {
super("ImageCache", initialCapacity, expirationInMinutes, maxConcurrentThreads);
}

public synchronized void removeAllWithPrefix(String urlPrefix) {
CacheHelper.removeAllWithStringPrefix(this, urlPrefix);
}

@Override
public String getFileNameForKey(String imageUrl) {
return CacheHelper.getFileNameFromUrl(imageUrl);
}

@Override
protected byte[] readValueFromDisk(File file) throws IOException {
BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file));
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes");
}

int imageDataLength = (int) fileSize;

byte[] imageData = new byte[imageDataLength];
istream.read(imageData, 0, imageDataLength);
istream.close();

return imageData;
}

public synchronized Bitmap getBitmap(Object elementKey) {
byte[] imageData = super.get(elementKey);
if (imageData == null) {
return null;
}
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
}

@Override
protected void writeValueToDisk(File file, byte[] imageData) throws IOException {
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file));

ostream.write(imageData);

ostream.close();
}
}
/* Copyright (c) 2009 Matthias Kaeppler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.ignition.support.cache;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
* Implements a cache capable of caching image files. It exposes helper methods to immediately
* access binary image data as {@link Bitmap} objects.
*
* @author Matthias Kaeppler
*
*/
public class ImageCache extends AbstractCache<String, byte[]> {

public ImageCache(int initialCapacity, long expirationInMinutes, int maxConcurrentThreads) {
super("ImageCache", initialCapacity, expirationInMinutes, maxConcurrentThreads);
}

public synchronized void removeAllWithPrefix(String urlPrefix) {
CacheHelper.removeAllWithStringPrefix(this, urlPrefix);
}

@Override
public String getFileNameForKey(String imageUrl) {
return CacheHelper.getFileNameFromUrl(imageUrl);
}

@Override
protected byte[] readValueFromDisk(File file) throws IOException {
BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file));
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes");
}

int imageDataLength = (int) fileSize;

byte[] imageData = new byte[imageDataLength];
istream.read(imageData, 0, imageDataLength);
istream.close();

return imageData;
}

public synchronized Bitmap getBitmap(Object elementKey) {
byte[] imageData = super.get(elementKey);
if (imageData == null) {
return null;
}
try {
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
} catch (OutOfMemoryError e) {
return null;
}
}

@Override
protected void writeValueToDisk(File file, byte[] imageData) throws IOException {
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file));

ostream.write(imageData);

ostream.close();
}
}