wjhk.jupload2.upload.helper
Class ByteArrayEncoderHTTP

java.lang.Object
  extended by wjhk.jupload2.upload.helper.ByteArrayEncoderHTTP
All Implemented Interfaces:
ByteArrayEncoder

public class ByteArrayEncoderHTTP
extends java.lang.Object
implements ByteArrayEncoder

This class is a utility, which provide easy encoding for HTTP queries. The way to use this class is:

  1. Instantiate a new object
  2. Append data to it, using the append methods. Available for: String, byte[], other ByteArrayEncode...
  3. Close the stream. This will prevent any new data to be appended to it. The encoded length can now be calculated.
  4. Get the encoded length.
  5. Get the encoded byte array

Author:
etienne_sf

Field Summary
private  java.io.ByteArrayOutputStream baos
          The actual array, which will collect the encoded bytes.
private  java.lang.String bound
          The boundary, to put between to post variables.
(package private)  boolean closed
          Indicate whether the encoder is closed or not.
(package private) static java.lang.String DEFAULT_ENCODING
          The default encoding.
private  byte[] encodedByteArray
          The encoded byte array.
private  int encodedLength
          The byte array length.
private  java.lang.String encoding
          The current encoding.
private  UploadPolicy uploadPolicy
          The current upload policy.
private  java.io.Writer writer
          The writer, that will encode the input parameters to baos.
 
Constructor Summary
ByteArrayEncoderHTTP(UploadPolicy uploadPolicy)
          Create an encoder, using the DEFAULT_ENCODING encoding.
ByteArrayEncoderHTTP(UploadPolicy uploadPolicy, java.lang.String bound)
          Create an encoder, and specifies the encoding to use.
ByteArrayEncoderHTTP(UploadPolicy uploadPolicy, java.lang.String bound, java.lang.String encoding)
          Create an encoder, and specifies the boundary and encoding to use.
 
Method Summary
 ByteArrayEncoder append(byte[] b)
          Append a stream, to be encoded at the current end of the byte array.
 ByteArrayEncoder append(ByteArrayEncoder bae)
          Append a string, to be encoded at the current end of the byte array.
 ByteArrayEncoder append(int b)
          Append a byte, to be encoded at the current end of the byte array. he byte to be written is the eight low-order bits of the argument b.
 ByteArrayEncoder append(java.lang.String str)
          Append a string, to be encoded at the current end of the byte array.
 ByteArrayEncoder appendEndPropertyList()
          Finish a property list.
 ByteArrayEncoder appendFormVariables(java.lang.String formname)
          Add to the current encoder all properties contained in the given HTML form.
 ByteArrayEncoder appendTextProperty(java.lang.String name, java.lang.String value, int index)
          Append a property, name and value.
 void close()
          Closes the encoding writer, and prepares the encoded length and byte array.
 java.lang.String getBoundary()
          Gets the HTTP boundary, that separate the form variables.
static java.lang.String getDefaultEncoding()
           
 byte[] getEncodedByteArray()
          Get the encoded result.
 int getEncodedLength()
          Get the length of the encoded result.
 java.lang.String getEncoding()
          
 java.lang.String getString()
          Get the String that matches the encoded result.
private  void init(UploadPolicy uploadPolicy, java.lang.String bound, java.lang.String encoding)
          Initialization: called by the constructors.
 boolean isClosed()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ENCODING

static final java.lang.String DEFAULT_ENCODING
The default encoding. It can be retrieved with getDefaultEncoding().

See Also:
Constant Field Values

bound

private java.lang.String bound
The boundary, to put between to post variables. Can not be changed during the object 'life'.


encoding

private java.lang.String encoding
The current encoding. Can not be changed during the object 'life'.


closed

boolean closed
Indicate whether the encoder is closed or not. If closed, it's impossible to append new data to it. If not closed, it's impossible to get the encoded length or the encoded byte array.
Note: a closed byte array can not be re-opened.


baos

private java.io.ByteArrayOutputStream baos
The actual array, which will collect the encoded bytes.


encodedLength

private int encodedLength
The byte array length. Calculated when the ByteArrayOutput is closed.


encodedByteArray

private byte[] encodedByteArray
The encoded byte array. Calculated when the ByteArrayOutput is closed.


uploadPolicy

private UploadPolicy uploadPolicy
The current upload policy.


writer

private java.io.Writer writer
The writer, that will encode the input parameters to baos.

Constructor Detail

ByteArrayEncoderHTTP

public ByteArrayEncoderHTTP(UploadPolicy uploadPolicy)
                     throws JUploadIOException
Create an encoder, using the DEFAULT_ENCODING encoding.

Parameters:
uploadPolicy - The current upload policy
Throws:
JUploadIOException - Any IO exception

ByteArrayEncoderHTTP

public ByteArrayEncoderHTTP(UploadPolicy uploadPolicy,
                            java.lang.String bound)
                     throws JUploadIOException
Create an encoder, and specifies the encoding to use.

Parameters:
uploadPolicy - The current upload policy
bound - Any specific boundary. May be null: in this case a default boundary is used.
Throws:
JUploadIOException - Any IO exception

ByteArrayEncoderHTTP

public ByteArrayEncoderHTTP(UploadPolicy uploadPolicy,
                            java.lang.String bound,
                            java.lang.String encoding)
                     throws JUploadIOException
Create an encoder, and specifies the boundary and encoding to use.

Parameters:
uploadPolicy - The current upload policy
bound - Any specific boundary. May be null: in this case a default boundary is used.
encoding - The encoding to use. For instance, "UTF-8".
Throws:
JUploadIOException - Any IO exception
Method Detail

close

public void close()
           throws JUploadIOException
Description copied from interface: ByteArrayEncoder
Closes the encoding writer, and prepares the encoded length and byte array. This method must be called before call to ByteArrayEncoder.getEncodedLength() and ByteArrayEncoder.getEncodedByteArray(). Note: After a call to this method, you can not append any new data to the encoder.

Specified by:
close in interface ByteArrayEncoder
Throws:
JUploadIOException
See Also:
ByteArrayEncoder.close()

append

public ByteArrayEncoder append(java.lang.String str)
                        throws JUploadIOException
Append a string, to be encoded at the current end of the byte array.

Specified by:
append in interface ByteArrayEncoder
Parameters:
str - The string to append and encode.
Returns:
Return the current ByteArrayEncoder, to allow chained call (see explanation, here above).
Throws:
JUploadIOException

append

public ByteArrayEncoder append(int b)
                        throws JUploadIOException
Append a byte, to be encoded at the current end of the byte array. he byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Specified by:
append in interface ByteArrayEncoder
Parameters:
b - Writes the specified byte to this output stream.
Returns:
Return the current ByteArrayEncoder, to allow chained call (see explanation, here above).
Throws:
JUploadIOException

append

public ByteArrayEncoder append(byte[] b)
                        throws JUploadIOException
Append a stream, to be encoded at the current end of the byte array.

Specified by:
append in interface ByteArrayEncoder
Returns:
Return the current ByteArrayEncoder, to allow chained call (see explanation, here above).
Throws:
JUploadIOException

append

public ByteArrayEncoder append(ByteArrayEncoder bae)
                        throws JUploadIOException
Append a string, to be encoded at the current end of the byte array.

Specified by:
append in interface ByteArrayEncoder
Parameters:
bae - The ByteArrayEncoder whose encoding result should be appended to the current encoder. bae must be closed, before being appended.
Returns:
Return the current ByteArrayEncoder, to allow chained call (see explanation, here above).
Throws:
JUploadIOException - This exception is thrown when this method is called on a non-closed encoder.

appendTextProperty

public ByteArrayEncoder appendTextProperty(java.lang.String name,
                                           java.lang.String value,
                                           int index)
                                    throws JUploadIOException
Append a property, name and value. It will be encoded at the current end of the byte array.
Note: After the last call to appendTextProperty, you should call ByteArrayEncoder.appendEndPropertyList(), to properly finish the property list. In HTTP mode, it will add the last boundary, at a specific format.

Specified by:
appendTextProperty in interface ByteArrayEncoder
Parameters:
name - Name of the property to be added
value - Value of this property for the current file. It's up to the caller to call this method at the right time.
index - Index of the file concerned by this value. -1 if this is a global parameter.
Returns:
Return the current ByteArrayEncoder, to allow chained call (see explanation, here above).
Throws:
JUploadIOException
See Also:
ByteArrayEncoder.appendEndPropertyList()

appendEndPropertyList

public ByteArrayEncoder appendEndPropertyList()
                                       throws JUploadIOException
Finish a property list. In HTTP mode, the last boundary for the form/multipart content is added. After a call to this method, no more property may be written. If several ByteEncoder are used, it's up to the called to call this mehod only once, for the ByteEncoder that will be written last on the request.

Specified by:
appendEndPropertyList in interface ByteArrayEncoder
Returns:
Return the current ByteArrayEncoder, to allow chained call (see explanation, here above).
Throws:
JUploadIOException

appendFormVariables

public ByteArrayEncoder appendFormVariables(java.lang.String formname)
                                     throws JUploadIOException
Add to the current encoder all properties contained in the given HTML form. There is no index for the current file: all the form parameters are global parameters (index will be set to -1 when calling ByteArrayEncoder.appendTextProperty(String, String, int).

Specified by:
appendFormVariables in interface ByteArrayEncoder
Parameters:
formname - The HTML form name. This method will get the data from this form, by using the JUploadContext.getApplet() method.
Returns:
Return the current ByteArrayEncoder, to allow chained call (see explanation, here above).
Throws:
JUploadIOException

getBoundary

public java.lang.String getBoundary()
Gets the HTTP boundary, that separate the form variables.

Specified by:
getBoundary in interface ByteArrayEncoder
Returns:
The HTTP boundary, that was generated when the instance was created.

getDefaultEncoding

public static java.lang.String getDefaultEncoding()
Returns:
value of the DEFAULT_ENCODING constant.

isClosed

public boolean isClosed()

Specified by:
isClosed in interface ByteArrayEncoder
Returns:
the closed

getEncoding

public java.lang.String getEncoding()

Specified by:
getEncoding in interface ByteArrayEncoder
Returns:
the encoding

getEncodedLength

public int getEncodedLength()
                     throws JUploadIOException
Get the length of the encoded result. Can be called only once the encoder has been closed.

Specified by:
getEncodedLength in interface ByteArrayEncoder
Returns:
the encodedLength
Throws:
JUploadIOException - This exception is thrown when this method is called on a non-closed encoder.

getEncodedByteArray

public byte[] getEncodedByteArray()
                           throws JUploadIOException
Get the encoded result. Can be called only once the encoder has been closed.

Specified by:
getEncodedByteArray in interface ByteArrayEncoder
Returns:
the encodedByteArray
Throws:
JUploadIOException - This exception is thrown when this method is called on a non-closed encoder.

getString

public java.lang.String getString()
                           throws JUploadIOException
Get the String that matches the encoded result. Can be called only once the encoder has been closed.

Specified by:
getString in interface ByteArrayEncoder
Returns:
the String that has been encoded.
Throws:
JUploadIOException - This exception is thrown when this method is called on a non-closed encoder.

init

private void init(UploadPolicy uploadPolicy,
                  java.lang.String bound,
                  java.lang.String encoding)
           throws JUploadIOException
Initialization: called by the constructors.

Throws:
JUploadIOException