wjhk.jupload2.filedata
Class DefaultFileData

java.lang.Object
  extended by wjhk.jupload2.filedata.DefaultFileData
All Implemented Interfaces:
FileData
Direct Known Subclasses:
AudioFileData, PictureFileData

public class DefaultFileData
extends Object
implements FileData

This class contains all data and methods for a file to upload. The current UploadPolicy contains the necessary parameters to personalize the way files must be handled.

This class is the default FileData implementation. It gives the default behaviour, and is used by DefaultUploadPolicy. It provides standard control on the files choosen for upload.

Author:
etienne_sf
See Also:
FileData

Field Summary
protected  String mimeType
          Mime type of the file.
static Properties mimeTypes
          the mime type list, coming from: http://www.mimetype.org/ Thanks to them!
(package private)  UploadPolicy uploadPolicy
          The current upload policy.
 
Constructor Summary
DefaultFileData(File file, File root, UploadPolicy uploadPolicy)
          Standard constructor
 
Method Summary
 void afterUpload()
          This function is called after upload, whether it is successful or not.
 void appendFileProperties(wjhk.jupload2.upload.helper.ByteArrayEncoder bae)
          Called during the upload, by the FileUploadThread.
 void beforeUpload()
          Prepare the fileData to upload.
 boolean canRead()
          Indicate if this file can be read.
 String getDirectory()
          Get the directory of the file.
static String getExtension(File file)
          Returns the extension of the given file.
 File getFile()
          Standard getter, for the file described by the FileData instance.
 String getFileExtension()
          
 long getFileLength()
          
 String getFileName()
          Get the original filename.
 InputStream getInputStream()
          This function creates an InputStream from this file.
 Date getLastModified()
          
 String getMimeType()
          This function return the FileData content type.
 String getRelativeDir()
          Retrieves the path of this file relative to it's root dir
 long getUploadLength()
          Get size of upload, which may be different from th actual file length.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

uploadPolicy

UploadPolicy uploadPolicy
The current upload policy.


mimeTypes

public static Properties mimeTypes
the mime type list, coming from: http://www.mimetype.org/ Thanks to them!


mimeType

protected String mimeType
Mime type of the file. It will be written in the upload HTTP request.

Constructor Detail

DefaultFileData

public DefaultFileData(File file,
                       File root,
                       UploadPolicy uploadPolicy)
Standard constructor

Parameters:
file - The file whose data this instance will give.
root - The directory root, to be able to calculate the result of getRelativeDir()
uploadPolicy - The current upload policy.
Method Detail

appendFileProperties

public void appendFileProperties(wjhk.jupload2.upload.helper.ByteArrayEncoder bae)
                          throws JUploadIOException
Called during the upload, by the FileUploadThread. The FileData instance should then call the ByteArrayEncoder.appendFileProperty(String, String) method to add each file property to the current upload.

Specified by:
appendFileProperties in interface FileData
Parameters:
bae - The byte encoder, where the properties must be added
Throws:
JUploadIOException - Encapsulation of the IOException, if any would occurs.
See Also:
ByteArrayEncoder.appendFileProperty(String, String)

beforeUpload

public void beforeUpload()
                  throws JUploadException
Prepare the fileData to upload. For instance, picture data can be resized before upload (see PictureFileData. This method is called before the upload of this file.

Specified by:
beforeUpload in interface FileData
Throws:
JUploadException - Encapsulation of the Exception, if any would occurs.
See Also:
FileUploadThread

getUploadLength

public long getUploadLength()
                     throws JUploadException
Get size of upload, which may be different from th actual file length.

Specified by:
getUploadLength in interface FileData
Returns:
The length of upload. In this class, this is the size of the file, as it isn't transformed for upload. This size may change if encoding is necessary (needs a new FileData class), or if picture is to be resized or rotated.
Throws:
JUploadException - Encapsulation of the Exception, if any would occurs.
See Also:
PictureFileData

afterUpload

public void afterUpload()
This function is called after upload, whether it is successful or not. It allows fileData to free any resssource created for the upload. For instance, PictureFileData.afterUpload() removes the temporary file, if any was created.

Specified by:
afterUpload in interface FileData

getInputStream

public InputStream getInputStream()
                           throws JUploadException
This function creates an InputStream from this file. The FileUploadThread class then reads bytes from it and transfers them to the webserver. The caller is responsible for closing this stream.

Specified by:
getInputStream in interface FileData
Returns:
An InputStream, representing this instance.
Throws:
JUploadException - Encapsulation of the Exception, if any would occurs.

getFileName

public String getFileName()
Get the original filename. This is the name of the file, into the local hardrive

Specified by:
getFileName in interface FileData
Returns:
The original filename

getFileExtension

public String getFileExtension()

Specified by:
getFileExtension in interface FileData
Returns:
The extension for the original file.

getFileLength

public long getFileLength()

Specified by:
getFileLength in interface FileData
Returns:
The length of the original file.

getLastModified

public Date getLastModified()

Specified by:
getLastModified in interface FileData
Returns:
The original file date.

getDirectory

public String getDirectory()
Get the directory of the file.

Specified by:
getDirectory in interface FileData
Returns:
The directory where this file is stored.

getMimeType

public String getMimeType()
This function return the FileData content type.

Specified by:
getMimeType in interface FileData
Returns:
The mimeType for the file.

canRead

public boolean canRead()
Indicate if this file can be read. Take care of the File.canRead() methods, that seems to be wrong from time to time.

Specified by:
canRead in interface FileData
Returns:
indicates whether the file can be read or not.

getFile

public File getFile()
Standard getter, for the file described by the FileData instance.

Specified by:
getFile in interface FileData
Returns:
the File instance associated with this row.

getRelativeDir

public String getRelativeDir()
Retrieves the path of this file relative to it's root dir

Specified by:
getRelativeDir in interface FileData
Returns:
This instance's relative path or an empty string if it was not created using a root parameter.

getExtension

public static String getExtension(File file)
Returns the extension of the given file. To be clear: jpg is the extension for the file named picture.jpg.

Parameters:
file - the file whose the extension is wanted!
Returns:
The extension, without the point, for the given file.