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

View File

@@ -10,6 +10,7 @@ import java.awt.Image;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -62,8 +63,8 @@ public class FontLoader {
} }
public static Font loadFont( String name, String source ) { public static Font loadFont( String name, String source ) {
Validator.requireNotNull(source, "Font source may not be null"); Validator.requireNotNull(source, "source");
Validator.requireNotEmpty(source, "Font source may not be empty."); Validator.requireNotEmpty(source, "source");
if( fontCache.containsKey(name) ) { if( fontCache.containsKey(name) ) {
LOG.trace("Retrieved font <%s> from font cache.", name); LOG.trace("Retrieved font <%s> from font cache.", name);
@@ -91,10 +92,12 @@ public class FontLoader {
//ge.registerFont(font); //ge.registerFont(font);
} }
LOG.debug("Loaded custom font from source <%s>.", source); 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 ) { } 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 ) { } 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; return font;

View File

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

View File

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