Class GZIPFileDataSource
- All Implemented Interfaces:
DataSource
Description Contents
Outline
This class providers a wrapper around a file such that GZIPed files will be unzipped when thegetInputStream()
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 Extension | Content 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 Summary
ConstructorsConstructorDescriptionGZIPFileDataSource
(File f, String zipExtension) Create a new data source for the specified file. -
Method Summary
Modifier and TypeMethodDescriptionGet the content type for this data source.Get an appropriate InputStream for the data source.getName()
Get the name of the data source.Get an appropriate OutputStream for the data source.void
Set the File to use for the data source.void
setZipExtension
(String ext) Set the zip extension to use.
-
Constructor Details
-
GZIPFileDataSource
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 tonull
to use the default of .gz.
-
-
Method Details
-
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 interfaceDataSource
- 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
Get an appropriate InputStream for the data source.
Here we just return a buffered GZIPInputStream.- Specified by:
getInputStream
in interfaceDataSource
- Returns:
- The InputStream for the data source.
- Throws:
IOException
- If we can't get the stream to the source.
-
getOutputStream
Get an appropriate OutputStream for the data source.
Here we just return a buffered GZIPOutputStream.- Specified by:
getOutputStream
in interfaceDataSource
- Returns:
- The OutputStream for the data source.
- Throws:
IOException
- If we can't get the stream to the source.
-
setZipExtension
Set the zip extension to use.- Parameters:
ext
- The zip extension.
-
setFile
Set the File to use for the data source.- Parameters:
file
- The file.
-
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 interfaceDataSource
- Returns:
- The name of the data source.
-