Gridarta Editor
ArchFaceProvider.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.face;
21 
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28 import javax.swing.ImageIcon;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 
38 public class ArchFaceProvider extends AbstractFaceProvider {
39 
44  private final Map<String, String> files = new HashMap<>();
45 
50  public int size() {
51  return files.size();
52  }
53 
60  public void addInfo(@NotNull final String faceName, @NotNull final String fileName) {
61  files.put(faceName, fileName);
62  }
63 
69  @Nullable
70  public String getFilename(@NotNull final String faceName) {
71  return files.get(faceName);
72  }
73 
74  @Nullable
75  @Override
76  protected ImageIcon createImage(@NotNull final String faceName, final long stretch) {
77  final String filename = files.get(faceName);
78  if (filename == null) {
79  return null;
80  }
81 
82  final File file = new File(filename);
83  final long length = file.length();
84  if (length <= 0L || length >= Integer.MAX_VALUE) {
85  return null;
86  }
87 
88  // It has been checked that the value will fit into an int.
89  final int intLength = (int) length;
90  final byte[] buf = new byte[intLength];
91  try {
92  try (InputStream fis = new FileInputStream(file)) {
93  if (fis.read(buf) != intLength) {
94  return null;
95  }
96  }
97  } catch (final IOException ex) {
98  return null;
99  }
100 
101  return new ImageIcon(buf);
102  }
103 
104 }
net.sf.gridarta.model.face.AbstractFaceProvider
Abstract Base class for FaceProviders which implements a memory sensitive cache.
Definition: AbstractFaceProvider.java:36
net.sf.gridarta.model.face.ArchFaceProvider.getFilename
String getFilename(@NotNull final String faceName)
Get the filename for a face.
Definition: ArchFaceProvider.java:70
net.sf.gridarta.model.face.ArchFaceProvider
Implementation of FaceProvider which reads images from the arch directory.
Definition: ArchFaceProvider.java:38
net.sf.gridarta.model.face.ArchFaceProvider.createImage
ImageIcon createImage(@NotNull final String faceName, final long stretch)
Creates an image not found in the cache.
Definition: ArchFaceProvider.java:76
net.sf.gridarta.model.face.ArchFaceProvider.addInfo
void addInfo(@NotNull final String faceName, @NotNull final String fileName)
Report position of a face for loading it later.
Definition: ArchFaceProvider.java:60
net.sf.gridarta.model.face.ArchFaceProvider.size
int size()
Returns the number of faces.
Definition: ArchFaceProvider.java:50
net.sf.gridarta.model.face.ArchFaceProvider.files
final Map< String, String > files
The icon filename.
Definition: ArchFaceProvider.java:44