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-2015 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 >= (long) Integer.MAX_VALUE) {
85  return null;
86  }
87 
88  // It has been checked that the value will fit into an int.
89  //noinspection NumericCastThatLosesPrecision
90  final int intLength = (int) length;
91  final byte[] buf = new byte[intLength];
92  try {
93  try (InputStream fis = new FileInputStream(file)) {
94  if (fis.read(buf) != intLength) {
95  return null;
96  }
97  }
98  } catch (final IOException ex) {
99  return null;
100  }
101 
102  return new ImageIcon(buf);
103  }
104 
105 }
void addInfo(@NotNull final String faceName, @NotNull final String fileName)
Report position of a face for loading it later.
int size()
Returns the number of faces.
final Map< String, String > files
The icon filename.
String getFilename(@NotNull final String faceName)
Get the filename for a face.
Abstract Base class for FaceProviders which implements a memory sensitive cache.
ImageIcon createImage(@NotNull final String faceName, final long stretch)
Implementation of FaceProvider which reads images from the arch directory.