22 package com.realtime.crossfire.jxclient.faces;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import javax.swing.Icon;
30 import javax.swing.ImageIcon;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
52 if (!cacheDir.isDirectory() && !cacheDir.mkdirs()) {
53 System.err.println(cacheDir+
": cannot create directory");
62 public ImageIcon
load(@NotNull
final Face face) {
63 return load(face.getFaceName(), face.getFaceChecksum());
74 private ImageIcon
load(@NotNull
final String faceName,
final int faceChecksum) {
76 final long len = file.length();
77 if (len >= 0x10000 || len <= 0) {
80 final byte[] data =
new byte[(int)len];
82 try (
final FileInputStream fis =
new FileInputStream(file)) {
83 if (fis.read(data) != data.length) {
87 }
catch (
final IOException ignored) {
90 final ImageIcon imageIcon =
new ImageIcon(data);
91 return imageIcon.getIconWidth() <= 0 && imageIcon.getIconHeight() <= 0 ? null : imageIcon;
98 public void save(@NotNull
final Face face, @NotNull
final ImageIcon imageIcon) {
99 save(face.getFaceName(), face.getFaceChecksum(), imageIcon);
108 public void save(@NotNull
final String faceName,
final int faceChecksum, @NotNull
final Icon imageIcon) {
121 final String dirName = quotedFaceName.substring(0, Math.min(2, quotedFaceName.length()));
122 final File dir =
new File(
new File(cacheDir, dirName), quotedFaceName);
123 if (!dir.exists() && !dir.mkdirs()) {
124 System.err.println(
"Cannot create directory: "+dir);
126 return new File(dir, Integer.toString(faceChecksum));
static String quoteName(@NotNull final String name)
Converts a file name to a "safe" form.
Utility class for manipulating images.
void save(@NotNull final Face face, @NotNull final ImageIcon imageIcon)
Stores an ImageIcon into the cache.the face to save the image icon to store
static void saveImageIcon(@NotNull final File outputFile, @NotNull final Icon imageIcon)
Saves an ImageIcon to a file.
Interface for ImageIcon caching classes.
File getImageFileName(@NotNull final String faceName, final int faceChecksum)
Calculates a hashed image name to be used as a file name.
A disk based cache for image files.
ImageIcon load(@NotNull final Face face)
Retrieves an image from the cache.the face to retrieve the image icon, ornull if the cache does not ...
final File cacheDir
The directory where the images are saved.
Utility class for manipulating filenames.
void save(@NotNull final String faceName, final int faceChecksum, @NotNull final Icon imageIcon)
Stores an ImageIcon into the cache.
FileCache(@NotNull final File cacheDir)
Creates a new instance.
ImageIcon load(@NotNull final String faceName, final int faceChecksum)
Retrieves an image from the cache.