Crossfire JXClient, Trunk  R20561
FileCache.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.faces;
23 
26 import java.io.File;
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;
33 
38 public class FileCache implements ImageCache {
39 
43  @NotNull
44  private final File cacheDir;
45 
50  public FileCache(@NotNull final File cacheDir) {
51  this.cacheDir = cacheDir;
52  if (!cacheDir.isDirectory() && !cacheDir.mkdirs()) {
53  System.err.println(cacheDir+": cannot create directory");
54  }
55  }
56 
60  @Nullable
61  @Override
62  public ImageIcon load(@NotNull final Face face) {
63  return load(face.getFaceName(), face.getFaceChecksum());
64  }
65 
73  @Nullable
74  private ImageIcon load(@NotNull final String faceName, final int faceChecksum) {
75  final File file = getImageFileName(faceName, faceChecksum);
76  final long len = file.length();
77  if (len >= 0x10000 || len <= 0) {
78  return null;
79  }
80  final byte[] data = new byte[(int)len];
81  try {
82  try (final FileInputStream fis = new FileInputStream(file)) {
83  if (fis.read(data) != data.length) {
84  return null;
85  }
86  }
87  } catch (final IOException ignored) {
88  return null;
89  }
90  final ImageIcon imageIcon = new ImageIcon(data); // cannot use ImageIcon(String) since this caches "file not found"
91  return imageIcon.getIconWidth() <= 0 && imageIcon.getIconHeight() <= 0 ? null : imageIcon;
92  }
93 
97  @Override
98  public void save(@NotNull final Face face, @NotNull final ImageIcon imageIcon) {
99  save(face.getFaceName(), face.getFaceChecksum(), imageIcon);
100  }
101 
108  public void save(@NotNull final String faceName, final int faceChecksum, @NotNull final Icon imageIcon) {
109  Images.saveImageIcon(getImageFileName(faceName, faceChecksum), imageIcon);
110  }
111 
118  @NotNull
119  private File getImageFileName(@NotNull final String faceName, final int faceChecksum) {
120  final String quotedFaceName = FilenameUtils.quoteName(faceName);
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);
125  }
126  return new File(dir, Integer.toString(faceChecksum));
127  }
128 
129 }
static String quoteName(@NotNull final String name)
Converts a file name to a "safe" form.
Utility class for manipulating images.
Definition: Images.java:36
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
Definition: FileCache.java:98
static void saveImageIcon(@NotNull final File outputFile, @NotNull final Icon imageIcon)
Saves an ImageIcon to a file.
Definition: Images.java:50
Interface for ImageIcon caching classes.
Definition: ImageCache.java:32
File getImageFileName(@NotNull final String faceName, final int faceChecksum)
Calculates a hashed image name to be used as a file name.
Definition: FileCache.java:119
A disk based cache for image files.
Definition: FileCache.java:38
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 ...
Definition: FileCache.java:62
final File cacheDir
The directory where the images are saved.
Definition: FileCache.java:44
Utility class for manipulating filenames.
void save(@NotNull final String faceName, final int faceChecksum, @NotNull final Icon imageIcon)
Stores an ImageIcon into the cache.
Definition: FileCache.java:108
FileCache(@NotNull final File cacheDir)
Creates a new instance.
Definition: FileCache.java:50
ImageIcon load(@NotNull final String faceName, final int faceChecksum)
Retrieves an image from the cache.
Definition: FileCache.java:74