org.apache.click.extras.gae
Class MemoryFileItem

java.lang.Object
  extended by org.apache.click.extras.gae.MemoryFileItem
All Implemented Interfaces:
Serializable, FileItem, FileItemHeadersSupport

public class MemoryFileItem
extends Object
implements FileItem, FileItemHeadersSupport

Provides an In-Memory FileItem implementation which represents a file or form item that was received within a multipart/form-data POST request.

This FileItem implementation can safely be used with Google App Engine (GAE) since the file content is not written to disk.

The MemoryFileItem is based on the Commons FileUpload project.

See Also:
Serialized Form

Field Summary
static String DEFAULT_CHARSET
          Default content charset to be used when no explicit charset parameter is provided by the sender.
 
Constructor Summary
MemoryFileItem(String fieldName, String contentType, boolean isFormField, String fileName)
          Constructs a new MemoryFileItem for the given fieldName, contentType isFormField and fileName parameters.
 
Method Summary
 void delete()
          This method does nothing since the file is stored in memory only.
 byte[] get()
          Returns the contents of the file item as an array of bytes.
 String getCharSet()
          Returns the content charset passed by the agent or null if not defined.
 String getContentType()
          Returns the content type passed by the browser or null if not defined.
 String getFieldName()
          Returns the name of the field in the multipart form corresponding to this file item.
 FileItemHeaders getHeaders()
          Returns the file item headers.
 InputStream getInputStream()
          Returns an InputStream that can be used to retrieve the contents of the file.
 String getName()
          Returns the original filename in the client's filesystem, as provided by the browser (or other client software).
 OutputStream getOutputStream()
          Returns an OutputStream that can be used for storing the contents of the file.
 long getSize()
          Returns the size of the file item, in bytes.
 String getString()
          Returns the contents of the file as a String, using the default character encoding.
 String getString(String charset)
          Returns the contents of the file as a String, using the specified encoding.
 boolean isFormField()
          Determines whether or not a FileItem instance represents a simple form field.
 boolean isInMemory()
          Provides a hint as to whether or not the file contents will be read from memory.
 void setFieldName(String fieldName)
          Sets the field name used to reference this file item.
 void setFormField(boolean isFormField)
          Specifies whether or not a FileItem instance represents a simple form field.
 void setHeaders(FileItemHeaders headers)
          Sets the file item headers.
 void write(File file)
          This method does nothing since the file is stored in memory only.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARSET

public static final String DEFAULT_CHARSET
Default content charset to be used when no explicit charset parameter is provided by the sender. Media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP.

See Also:
Constant Field Values
Constructor Detail

MemoryFileItem

public MemoryFileItem(String fieldName,
                      String contentType,
                      boolean isFormField,
                      String fileName)
Constructs a new MemoryFileItem for the given fieldName, contentType isFormField and fileName parameters.

Parameters:
fieldName - the name of the form field as provided by the browser
contentType - the content type passed by the browser
isFormField - specifies whether or not this item is a simple form field
fileName - the original filename in the user's filesystem
Method Detail

delete

public void delete()
This method does nothing since the file is stored in memory only.

Specified by:
delete in interface FileItem

get

public byte[] get()
Returns the contents of the file item as an array of bytes.

Specified by:
get in interface FileItem
Returns:
the contents of the file item as an array of bytes

getContentType

public String getContentType()
Returns the content type passed by the browser or null if not defined.

Specified by:
getContentType in interface FileItem
Returns:
the content type passed by the browser or null if not defined

getFieldName

public String getFieldName()
Returns the name of the field in the multipart form corresponding to this file item.

Specified by:
getFieldName in interface FileItem
Returns:
the name of the form field

setFieldName

public void setFieldName(String fieldName)
Sets the field name used to reference this file item.

Specified by:
setFieldName in interface FileItem
Parameters:
fieldName - the name of the form field

isFormField

public boolean isFormField()
Determines whether or not a FileItem instance represents a simple form field.

Specified by:
isFormField in interface FileItem
Returns:
true if the instance represents a simple form field; false if it represents an uploaded file

setFormField

public void setFormField(boolean isFormField)
Specifies whether or not a FileItem instance represents a simple form field.

Specified by:
setFormField in interface FileItem
Parameters:
isFormField - true if the instance represents a simple form field; false if it represents an uploaded file

getInputStream

public InputStream getInputStream()
                           throws IOException
Returns an InputStream that can be used to retrieve the contents of the file.

Specified by:
getInputStream in interface FileItem
Returns:
an InputStream that can be used to retrieve the contents of the file
Throws:
IOException - if an error occurs

getName

public String getName()
Returns the original filename in the client's filesystem, as provided by the browser (or other client software). In most cases, this will be the base file name, without path information. However, some clients, such as Internet Explorer, do include path information.

You can easily remove the path information with the following snippet:

 String fileName = fileItem.getName();
 if (fileName != null) {
     filename = FilenameUtils.getName(filename);
 } 

Specified by:
getName in interface FileItem
Returns:
the original filename in the client's filesystem.

getOutputStream

public OutputStream getOutputStream()
                             throws IOException
Returns an OutputStream that can be used for storing the contents of the file.

Specified by:
getOutputStream in interface FileItem
Returns:
an OutputStream that can be used for storing the contents of the file
Throws:
IOException - if an error occurs

getSize

public long getSize()
Returns the size of the file item, in bytes.

Specified by:
getSize in interface FileItem
Returns:
the size of the file item, in bytes

getString

public String getString()
Returns the contents of the file as a String, using the default character encoding. This method uses get() to retrieve the contents of the file.

Specified by:
getString in interface FileItem
Returns:
the contents of the file, as a string.

getString

public String getString(String charset)
                 throws UnsupportedEncodingException
Returns the contents of the file as a String, using the specified encoding. This method uses get() to retrieve the contents of the file.

Specified by:
getString in interface FileItem
Parameters:
charset - the charset to use
Returns:
the contents of the file, as a string
Throws:
UnsupportedEncodingException - if the requested character encoding is not available.

isInMemory

public boolean isInMemory()
Provides a hint as to whether or not the file contents will be read from memory. This method always returns true.

Specified by:
isInMemory in interface FileItem
Returns:
true to indicate that the file contents will be read from memory

write

public void write(File file)
           throws Exception
This method does nothing since the file is stored in memory only.

Specified by:
write in interface FileItem
Parameters:
file - the File into which the uploaded item should be stored
Throws:
Exception - if an error occurs

getHeaders

public FileItemHeaders getHeaders()
Returns the file item headers.

Specified by:
getHeaders in interface FileItemHeadersSupport
Returns:
the file items headers

setHeaders

public void setHeaders(FileItemHeaders headers)
Sets the file item headers.

Specified by:
setHeaders in interface FileItemHeadersSupport
Parameters:
headers - the file items headers

getCharSet

public String getCharSet()
Returns the content charset passed by the agent or null if not defined.

Returns:
The content charset passed by the agent or null if not defined