wjhk.jupload2.upload
Class FileUploadThreadHTTP

java.lang.Object
  extended by java.lang.Thread
      extended by wjhk.jupload2.upload.DefaultFileUploadThread
          extended by wjhk.jupload2.upload.FileUploadThreadHTTP
All Implemented Interfaces:
Runnable, FileUploadThread

public class FileUploadThreadHTTP
extends DefaultFileUploadThread

This class implements the file upload via HTTP POST request.

Version:
$Revision: 450 $
Author:
etienne_sf

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class wjhk.jupload2.upload.DefaultFileUploadThread
filesToUpload, maxChunkSize, nbMaxFilesPerUpload, stop, uploadException, uploadPolicy
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
FileUploadThreadHTTP(FileData[] filesDataParam, UploadPolicy uploadPolicy, JProgressBar progress)
          Creates a new instance.
 
Method Summary
(package private)  void afterFile(int index)
          Idem as DefaultFileUploadThread.beforeFile(int), but is called after each file (and each chunks for each file).
(package private)  void beforeFile(int index)
          This method is called before sending the bytes corresponding to the file whose index is given in argument.
(package private)  void beforeRequest(int firstFileToUploadParam, int nbFilesToUploadParam)
          This method is called before starting of each request.
(package private) static byte[] byteAppend(byte[] buf1, byte[] buf2)
          Concatenates two byte arrays.
(package private) static byte[] byteAppend(byte[] buf1, byte[] buf2, int len)
          Concatenates two byte arrays.
(package private)  void cleanAll()
          Clean any used resource, like a 'permanent' connection.
(package private)  void cleanRequest()
          Clean any used resource of the last executed request.
(package private)  int finishRequest()
          This method is called at the end of each request.
(package private)  long getAdditionnalBytesForUpload(int index)
          This methods upload overhead for the file number indexFile in the filesDataParam given to the constructor.
(package private)  OutputStream getOutputStream()
          Get the output stream where the files should be written for upload.
(package private)  String getResponseBody()
          Return the the body for the server response.
(package private) static byte[] readLine(InputStream inputStream, boolean includeCR)
          Similar like BufferedInputStream#readLine() but operates on raw bytes.
(package private) static String readLine(InputStream inputStream, String charset, boolean includeCR)
          Similar like BufferedInputStream#readLine() but operates on raw bytes.
(package private)  void startRequest(long contentLength, boolean bChunkEnabled, int chunkPart, boolean bLastChunk)
          This method is called for each upload request to the server.
 
Methods inherited from class wjhk.jupload2.upload.DefaultFileUploadThread
close, getException, getResponseMsg, getStartTime, getTotalLength, getUploadedLength, isUploadStopped, nbBytesUploaded, quoteCRLF, run, setResponseMsg, stopUpload
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface wjhk.jupload2.upload.FileUploadThread
isAlive, join, join, start
 

Constructor Detail

FileUploadThreadHTTP

public FileUploadThreadHTTP(FileData[] filesDataParam,
                            UploadPolicy uploadPolicy,
                            JProgressBar progress)
Creates a new instance.

Parameters:
filesDataParam - The files to upload.
uploadPolicy - The policy to be applied.
progress - The progress bar to be updated.
Method Detail

beforeRequest

void beforeRequest(int firstFileToUploadParam,
                   int nbFilesToUploadParam)
             throws JUploadException
Description copied from class: DefaultFileUploadThread
This method is called before starting of each request. It can be used to prepare any work, before starting the request. For instance, in HTTP, the tail must be properly calculated, as the last one must be different from the others.

Specified by:
beforeRequest in class DefaultFileUploadThread
Throws:
JUploadException
See Also:
DefaultFileUploadThread.beforeRequest(int, int)

getAdditionnalBytesForUpload

long getAdditionnalBytesForUpload(int index)
                            throws JUploadIOException
Description copied from class: DefaultFileUploadThread
This methods upload overhead for the file number indexFile in the filesDataParam given to the constructor. For instance, in HTTP, the upload contains a head and a tail for each files.

Specified by:
getAdditionnalBytesForUpload in class DefaultFileUploadThread
Parameters:
index - The index of the file in the filesDataParam array, whose addtional length is asked.
Returns:
The additional number of bytes for this file.
Throws:
JUploadIOException
See Also:
DefaultFileUploadThread.getAdditionnalBytesForUpload(int)

afterFile

void afterFile(int index)
         throws JUploadIOException
Description copied from class: DefaultFileUploadThread
Idem as DefaultFileUploadThread.beforeFile(int), but is called after each file (and each chunks for each file).

Specified by:
afterFile in class DefaultFileUploadThread
Parameters:
index - The index of the file that was just sent.
Throws:
JUploadIOException
See Also:
DefaultFileUploadThread.afterFile(int)

beforeFile

void beforeFile(int index)
          throws JUploadException
Description copied from class: DefaultFileUploadThread
This method is called before sending the bytes corresponding to the file whose index is given in argument. If the file is splitted in chunks (see the maxChunkSize applet parameter), this method is called before each chunk for this file.

Specified by:
beforeFile in class DefaultFileUploadThread
Parameters:
index - The index of the file that will be sent just after
Throws:
JUploadException
See Also:
DefaultFileUploadThread.beforeFile(int)

cleanAll

void cleanAll()
        throws JUploadException
Description copied from class: DefaultFileUploadThread
Clean any used resource, like a 'permanent' connection. This method is called after the end of the last request (see on the top of this page for details).

Specified by:
cleanAll in class DefaultFileUploadThread
Throws:
JUploadException
See Also:
DefaultFileUploadThread.cleanAll()

cleanRequest

void cleanRequest()
            throws JUploadException
Description copied from class: DefaultFileUploadThread
Clean any used resource of the last executed request. In HTTP mode, the output stream, input stream and the socket should be cleaned here.

Specified by:
cleanRequest in class DefaultFileUploadThread
Throws:
JUploadException
See Also:
DefaultFileUploadThread.cleanRequest()

finishRequest

int finishRequest()
            throws JUploadException
Description copied from class: DefaultFileUploadThread
This method is called at the end of each request.

Specified by:
finishRequest in class DefaultFileUploadThread
Returns:
The response status code from the server (200 == OK)
Throws:
JUploadException
See Also:
DefaultFileUploadThread.startRequest(long, boolean, int, boolean)

getResponseBody

String getResponseBody()
Description copied from class: DefaultFileUploadThread
Return the the body for the server response. That is: the server response without the http header. This the real functionnal response from the server application, that would be outputed, for instance, by any 'echo' PHP command.

Specified by:
getResponseBody in class DefaultFileUploadThread
See Also:
DefaultFileUploadThread.getResponseBody()

getOutputStream

OutputStream getOutputStream()
                       throws JUploadException
Description copied from class: DefaultFileUploadThread
Get the output stream where the files should be written for upload.

Specified by:
getOutputStream in class DefaultFileUploadThread
Returns:
The target output stream for upload.
Throws:
JUploadException
See Also:
DefaultFileUploadThread.getOutputStream()

startRequest

void startRequest(long contentLength,
                  boolean bChunkEnabled,
                  int chunkPart,
                  boolean bLastChunk)
            throws JUploadException
Description copied from class: DefaultFileUploadThread
This method is called for each upload request to the server. The number of request to the server depends on:
  • The total number of files to upload.
  • The value of the nbFilesPerRequest applet parameter.
  • The value of the maxChunkSize applet parameter.
  • The main objective of this method is to open the connection to the server, where the files to upload will be written. It should also send any header necessary for this upload request. The DefaultFileUploadThread.getOutputStream() methods is then called to know where the uploaded files should be written.
    Note: it's up to the class containing this method to internally manage the connection.

    Specified by:
    startRequest in class DefaultFileUploadThread
    Parameters:
    contentLength - The total number of bytes for the files (or the chunk) to upload in this query.
    bChunkEnabled - True if this upload is part of a file (can occurs only if the maxChunkSize applet parameter is set). False otherwise.
    chunkPart - The chunk number. Should be ignored if bChunkEnabled is false.
    bLastChunk - True if in chunk mode, and this upload is the last one. Should be ignored if bChunkEnabled is false.
    Throws:
    JUploadException
    See Also:
    DefaultFileUploadThread.startRequest(long, boolean, int, boolean)

    readLine

    static String readLine(InputStream inputStream,
                           String charset,
                           boolean includeCR)
                    throws IOException
    Similar like BufferedInputStream#readLine() but operates on raw bytes. Line-Ending is always "\r\n".

    Parameters:
    charset - The input charset of the stream.
    includeCR - Set to true, if the terminating CR/LF should be included in the returned byte array.
    Throws:
    IOException

    readLine

    static byte[] readLine(InputStream inputStream,
                           boolean includeCR)
                    throws IOException
    Similar like BufferedInputStream#readLine() but operates on raw bytes. Line-Ending is always "\r\n".

    Parameters:
    includeCR - Set to true, if the terminating CR/LF should be included in the returned byte array.
    Throws:
    IOException

    byteAppend

    static byte[] byteAppend(byte[] buf1,
                             byte[] buf2)
    Concatenates two byte arrays.

    Parameters:
    buf1 - The first array
    buf2 - The second array
    Returns:
    A byte array, containing buf2 appended to buf2

    byteAppend

    static byte[] byteAppend(byte[] buf1,
                             byte[] buf2,
                             int len)
    Concatenates two byte arrays.

    Parameters:
    buf1 - The first array
    buf2 - The second array
    len - Number of bytes to copy from buf2
    Returns:
    A byte array, containing buf2 appended to buf2