us.zuercher.gpx2map.image.netpbm
Class NetpbmImageFactory

java.lang.Object
  extended by us.zuercher.gpx2map.image.netpbm.NetpbmImageFactory
All Implemented Interfaces:
ImageFactory

public class NetpbmImageFactory
extends Object
implements ImageFactory

NetpbmImageFactory is a factory for NetpbmImage objects.

Author:
Stephan Zuercher

Nested Class Summary
private  class NetpbmImageFactory.CanLoadLogicTarget
          CanLoadLogicTarget implements NetpbmImageFactory.GzipRetryLogicTarget to retrieve the Netpbm magic number from an image file.
private static class NetpbmImageFactory.ExceptionHolder
          ExceptionHolder holds an IOException for later throwing.
private  class NetpbmImageFactory.GzipRetryLogic
          GzipRetryLogic encapsulates the logic of reading Netpbm files that may or may not be gzipped.
private static interface NetpbmImageFactory.GzipRetryLogicTarget
           
private  class NetpbmImageFactory.LoadImageLogicTarget
          LoadImageLogicTarget implements NetpbmImageFactory.GzipRetryLogicTarget to read image data from the input stream.
 
Field Summary
private static List<ImageFormatDescriptor> formatDescriptors
          List of ImageFormatDescriptor objects for our supported formats.
static NetpbmImageFactory instance
          NetpbmImageFactory singleton instance.
static ImageFormatDescriptor PAM_IMAGE_FORMAT_DESCRIPTOR
          PAM ImageFormatDescriptor.
static ImageFormatDescriptor PPM_IMAGE_FORMAT_DESCRIPTOR
          PPM ImageFormatDescriptor.
 
Constructor Summary
private NetpbmImageFactory()
          NetpbmImageFactory is a singleton.
 
Method Summary
 boolean canLoad(File imageFile)
          Tests whether the given file contains a supported Netpbm image format.
private  boolean canLoad(NetpbmImageFactory.GzipRetryLogic logic)
          Tests whether an image's format is supported using the given NetpbmImageFactory.GzipRetryLogic object.
 boolean canLoad(URL imageUrl)
          Tests whether the given URL refers to a supported Netpbm image format.
private  void closeQuietly(InputStream input)
          Closes an input stream and silently discards IOExceptions encountered during closure.
 List<ImageFormatDescriptor> getSupportedImageFormats()
          Returns the ImageFormatDescriptors for image formats supported by this factory.
 NetpbmImage loadImage(File netpbmImageFile)
          Loads a Netpbm-compatible image from the given filename.
private  NetpbmImage loadImage(NetpbmImageFactory.GzipRetryLogic logic)
          Loads an image using the given NetpbmImageFactory.GzipRetryLogic object.
 NetpbmImage loadImage(URL netpbmImageUrl)
          Loads a Netpbm-compatible image from the given URL.
private  NetpbmImage makeImage(InputStream input, NetpbmImageFactory.ExceptionHolder exceptionHolder)
          Load an image given an input stream.
private  BufferedInputStream makeInputStream(File file, boolean useGzip)
          Create a input stream from a File.
private  BufferedInputStream makeInputStream(URL url, boolean useGzip)
          Create a input stream from a URL.
private  String readMagic(InputStream input)
          Reads the Netpbm magic number from the start of a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAM_IMAGE_FORMAT_DESCRIPTOR

public static final ImageFormatDescriptor PAM_IMAGE_FORMAT_DESCRIPTOR
PAM ImageFormatDescriptor.


PPM_IMAGE_FORMAT_DESCRIPTOR

public static final ImageFormatDescriptor PPM_IMAGE_FORMAT_DESCRIPTOR
PPM ImageFormatDescriptor.


instance

public static final NetpbmImageFactory instance
NetpbmImageFactory singleton instance.


formatDescriptors

private static final List<ImageFormatDescriptor> formatDescriptors
List of ImageFormatDescriptor objects for our supported formats.

Constructor Detail

NetpbmImageFactory

private NetpbmImageFactory()
NetpbmImageFactory is a singleton.

See Also:
instance
Method Detail

getSupportedImageFormats

public List<ImageFormatDescriptor> getSupportedImageFormats()
Returns the ImageFormatDescriptors for image formats supported by this factory.

Specified by:
getSupportedImageFormats in interface ImageFactory
Returns:
the ImageFormatDescriptors for image formats supported by this factory.

canLoad

public boolean canLoad(File imageFile)
                throws IOException
Tests whether the given file contains a supported Netpbm image format.

Specified by:
canLoad in interface ImageFactory
Parameters:
imageFile - the file to test
Returns:
true if the file contains a supported Netpbm image format, false otherwise
Throws:
IOException - if the file cannot be read

canLoad

public boolean canLoad(URL imageUrl)
                throws IOException
Tests whether the given URL refers to a supported Netpbm image format.

Specified by:
canLoad in interface ImageFactory
Parameters:
imageUrl - the URL to test
Returns:
true if the URL refers to a supported Netpbm image format, false otherwise
Throws:
IOException - if the URL cannot be read

canLoad

private boolean canLoad(NetpbmImageFactory.GzipRetryLogic logic)
                 throws IOException
Tests whether an image's format is supported using the given NetpbmImageFactory.GzipRetryLogic object.

Parameters:
logic - Gzip retry logic for a file or URL
Returns:
true if the file's magic number indicates a supported Netpbm image format.
Throws:
IOException - if there's an error reading the image data

loadImage

public NetpbmImage loadImage(File netpbmImageFile)
                      throws IOException,
                             UnknownNetpbmFormatException
Loads a Netpbm-compatible image from the given filename. Image files may optionally be compressed using the gzip format. The factory detects image types and compressed images by attempting to read the magic number from the start of the file. If the magic number is not recognized, the file is reopened with gzip-decompression enabled. If the magic number is still not recognized, an exception if thown.

Specified by:
loadImage in interface ImageFactory
Parameters:
netpbmImageFile - location of a netpbm image
Returns:
an implementation of NetpbmImage
Throws:
IOException - if the file cannot be read or the magic number is not recognized.
UnknownNetpbmFormatException - if the file format is not recognized

loadImage

public NetpbmImage loadImage(URL netpbmImageUrl)
                      throws IOException,
                             UnknownNetpbmFormatException
Loads a Netpbm-compatible image from the given URL. Images may optionally be compressed using the gzip format. The factory detects image types and compressed images by attempting to read the magic number from the start of the file. If the magic number is not recognized, the URL is reopened with gzip-decompression enabled. If the magic number is still not recognized, an exception if thown.

Specified by:
loadImage in interface ImageFactory
Parameters:
netpbmImageUrl - location of a netpbm image
Returns:
an implementation of NetpbmImage
Throws:
IOException - if the file cannot be read or the magic number is not recognized.
UnknownNetpbmFormatException - if the file format is not recognized

loadImage

private NetpbmImage loadImage(NetpbmImageFactory.GzipRetryLogic logic)
                       throws IOException,
                              UnknownNetpbmFormatException
Loads an image using the given NetpbmImageFactory.GzipRetryLogic object.

Parameters:
logic - Gzip retry logic for a file or URL
Returns:
a NetpbmImage loaded by the gzip retry logic
Throws:
IOException - if there's an error reading the image data
UnknownNetpbmFormatException - if the image format is not supported

makeInputStream

private BufferedInputStream makeInputStream(File file,
                                            boolean useGzip)
                                     throws IOException
Create a input stream from a File.

Parameters:
file - the file to read
useGzip - flag controlling use of gzip
Returns:
a BufferedInputStream on file, possibly decompressing gzip
Throws:
IOException - if there's an error opening the file

makeInputStream

private BufferedInputStream makeInputStream(URL url,
                                            boolean useGzip)
                                     throws IOException
Create a input stream from a URL.

Parameters:
url - the URL to read
useGzip - flag controlling use of gzip
Returns:
a BufferedInputStream on file, possibly decompressing gzip
Throws:
IOException - if there's an error opening the URL

makeImage

private NetpbmImage makeImage(InputStream input,
                              NetpbmImageFactory.ExceptionHolder exceptionHolder)
                       throws IOException
Load an image given an input stream. Requires an input stream that supports mark/reset in order to check the image's magic number. If this method returns null or throws an IOException, the input stream is automatically closed. Otherwise, it is the responsibility of the NetpbmImage to close the input stream when all image data has been loaded.

Parameters:
input - an input stream that supports mark/reset
exceptionHolder - place to store the IOException that caused a null return value, if any.
Returns:
a NetpbmImage or null if the image magic number is not recognized, if there's an error reading the image header, or if the input stream is null
Throws:
IOException - if mark/reset is not supported; if there's an error reading the magic number; if the stream does not contain enough data to determine the magic number; if an error occurs while reseting the stream after reading the magic number.

readMagic

private String readMagic(InputStream input)
                  throws IOException
Reads the Netpbm magic number from the start of a file.

Parameters:
input - an input stream that supports mark/reset.
Returns:
the first two characters of the file, which may or may not be a valid Netpbm magic number.
Throws:
IOException - if there's an error reading the file or mark/reset is not supported

closeQuietly

private void closeQuietly(InputStream input)
Closes an input stream and silently discards IOExceptions encountered during closure.

Parameters:
input -