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.
(package private)  boolean preparedForUpload
          Indicates whether the file is prepared for upload or not.
(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, int index)
          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
static File getRoot(File[] fileArray)
          Return the 'biggest' common ancestror of the given file array.
 long getUploadLength()
          Get size of upload, which may be different from the actual file length.
 boolean isPreparedForUpload()
          Indicates whether the file can be uploaded or not.
 
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.


preparedForUpload

boolean preparedForUpload
Indicates whether the file is prepared for upload or not.

See Also:
FileData.isPreparedForUpload()

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,
                                 int index)
                          throws JUploadIOException
Called during the upload, by the FileUploadThread. The FileData instance should then call the ByteArrayEncoder.appendTextProperty(String, String, int) 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
index - Index of the file concerned by this value. -1 if this is a global parameter.
Throws:
JUploadIOException - Encapsulation of the IOException, if any would occurs.
See Also:
ByteArrayEncoder.appendTextProperty(String, String, int)

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 the actual file length. This call is valid only after a call to FileData.beforeUpload() and before the call to FileData.afterUpload().

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 resource 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.
This method may only be called when FileData.isPreparedForUpload() returns true.

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.

getRoot

public static File getRoot(File[] fileArray)
Return the 'biggest' common ancestror of the given file array. For instance, the root for the files /usr/bin/toto and /usr/titi is /usr.

Parameters:
fileArray -
Returns:
The common root for the given files.

isPreparedForUpload

public boolean isPreparedForUpload()
Indicates whether the file can be uploaded or not. This boolean should be set to true in the call to FileData.beforeUpload(), and the to false in the call to FileData.afterUpload().

Specified by:
isPreparedForUpload in interface FileData
Returns:
True if the file is ready for upload.