Validator eingesetzt und Exception handling verbessert

This commit is contained in:
ngb
2023-01-17 11:27:08 +01:00
parent ef248a8580
commit 02085c83fc
4 changed files with 34 additions and 17 deletions

View File

@@ -1,9 +1,11 @@
package schule.ngb.zm.util.io;
import schule.ngb.zm.util.Log;
import schule.ngb.zm.util.Validator;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@@ -11,6 +13,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Level;
/**
* Hilfsklasse, um Textdateien in verschiedenen Formaten einzulesen.
@@ -63,6 +66,9 @@ public final class FileLoader {
* @return Eine Liste mit den Zeilen der Textdatei.
*/
public static List<String> loadLines( String source, Charset charset ) {
Validator.requireNotNull(source, "source");
Validator.requireNotNull(charset, "charset");
try( BufferedReader reader = ResourceStreamProvider.getReader(source, charset) ) {
List<String> result = new ArrayList<>();
@@ -72,8 +78,11 @@ public final class FileLoader {
}
return result;
} catch( MalformedURLException muex ) {
LOG.warn("Could not find resource for <%s>", source);
return Collections.emptyList();
} catch( IOException ex ) {
LOG.error(ex, "Error while loading lines from source <%s>", source);
LOG.warn(ex, "Error while loading lines from source <%s>", source);
return Collections.emptyList();
}
}
@@ -101,6 +110,9 @@ public final class FileLoader {
* @return Der Inhalt der Textdatei.
*/
public static String loadText( String source, Charset charset ) {
Validator.requireNotNull(source, "source");
Validator.requireNotNull(charset, "charset");
try( BufferedReader reader = ResourceStreamProvider.getReader(source, charset) ) {
StringBuilder result = new StringBuilder();
@@ -110,8 +122,11 @@ public final class FileLoader {
}
return result.toString();
} catch( MalformedURLException muex ) {
LOG.warn("Could not find resource for <%s>", source);
return "";
} catch( IOException ex ) {
LOG.error(ex, "Error while loading string from source <%s>", source);
LOG.warn(ex, "Error while loading string from source <%s>", source);
return "";
}
}

View File

@@ -10,6 +10,7 @@ import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -62,8 +63,8 @@ public class FontLoader {
}
public static Font loadFont( String name, String source ) {
Validator.requireNotNull(source, "Font source may not be null");
Validator.requireNotEmpty(source, "Font source may not be empty.");
Validator.requireNotNull(source, "source");
Validator.requireNotEmpty(source, "source");
if( fontCache.containsKey(name) ) {
LOG.trace("Retrieved font <%s> from font cache.", name);
@@ -91,10 +92,12 @@ public class FontLoader {
//ge.registerFont(font);
}
LOG.debug("Loaded custom font from source <%s>.", source);
} catch( MalformedURLException muex ) {
LOG.warn("Could not find font resource for <%s>", source);
} catch( IOException ioex ) {
LOG.error(ioex, "Error loading custom font file from source <%s>.", source);
LOG.warn(ioex, "Error loading custom font file from source <%s>.", source);
} catch( FontFormatException ffex ) {
LOG.error(ffex, "Error creating custom font from source <%s>.", source);
LOG.warn(ffex, "Error creating custom font from source <%s>.", source);
}
return font;

View File

@@ -16,6 +16,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
/**
* Eine Hilfsklasse mit Klassenmethoden, um Bilder zu laden.
@@ -67,8 +68,8 @@ public final class ImageLoader {
* @return
*/
public static BufferedImage loadImage( String source, boolean caching ) {
Validator.requireNotNull(source, "Image source may not be null");
Validator.requireNotEmpty(source, "Image source may not be empty.");
Validator.requireNotNull(source, "source");
Validator.requireNotEmpty(source, "source");
if( caching && imageCache.containsKey(source) ) {
return imageCache.get(source);
@@ -84,8 +85,10 @@ public final class ImageLoader {
if( caching && img != null ) {
imageCache.put(source, img);
}
} catch( MalformedURLException muex ) {
LOG.warn("Could not find image resource for <%s>", source);
} catch( IOException ioex ) {
LOG.error(ioex, "Error loading image file from source <%s>.", source);
LOG.warn(ioex, "Error loading image file from source <%s>.", source);
}
return img;
}
@@ -318,12 +321,8 @@ public final class ImageLoader {
* @throws IOException Falls es einen Fehler beim Speichern gab.
*/
public static void saveImage( BufferedImage image, File file, boolean overwriteIfExists ) throws IOException {
if( image == null ) {
throw new NullPointerException("Image may not be <null>.");
}
if( file == null ) {
throw new NullPointerException("File may not be <null>.");
}
Validator.requireNotNull(image, "image");
Validator.requireNotNull(file, "file");
if( file.isFile() ) {
// Datei existiert schon

View File

@@ -47,8 +47,8 @@ public class ResourceStreamProvider {
* einer bestehenden Ressource.
*/
public static URL getResourceURL( String source ) throws NullPointerException, IllegalArgumentException, IOException {
Validator.requireNotNull(source, "Resource source may not be null");
Validator.requireNotEmpty(source, "Resource source may not be empty.");
Validator.requireNotNull(source, "source");
Validator.requireNotEmpty(source, "source");
// Ist source ein valider Dateipfad?
File file = new File(source);