public class GZIPFileDataSource
extends java.lang.Object
implements javax.activation.DataSource
getInputStream()
method is called. Or
that the file will be zipped with the named zip file extension when
the getOutputStream()
method is called.
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.
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.
If the file extension does not match any of the "pre-defined" file extensions then a content type of application/octet-stream is returned.
Both the input and output streams that you gain from this class are buffered.
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 and Description |
---|
GZIPFileDataSource(java.io.File f,
java.lang.String zipExtension)
Create a new data source for the specified file.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getContentType()
Get the content type for this data source.
|
java.io.InputStream |
getInputStream()
Get an appropriate InputStream for the data source.
|
java.lang.String |
getName()
Get the name of the data source.
|
java.io.OutputStream |
getOutputStream()
Get an appropriate OutputStream for the data source.
|
void |
setFile(java.io.File file)
Set the File to use for the data source.
|
void |
setZipExtension(java.lang.String ext)
Set the zip extension to use.
|
public GZIPFileDataSource(java.io.File f, java.lang.String zipExtension)
We expect the file to have the file extension as given by zipExtension.
f
- The File.zipExtension
- The file extension for gzipped files. Set to null
to use the default of .gz.public java.lang.String getContentType()
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.
getContentType
in interface javax.activation.DataSource
public java.io.InputStream getInputStream() throws java.io.IOException
getInputStream
in interface javax.activation.DataSource
java.io.IOException
- If we can't get the stream to the source.public java.io.OutputStream getOutputStream() throws java.io.IOException
getOutputStream
in interface javax.activation.DataSource
java.io.IOException
- If we can't get the stream to the source.public void setZipExtension(java.lang.String ext)
ext
- The zip extension.public void setFile(java.io.File file)
file
- The file.public java.lang.String getName()
getName
in interface javax.activation.DataSource