Class MultipartFormData

java.lang.Object
org.tinystruct.http.servlet.MultipartFormData

public class MultipartFormData extends Object
The MultipartFormData class is responsible for reading the input data of a multipart request and splitting it up into input elements, wrapped inside a ContentDisposition for easy definition. To use this class, create a new instance of MultipartFormData passing it a HttpServletRequest in the constructor. Then use the getNextPart method until it returns null, then you're finished. Example:
      MultipartFormData iterator = new MultipartFormData(request);
      ContentDisposition element;

      while ((element = iterator.getNextPart()) != null) {
           //do something with element
      }
 
See Also:
  • ContentDisposition
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected String
    The boundary for this multipart request
    protected int
    The amount of data read from a request at a time.
    protected boolean
    Whether the input stream is finished
    protected jakarta.servlet.ServletInputStream
    The input stream instance for this class
    protected final org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest,jakarta.servlet.ServletInputStream>
    The request instance for this class
  • Constructor Summary

    Constructors
    Constructor
    Description
    MultipartFormData(org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest,jakarta.servlet.ServletInputStream> request)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Get the maximum amount of bytes read from a line at one time
    org.tinystruct.transfer.http.upload.ContentDisposition
    Retrieves the next element in the iterator if one exists.
    static String
    parseBoundary(String contentType)
    Parses a content-type String for the boundary.
    static String
    parseContentType(String contentTypeString)
    Parses the "Content-Type" line of a multipart form for a content type
    static String
    parseDispositionFilename(String dispositionString)
    Retrieves the "filename" attribute from a content disposition line
    static String
    parseDispositionName(String dispositionString)
    Retrieves the "name" attribute from a content disposition line
    static String
    parseForAttribute(String attribute, String parseString)
    Parses a string looking for an attribute-value pair, and returns the value.
    protected void
    Handles retrieving the boundary and setting the input stream
    protected String
    Reads the input stream until it reaches a new line
    void
    setBufferSize(int bufferSize)
    Set the maximum amount of bytes read from a line at one time

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • request

      protected final org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest,jakarta.servlet.ServletInputStream> request
      The request instance for this class
    • inputStream

      protected jakarta.servlet.ServletInputStream inputStream
      The input stream instance for this class
    • boundary

      protected String boundary
      The boundary for this multipart request
    • end

      protected boolean end
      Whether the input stream is finished
    • bufferSize

      protected int bufferSize
      The amount of data read from a request at a time. This also represents the maximum size in bytes of a line read from the request which defaults to 4 * 1024 (4 KB)
  • Constructor Details

    • MultipartFormData

      public MultipartFormData(org.tinystruct.http.Request<jakarta.servlet.http.HttpServletRequest,jakarta.servlet.ServletInputStream> request) throws jakarta.servlet.ServletException
      Throws:
      jakarta.servlet.ServletException
  • Method Details

    • parseBoundary

      public static String parseBoundary(String contentType)
      Parses a content-type String for the boundary. Appends a "--" to the beginning of the boundary, because that's the real boundary as opposed to the shortened one in the content type.
      Parameters:
      contentType - content type
      Returns:
      boundary
    • parseContentType

      public static String parseContentType(String contentTypeString)
      Parses the "Content-Type" line of a multipart form for a content type
      Parameters:
      contentTypeString - A String representing the Content-Type line, with a trailing "\n"
      Returns:
      The content type specified, or null if one can't be found.
    • parseDispositionName

      public static String parseDispositionName(String dispositionString)
      Retrieves the "name" attribute from a content disposition line
      Parameters:
      dispositionString - The entire "Content-disposition" string
      Returns:
      null if no name could be found, otherwise, returns the name
      See Also:
    • parseDispositionFilename

      public static String parseDispositionFilename(String dispositionString)
      Retrieves the "filename" attribute from a content disposition line
      Parameters:
      dispositionString - The entire "Content-disposition" string
      Returns:
      null if no filename could be found, otherwise, returns the filename
      See Also:
    • parseForAttribute

      public static String parseForAttribute(String attribute, String parseString)
      Parses a string looking for an attribute-value pair, and returns the value. For example:
            String parseString = "Content-Disposition: filename=\"bob\" name=\"jack\"";
            MultipartFormData.parseForAttribute(parseString, "name");
       
      That will return "bob".
      Parameters:
      attribute - The name of the attribute you're trying to get
      parseString - The string to retrieve the value from
      Returns:
      The value of the attribute, or null if none could be found
    • getNextPart

      public org.tinystruct.transfer.http.upload.ContentDisposition getNextPart()
      Retrieves the next element in the iterator if one exists.
      Returns:
      a ContentDisposition representing the next element in the request data
    • getBufferSize

      public int getBufferSize()
      Get the maximum amount of bytes read from a line at one time
      Returns:
      buffer size
      See Also:
      • ServletInputStream.readLine(byte[], int, int)
    • setBufferSize

      public void setBufferSize(int bufferSize)
      Set the maximum amount of bytes read from a line at one time
      Parameters:
      bufferSize - buffer size
      See Also:
      • ServletInputStream.readLine(byte[], int, int)
    • parseRequest

      protected void parseRequest() throws jakarta.servlet.ServletException
      Handles retrieving the boundary and setting the input stream
      Throws:
      jakarta.servlet.ServletException - servlet exception
    • readLine

      protected String readLine()
      Reads the input stream until it reaches a new line
      Returns:
      one line