20 package net.sf.gridarta.model.face;
22 import java.io.BufferedReader;
23 import java.io.ByteArrayOutputStream;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.Reader;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
40 import net.
sf.japi.swing.action.ActionBuilder;
41 import net.
sf.japi.swing.action.ActionBuilderFactory;
42 import org.apache.log4j.Category;
43 import org.apache.log4j.Logger;
44 import org.jetbrains.annotations.NotNull;
72 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
93 final File tmpFaceFile;
96 }
catch (
final IOException ex) {
109 BufferedReader treeIn = null;
112 final InputStream inputStream2 = url.openStream();
116 treeIn =
new BufferedReader(reader);
117 }
catch (
final FileNotFoundException ignored) {
120 final byte[] tag =
"IMAGE ".getBytes();
121 final StringBuilder faceB =
new StringBuilder();
125 while (offset < data.length) {
130 throw new IOException(
"expecting 'IMAGE' at position " + offset);
133 if (includeFaceNumbers) {
134 while (data[offset++] != 0x20) {
141 final char c = (char) data[offset++];
145 if (c < '0' || c >
'9') {
146 throw new IOException(
"syntax error at position " + offset +
": not a digit");
149 size += (int) c - (
int)
'0';
154 final char c = (char) data[offset++];
164 final String faceName = faceB.toString().intern();
166 if (offset + size > data.length) {
167 throw new IOException(
"truncated at position " + offset);
170 if (treeIn != null) {
171 final String originalFilename = treeIn.readLine();
172 if (originalFilename == null) {
173 LOG.warn(ACTION_BUILDER.format(
"logFaceObjectWithoutOriginalName", faceName));
175 final Matcher matcher = pattern.matcher(originalFilename);
176 if (matcher.matches()) {
189 collectedFaceProvider.
addInfo(faceName, offset, size);
194 if (treeIn != null) {
198 }
catch (
final IOException ex) {
202 if (LOG.isInfoEnabled()) {
203 LOG.info(
"Loaded " + faces +
" faces from '" + tmpFaceFile +
"'.");
205 return collectedFaceProvider;
215 final ByteArrayOutputStream bufOut =
new ByteArrayOutputStream((
int) file.length());
216 try (InputStream inputStream =
new FileInputStream(file)) {
219 final int len = inputStream.read(buf);
223 bufOut.write(buf, 0, len);
226 return bufOut.toByteArray();
static final ActionBuilder ACTION_BUILDER
Action Builder.
Convenience class for adding messages to a ErrorView instance using a fixed category name...
static final String MAP_ENCODING
Encoding to use for maps and other data.
void addWarning(@NotNull final ErrorViewCategory category)
Adds a warning message.
final boolean includeFaceNumbers
Whether the face file contains face numbers.
This interface represents a lazy loader that provides images on demand.
void addInfo(@NotNull final String faceName, final int pos, final int size)
Report position and size of a face for loading it later.
Implementation of FaceProvider which reads images from the collected PNG archive. ...
Defines possible error categories for ErrorView instances.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Interface for classes displaying error messages.
Base package of all Gridarta classes.
FaceObject getFaceObject()
Returns the illegal face object.
Exception that's thrown in case a face name was not unique.
Utility-class for Gridarta's I/O.
Exception thrown to indicate that a face object is not acceptable.
static final Category LOG
The Logger for printing log messages.
static File getFile(@Nullable final File dir, @NotNull final String fileName)
Returns a File instance for a resource that is a regular file on the file system. ...
static URL getResource(@Nullable final File dir, @NotNull final String fileName)
Get the URL of a resource.
DefaultFaceObjects(final boolean includeFaceNumbers)
Creates a new instance.
FaceProvider loadFacesCollection(@NotNull final ErrorView errorView, @NotNull final File collectedDirectory)
Loads all faces from a png collection file.
Utility class for ActionBuilder related functions.
static final int CHUNK_SIZE
The chunk size in bytes when reading file contents.
static final long serialVersionUID
The serial version UID.
static boolean contains(final byte[] src, final int offset, final byte[] search)
Helper method that checks whether a region in a byte array at a given offset contains the same as ano...
String getFaceName()
Get the faceName, which is the name of the face as usable by the "face" attribute.
FaceObject getDuplicate()
Returns the duplicate that caused this exception.
Abstract base implementation of FaceObjects.
Abstract base class for FaceObjects implementations.
A FaceProvider that does not return any faces.
boolean isIncludeFaceNumbers()
Returns whether the images file contains face numbers.
Utility class for array related functions.
void addFaceObject(@NotNull final String faceName, @NotNull final String originalFilename, final int offset, final int size)
static byte [] getFileContents(@NotNull final File file)
Returns the contents of a File as a.