Class GZIPFileDataSource

  • All Implemented Interfaces:
    javax.activation.DataSource

    public class GZIPFileDataSource
    extends Object
    implements javax.activation.DataSource
    A Class for handling a gzipped file as a DataSource.

    Description Contents

    Outline

    This class providers a wrapper around a file such that GZIPed files will be unzipped when the getInputStream() method is called. Or that the file will be zipped with the named zip file extension when the getOutputStream() method is called.

    Zip file extension

    When used as an output this class expects your file to be gzipped and be able to be unzipped using a GZIPInputStream.

    When used an an input we use a GZIPOutputStream to write the file in a gzipped form.

    For the content type we only support a number of different content types and this is based upon the file extension without any zip file extension. What this means is that if you have a File called:

       myFile.txt.gz
     

    And you call the getName() method then the name:

       myFile.txt
     

    Will be returned and the content type would be: text/plain.

    If you don't specify a zipExtension then a default of .gz is used.

    Content Types

    The file extensions we support and their content types are listed below (in other words does the file name end with the value specified):

    File ExtensionContent Type Returned
    .doc application/msword
    .sdw application/x-swriter
    .rtf text/rtf
    .html text/html
    .txt text/plain

    It should be noted that file extensions are case-insensitive, this is because on Windows platforms .doc and .DOC are treated the same.

    If you know of any other content types/file extensions that MUST be supported directly by this class then please contact code-monkey@gentlyweb.com with details.

    Default content type

    If the file extension does not match any of the "pre-defined" file extensions then a content type of application/octet-stream is returned.

    Stream Buffering

    Both the input and output streams that you gain from this class are buffered.

    Thread safety and reuse

    It is possible to reuse this class by setting a new File via the setFile(File) method and potentially a different zip extension via the setZipExtension(String).

    This class is NOT Thread safe and you should synchronize externally if you plan to use multiple Threads with it.

    • Constructor Detail

      • GZIPFileDataSource

        public GZIPFileDataSource​(File f,
                                  String zipExtension)
        Create a new data source for the specified file.

        We expect the file to have the file extension as given by zipExtension.

        Parameters:
        f - The File.
        zipExtension - The file extension for gzipped files. Set to null to use the default of .gz.
    • Method Detail

      • getContentType

        public String getContentType()
        Get the content type for this data source.

        We base the content type on the file extension of the file minus the zipExtension, so if a file is called myFile.txt.gz and the zip extension is .gz then we trim off the .gz and then look for the "real" file extension, then determine the appropriate content type and return it.

        You should note that the file DOESN'T have to have the zipExtension for this method to work.

        If we don't have a specific file extension to use (see the table of content type to file extension mappings for full details of what this method returns.

        Specified by:
        getContentType in interface javax.activation.DataSource
        Returns:
        The content type based upon the file extension of the file, or application/octet-stream if we don't recognise the file extension.
      • getInputStream

        public InputStream getInputStream()
                                   throws IOException
        Get an appropriate InputStream for the data source.

        Here we just return a buffered GZIPInputStream.
        Specified by:
        getInputStream in interface javax.activation.DataSource
        Returns:
        The InputStream for the data source.
        Throws:
        IOException - If we can't get the stream to the source.
      • getOutputStream

        public OutputStream getOutputStream()
                                     throws IOException
        Get an appropriate OutputStream for the data source.

        Here we just return a buffered GZIPOutputStream.
        Specified by:
        getOutputStream in interface javax.activation.DataSource
        Returns:
        The OutputStream for the data source.
        Throws:
        IOException - If we can't get the stream to the source.
      • setZipExtension

        public void setZipExtension​(String ext)
        Set the zip extension to use.
        Parameters:
        ext - The zip extension.
      • setFile

        public void setFile​(File file)
        Set the File to use for the data source.
        Parameters:
        file - The file.
      • getName

        public String getName()
        Get the name of the data source.

        If the file ends with the zipExtension then we strip that off before returning the name.
        Specified by:
        getName in interface javax.activation.DataSource
        Returns:
        The name of the data source.