wjhk.jupload2.upload
Class UploadFileData

java.lang.Object
  extended by wjhk.jupload2.upload.UploadFileData
All Implemented Interfaces:
FileData

public class UploadFileData
extends Object
implements FileData

This class implements the FileData interface, and is responsible to do the actual upload of the files.

Author:
etienne_sf

Constructor Summary
UploadFileData(FileData fileDataParam, FileUploadManagerThread fileUploadThreadParam, UploadPolicy uploadPolicyParam)
          Standard constructor for the UploadFileData class.
 
Method Summary
 void afterUpload()
          This method closes the inputstream, and remove the file from the filepanel.
 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.
 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()
          
(package private)  String getMD5()
          Retrieves the MD5 sum of the file.
 String getMimeType()
          This function return the FileData content type.
 String getRelativeDir()
          Retrieves the path of this file relative to it's root dir
(package private)  long getRemainingLength()
          Get the number of files that are still to upload.
 String getUploadFilename(int index)
          Retrieves the file name, that should be used in the server application.
 long getUploadLength()
          This methods stores locally the upload length.
 String getUploadName(int index)
          Retrieves the upload file name, that should be sent to the server.
 boolean isPreparedForUpload()
          Indicates whether the file can be uploaded or not.
(package private)  void uploadFile(OutputStream outputStream, long amount)
          This methods writes the file data (see FileData.getInputStream() to the given outputStream (the output toward the HTTP server).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UploadFileData

public UploadFileData(FileData fileDataParam,
                      FileUploadManagerThread fileUploadThreadParam,
                      UploadPolicy uploadPolicyParam)
Standard constructor for the UploadFileData class.

Parameters:
fileDataParam - The file data the this instance must transmist.
fileUploadThreadParam - The current instance of FileUploadThread
uploadPolicyParam - The current upload policy, instance of UploadPolicy
Method Detail

getRemainingLength

long getRemainingLength()
Get the number of files that are still to upload. It is initialized at the creation of the file, by a call to the FileData.getUploadLength().
Note: When the upload for this file is finish and you want to send it again (for instance the upload failed, and you want to do a retry), you should not reuse this instance, but, instead, create a new UploadFileData instance.

Returns:
Number of bytes still to upload.
See Also:
getInputStream()

getMD5

String getMD5()
        throws JUploadException
Retrieves the MD5 sum of the file.
Caution: since 3.3.0, this method has been rewrited. The file is now parsed once within this method. This allows proper calculation of the file head and tail, before upload.

Returns:
The corresponding MD5 sum.
Throws:
JUploadException

uploadFile

void uploadFile(OutputStream outputStream,
                long amount)
          throws JUploadException,
                 JUploadInterrupted
This methods writes the file data (see FileData.getInputStream() to the given outputStream (the output toward the HTTP server).

Parameters:
outputStream - The stream on which the data is to be written.
amount - The number of bytes to write.
Throws:
JUploadException - if an I/O error occurs.
JUploadInterrupted - Thrown when an interruption of the thread is detected.

afterUpload

public void afterUpload()
This method closes the inputstream, and remove the file from the filepanel. Then it calls FileData.afterUpload().

Specified by:
afterUpload in interface FileData
See Also:
FileData.afterUpload()

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

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.

getDirectory

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

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

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.

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.

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

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.

getLastModified

public Date getLastModified()

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

getMimeType

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

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

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.

getUploadFilename

public String getUploadFilename(int index)
                         throws JUploadException
Retrieves the file name, that should be used in the server application. Default is to send the original filename.

Parameters:
index - The index of this file in the current request to the server.
Returns:
The real file name. Not used in FTP upload.
Throws:
JUploadException - Thrown when an error occurs.
See Also:
UploadPolicy.getUploadFilename(FileData, int)

getUploadName

public String getUploadName(int index)
                     throws JUploadException
Retrieves the upload file name, that should be sent to the server. It's the technical name used to retrieve the file content. Default is File0, File1... This method just calls the UploadPolicy.getUploadFilename(FileData, int) method.

Parameters:
index - The index of this file in the current request to the server.
Returns:
The technical upload file name. Not used in FTP upload.
Throws:
JUploadException
See Also:
UploadPolicy.getUploadName(FileData, int)

getUploadLength

public long getUploadLength()
                     throws JUploadException
This methods stores locally the upload length. So, on the contrary of the FileData interface, this method may be called after afterUpload(), at one condition: that it has been called once before afterUpload() is called.

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:
FileData.getUploadLength()

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.