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.
private  boolean closed
          Indicate whether the encoder is closed or not.
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, java.lang.String bound)
          Create an encoder, using the DEFAULT_ENCODING encoding.
ByteArrayEncoderHTTP(UploadPolicy uploadPolicy, java.lang.String encoding, java.lang.String bound)
          Create an encoder, and specifies the 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(java.lang.String str)
          Append a string, to be encoded at the current end of the byte array.
 ByteArrayEncoder appendFileProperty(java.lang.String name, java.lang.String value)
          Append a property, name and value.
 ByteArrayEncoder appendFormVariables(java.lang.String formname)
          Add to the current encoder all properties contained in the given HTML form.
 void close()
          Closes the encoding writer, and prepares the encoded length and byte array.
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 encoding, java.lang.String bound)
          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

private 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

private 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,
                            java.lang.String bound)
                     throws JUploadIOException
Create an encoder, using the DEFAULT_ENCODING encoding.

Parameters:
uploadPolicy - The current upload policy
bound - The HTTP boundary. Can be null, if not used.
Throws:
JUploadIOException - Any IO exception

ByteArrayEncoderHTTP

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

Parameters:
uploadPolicy - The current upload policy
encoding - The encoding to use. For instance, "UTF-8".
bound - The HTTP boundary. Can be null, if not used.
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(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.

appendFileProperty

public ByteArrayEncoder appendFileProperty(java.lang.String name,
                                           java.lang.String value)
                                    throws JUploadIOException
Append a property, name and value. It will be encoded at the current end of the byte array.

Specified by:
appendFileProperty 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.
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.

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

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 encoding,
                  java.lang.String bound)
           throws JUploadIOException
Initialization: called by the constructors.

Throws:
JUploadIOException