MultipartStream.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 26k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2001-2004 The Apache Software Foundation
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package net.jforum.util.legacy.commons.fileupload;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.io.Serializable;
  22. import java.io.UnsupportedEncodingException;
  23. /**
  24.  * <p> Low level API for processing file uploads.
  25.  *
  26.  * <p> This class can be used to process data streams conforming to MIME
  27.  * 'multipart' format as defined in
  28.  * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Arbitrarily
  29.  * large amounts of data in the stream can be processed under constant
  30.  * memory usage.
  31.  *
  32.  * <p> The format of the stream is defined in the following way:<br>
  33.  *
  34.  * <code>
  35.  *   multipart-body := preamble 1*encapsulation close-delimiter epilogue<br>
  36.  *   encapsulation := delimiter body CRLF<br>
  37.  *   delimiter := "--" boundary CRLF<br>
  38.  *   close-delimiter := "--" boudary "--"<br>
  39.  *   preamble := &lt;ignore&gt;<br>
  40.  *   epilogue := &lt;ignore&gt;<br>
  41.  *   body := header-part CRLF body-part<br>
  42.  *   header-part := 1*header CRLF<br>
  43.  *   header := header-name ":" header-value<br>
  44.  *   header-name := &lt;printable ascii characters except ":"&gt;<br>
  45.  *   header-value := &lt;any ascii characters except CR & LF&gt;<br>
  46.  *   body-data := &lt;arbitrary data&gt;<br>
  47.  * </code>
  48.  *
  49.  * <p>Note that body-data can contain another mulipart entity.  There
  50.  * is limited support for single pass processing of such nested
  51.  * streams.  The nested stream is <strong>required</strong> to have a
  52.  * boundary token of the same length as the parent stream (see {@link
  53.  * #setBoundary(byte[])}).
  54.  *
  55.  * <p>Here is an example of usage of this class.<br>
  56.  *
  57.  * <pre>
  58.  *    try {
  59.  *        MultipartStream multipartStream = new MultipartStream(input,
  60.  *                                                              boundary);
  61.  *        boolean nextPart = multipartStream.skipPreamble();
  62.  *        OutputStream output;
  63.  *        while(nextPart) {
  64.  *            header = chunks.readHeader();
  65.  *            // process headers
  66.  *            // create some output stream
  67.  *            multipartStream.readBodyPart(output);
  68.  *            nextPart = multipartStream.readBoundary();
  69.  *        }
  70.  *    } catch(MultipartStream.MalformedStreamException e) {
  71.  *          // the stream failed to follow required syntax
  72.  *    } catch(IOException) {
  73.  *          // a read or write error occurred
  74.  *    }
  75.  *
  76.  * </pre>
  77.  *
  78.  * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
  79.  * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
  80.  * @author Sean C. Sullivan
  81.  *
  82.  * @version $Id: MultipartStream.java,v 1.4 2007/04/12 02:11:54 rafaelsteil Exp $
  83.  */
  84. public class MultipartStream {
  85.     // ----------------------------------------------------- Manifest constants
  86.     /**
  87.      * The Carriage Return ASCII character value.
  88.      */
  89.     public static final byte CR = 0x0D;
  90.     /**
  91.      * The Line Feed ASCII character value.
  92.      */
  93.     public static final byte LF = 0x0A;
  94.     /**
  95.      * The dash (-) ASCII character value.
  96.      */
  97.     public static final byte DASH = 0x2D;
  98.     /**
  99.      * The maximum length of <code>header-part</code> that will be
  100.      * processed (10 kilobytes = 10240 bytes.).
  101.      */
  102.     public static final int HEADER_PART_SIZE_MAX = 10240;
  103.     /**
  104.      * The default length of the buffer used for processing a request.
  105.      */
  106.     protected static final int DEFAULT_BUFSIZE = 4096;
  107.     /**
  108.      * A byte sequence that marks the end of <code>header-part</code>
  109.      * (<code>CRLFCRLF</code>).
  110.      */
  111.     protected static final byte[] HEADER_SEPARATOR = {
  112.             CR, LF, CR, LF };
  113.     /**
  114.      * A byte sequence that that follows a delimiter that will be
  115.      * followed by an encapsulation (<code>CRLF</code>).
  116.      */
  117.     protected static final byte[] FIELD_SEPARATOR = {
  118.             CR, LF};
  119.     /**
  120.      * A byte sequence that that follows a delimiter of the last
  121.      * encapsulation in the stream (<code>--</code>).
  122.      */
  123.     protected static final byte[] STREAM_TERMINATOR = {
  124.             DASH, DASH};
  125.     // ----------------------------------------------------------- Data members
  126.     /**
  127.      * The input stream from which data is read.
  128.      */
  129.     private InputStream input;
  130.     /**
  131.      * The length of the boundary token plus the leading <code>CRLF--</code>.
  132.      */
  133.     private int boundaryLength;
  134.     /**
  135.      * The amount of data, in bytes, that must be kept in the buffer in order
  136.      * to detect delimiters reliably.
  137.      */
  138.     private int keepRegion;
  139.     /**
  140.      * The byte sequence that partitions the stream.
  141.      */
  142.     private byte[] boundary;
  143.     /**
  144.      * The length of the buffer used for processing the request.
  145.      */
  146.     private int bufSize;
  147.     /**
  148.      * The buffer used for processing the request.
  149.      */
  150.     private byte[] buffer;
  151.     /**
  152.      * The index of first valid character in the buffer.
  153.      * <br>
  154.      * 0 <= head < bufSize
  155.      */
  156.     private int head;
  157.     /**
  158.      * The index of last valid characer in the buffer + 1.
  159.      * <br>
  160.      * 0 <= tail <= bufSize
  161.      */
  162.     private int tail;
  163.     /**
  164.      * The content encoding to use when reading headers.
  165.      */
  166.     private String headerEncoding;
  167.     // ----------------------------------------------------------- Constructors
  168.     /**
  169.      * Default constructor.
  170.      *
  171.      * @see #MultipartStream(InputStream, byte[], int)
  172.      * @see #MultipartStream(InputStream, byte[])
  173.      *
  174.      */
  175.     public MultipartStream() {
  176.     }
  177.     /**
  178.      * <p> Constructs a <code>MultipartStream</code> with a custom size buffer.
  179.      *
  180.      * <p> Note that the buffer must be at least big enough to contain the
  181.      * boundary string, plus 4 characters for CR/LF and double dash, plus at
  182.      * least one byte of data.  Too small a buffer size setting will degrade
  183.      * performance.
  184.      *
  185.      * @param input    The <code>InputStream</code> to serve as a data source.
  186.      * @param boundary The token used for dividing the stream into
  187.      *                 <code>encapsulations</code>.
  188.      * @param bufSize  The size of the buffer to be used, in bytes.
  189.      *
  190.      *
  191.      * @see #MultipartStream()
  192.      * @see #MultipartStream(InputStream, byte[])
  193.      *
  194.      */
  195.     public MultipartStream(InputStream input,
  196.                            byte[] boundary,
  197.                            int bufSize) {
  198.         this.input = input;
  199.         this.bufSize = bufSize;
  200.         this.buffer = new byte[bufSize];
  201.         // We prepend CR/LF to the boundary to chop trailng CR/LF from
  202.         // body-data tokens.
  203.         this.boundary = new byte[boundary.length + 4];
  204.         this.boundaryLength = boundary.length + 4;
  205.         this.keepRegion = boundary.length + 3;
  206.         this.boundary[0] = CR;
  207.         this.boundary[1] = LF;
  208.         this.boundary[2] = DASH;
  209.         this.boundary[3] = DASH;
  210.         System.arraycopy(boundary, 0, this.boundary, 4, boundary.length);
  211.         head = 0;
  212.         tail = 0;
  213.     }
  214.     /**
  215.      * <p> Constructs a <code>MultipartStream</code> with a default size buffer.
  216.      *
  217.      * @param input    The <code>InputStream</code> to serve as a data source.
  218.      * @param boundary The token used for dividing the stream into
  219.      *                 <code>encapsulations</code>.
  220.      *
  221.      * @exception IOException when an error occurs.
  222.      *
  223.      * @see #MultipartStream()
  224.      * @see #MultipartStream(InputStream, byte[], int)
  225.      *
  226.      */
  227.     public MultipartStream(InputStream input,
  228.                            byte[] boundary)  {
  229.         this(input, boundary, DEFAULT_BUFSIZE);
  230.     }
  231.     // --------------------------------------------------------- Public methods
  232.     /**
  233.      * Retrieves the character encoding used when reading the headers of an
  234.      * individual part. When not specified, or <code>null</code>, the platform
  235.      * default encoding is used.
  236.      *
  237.      * @return The encoding used to read part headers.
  238.      */
  239.     public String getHeaderEncoding() {
  240.         return headerEncoding;
  241.     }
  242.     /**
  243.      * Specifies the character encoding to be used when reading the headers of
  244.      * individual parts. When not specified, or <code>null</code>, the platform
  245.      * default encoding is used.
  246.      *
  247.      * @param encoding The encoding used to read part headers.
  248.      */
  249.     public void setHeaderEncoding(String encoding) {
  250.         headerEncoding = encoding;
  251.     }
  252.     /**
  253.      * Reads a byte from the <code>buffer</code>, and refills it as
  254.      * necessary.
  255.      *
  256.      * @return The next byte from the input stream.
  257.      *
  258.      * @exception IOException if there is no more data available.
  259.      */
  260.     public byte readByte()
  261.         throws IOException {
  262.         // Buffer depleted ?
  263.         if (head == tail) {
  264.             head = 0;
  265.             // Refill.
  266.             tail = input.read(buffer, head, bufSize);
  267.             if (tail == -1) {
  268.                 // No more data available.
  269.                 throw new IOException("No more data is available");
  270.             }
  271.         }
  272.         return buffer[head++];
  273.     }
  274.     /**
  275.      * Skips a <code>boundary</code> token, and checks whether more
  276.      * <code>encapsulations</code> are contained in the stream.
  277.      *
  278.      * @return <code>true</code> if there are more encapsulations in
  279.      *         this stream; <code>false</code> otherwise.
  280.      *
  281.      * @exception MalformedStreamException if the stream ends unexpecetedly or
  282.      *                                     fails to follow required syntax.
  283.      */
  284.     public boolean readBoundary()
  285.         throws MalformedStreamException {
  286.         byte[] marker = new byte[2];
  287.         boolean nextChunk = false;
  288.         head += boundaryLength;
  289.         try {
  290.             marker[0] = readByte();
  291.             if (marker[0] == LF) {
  292.                 // Work around IE5 Mac bug with input type=image.
  293.                 // Because the boundary delimiter, not including the trailing
  294.                 // CRLF, must not appear within any file (RFC 2046, section
  295.                 // 5.1.1), we know the missing CR is due to a buggy browser
  296.                 // rather than a file containing something similar to a
  297.                 // boundary.
  298.                 return true;
  299.             }
  300.             marker[1] = readByte();
  301.             if (arrayequals(marker, STREAM_TERMINATOR, 2)) {
  302.                 nextChunk = false;
  303.             } else if (arrayequals(marker, FIELD_SEPARATOR, 2)) {
  304.                 nextChunk = true;
  305.             } else {
  306.                 throw new MalformedStreamException(
  307.                         "Unexpected characters follow a boundary");
  308.             }
  309.         } catch (IOException e) {
  310.             throw new MalformedStreamException("Stream ended unexpectedly");
  311.         }
  312.         return nextChunk;
  313.     }
  314.     /**
  315.      * <p>Changes the boundary token used for partitioning the stream.
  316.      *
  317.      * <p>This method allows single pass processing of nested multipart
  318.      * streams.
  319.      *
  320.      * <p>The boundary token of the nested stream is <code>required</code>
  321.      * to be of the same length as the boundary token in parent stream.
  322.      *
  323.      * <p>Restoring the parent stream boundary token after processing of a
  324.      * nested stream is left to the application.
  325.      *
  326.      * @param boundary The boundary to be used for parsing of the nested
  327.      *                 stream.
  328.      *
  329.      * @exception IllegalBoundaryException if the <code>boundary</code>
  330.      *                                     has a different length than the one
  331.      *                                     being currently parsed.
  332.      */
  333.     public void setBoundary(byte[] boundary)
  334.         throws IllegalBoundaryException {
  335.         if (boundary.length != boundaryLength - 4) {
  336.             throw new IllegalBoundaryException(
  337.                     "The length of a boundary token can not be changed");
  338.         }
  339.         System.arraycopy(boundary, 0, this.boundary, 4, boundary.length);
  340.     }
  341.     /**
  342.      * <p>Reads the <code>header-part</code> of the current
  343.      * <code>encapsulation</code>.
  344.      *
  345.      * <p>Headers are returned verbatim to the input stream, including the
  346.      * trailing <code>CRLF</code> marker. Parsing is left to the
  347.      * application.
  348.      *
  349.      * <p><strong>TODO</strong> allow limiting maximum header size to
  350.      * protect against abuse.
  351.      *
  352.      * @return The <code>header-part</code> of the current encapsulation.
  353.      *
  354.      * @exception MalformedStreamException if the stream ends unexpecetedly.
  355.      */
  356.     public String readHeaders()
  357.         throws MalformedStreamException {
  358.         int i = 0;
  359.         byte[] b = new byte[1];
  360.         // to support multi-byte characters
  361.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  362.         int sizeMax = HEADER_PART_SIZE_MAX;
  363.         int size = 0;
  364.         while (i < 4) {
  365.             try {
  366.                 b[0] = readByte();
  367.             } catch (IOException e) {
  368.                 throw new MalformedStreamException("Stream ended unexpectedly");
  369.             }
  370.             size++;
  371.             if (b[0] == HEADER_SEPARATOR[i]) {
  372.                 i++;
  373.             } else {
  374.                 i = 0;
  375.             }
  376.             if (size <= sizeMax) {
  377.                 baos.write(b[0]);
  378.             }
  379.         }
  380.         String headers = null;
  381.         if (headerEncoding != null) {
  382.             try {
  383.                 headers = baos.toString(headerEncoding);
  384.             } catch (UnsupportedEncodingException e) {
  385.                 // Fall back to platform default if specified encoding is not
  386.                 // supported.
  387.                 headers = baos.toString();
  388.             }
  389.         } else {
  390.             headers = baos.toString();
  391.         }
  392.         return headers;
  393.     }
  394.     /**
  395.      * <p>Reads <code>body-data</code> from the current
  396.      * <code>encapsulation</code> and writes its contents into the
  397.      * output <code>Stream</code>.
  398.      *
  399.      * <p>Arbitrary large amounts of data can be processed by this
  400.      * method using a constant size buffer. (see {@link
  401.      * #MultipartStream(InputStream,byte[],int) constructor}).
  402.      *
  403.      * @param output The <code>Stream</code> to write data into.
  404.      *
  405.      * @return the amount of data written.
  406.      *
  407.      * @exception MalformedStreamException if the stream ends unexpectedly.
  408.      * @exception IOException              if an i/o error occurs.
  409.      */
  410.     public int readBodyData(OutputStream output)
  411.         throws MalformedStreamException,
  412.                IOException {
  413.         boolean done = false;
  414.         int pad;
  415.         int pos;
  416.         int bytesRead;
  417.         int total = 0;
  418.         while (!done) {
  419.             // Is boundary token present somewere in the buffer?
  420.             pos = findSeparator();
  421.             if (pos != -1) {
  422.                 // Write the rest of the data before the boundary.
  423.                 output.write(buffer, head, pos - head);
  424.                 total += pos - head;
  425.                 head = pos;
  426.                 done = true;
  427.             } else {
  428.                 // Determine how much data should be kept in the
  429.                 // buffer.
  430.                 if (tail - head > keepRegion) {
  431.                     pad = keepRegion;
  432.                 } else {
  433.                     pad = tail - head;
  434.                 }
  435.                 // Write out the data belonging to the body-data.
  436.                 output.write(buffer, head, tail - head - pad);
  437.                 // Move the data to the beginning of the buffer.
  438.                 total += tail - head - pad;
  439.                 System.arraycopy(buffer, tail - pad, buffer, 0, pad);
  440.                 // Refill buffer with new data.
  441.                 head = 0;
  442.                 bytesRead = input.read(buffer, pad, bufSize - pad);
  443.                 // [pprrrrrrr]
  444.                 if (bytesRead != -1) {
  445.                     tail = pad + bytesRead;
  446.                 } else {
  447.                     // The last pad amount is left in the buffer.
  448.                     // Boundary can't be in there so write out the
  449.                     // data you have and signal an error condition.
  450.                     output.write(buffer, 0, pad);
  451.                     output.flush();
  452.                     total += pad;
  453.                     throw new MalformedStreamException(
  454.                             "Stream ended unexpectedly");
  455.                 }
  456.             }
  457.         }
  458.         output.flush();
  459.         return total;
  460.     }
  461.     /**
  462.      * <p> Reads <code>body-data</code> from the current
  463.      * <code>encapsulation</code> and discards it.
  464.      *
  465.      * <p>Use this method to skip encapsulations you don't need or don't
  466.      * understand.
  467.      *
  468.      * @return The amount of data discarded.
  469.      *
  470.      * @exception MalformedStreamException if the stream ends unexpectedly.
  471.      * @exception IOException              if an i/o error occurs.
  472.      */
  473.     public int discardBodyData()
  474.         throws MalformedStreamException,
  475.                IOException {
  476.         boolean done = false;
  477.         int pad;
  478.         int pos;
  479.         int bytesRead;
  480.         int total = 0;
  481.         while (!done) {
  482.             // Is boundary token present somewere in the buffer?
  483.             pos = findSeparator();
  484.             if (pos != -1) {
  485.                 // Write the rest of the data before the boundary.
  486.                 total += pos - head;
  487.                 head = pos;
  488.                 done = true;
  489.             } else {
  490.                 // Determine how much data should be kept in the
  491.                 // buffer.
  492.                 if (tail - head > keepRegion) {
  493.                     pad = keepRegion;
  494.                 } else {
  495.                     pad = tail - head;
  496.                 }
  497.                 total += tail - head - pad;
  498.                 // Move the data to the beginning of the buffer.
  499.                 System.arraycopy(buffer, tail - pad, buffer, 0, pad);
  500.                 // Refill buffer with new data.
  501.                 head = 0;
  502.                 bytesRead = input.read(buffer, pad, bufSize - pad);
  503.                 // [pprrrrrrr]
  504.                 if (bytesRead != -1) {
  505.                     tail = pad + bytesRead;
  506.                 } else {
  507.                     // The last pad amount is left in the buffer.
  508.                     // Boundary can't be in there so signal an error
  509.                     // condition.
  510.                     total += pad;
  511.                     throw new MalformedStreamException(
  512.                             "Stream ended unexpectedly");
  513.                 }
  514.             }
  515.         }
  516.         return total;
  517.     }
  518.     /**
  519.      * Finds the beginning of the first <code>encapsulation</code>.
  520.      *
  521.      * @return <code>true</code> if an <code>encapsulation</code> was found in
  522.      *         the stream.
  523.      *
  524.      * @exception IOException if an i/o error occurs.
  525.      */
  526.     public boolean skipPreamble()
  527.         throws IOException {
  528.         // First delimiter may be not preceeded with a CRLF.
  529.         System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2);
  530.         boundaryLength = boundary.length - 2;
  531.         try {
  532.             // Discard all data up to the delimiter.
  533.             discardBodyData();
  534.             // Read boundary - if succeded, the stream contains an
  535.             // encapsulation.
  536.             return readBoundary();
  537.         } catch (MalformedStreamException e) {
  538.             return false;
  539.         } finally {
  540.             // Restore delimiter.
  541.             System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2);
  542.             boundaryLength = boundary.length;
  543.             boundary[0] = CR;
  544.             boundary[1] = LF;
  545.         }
  546.     }
  547.     /**
  548.      * Compares <code>count</code> first bytes in the arrays
  549.      * <code>a</code> and <code>b</code>.
  550.      *
  551.      * @param a     The first array to compare.
  552.      * @param b     The second array to compare.
  553.      * @param count How many bytes should be compared.
  554.      *
  555.      * @return <code>true</code> if <code>count</code> first bytes in arrays
  556.      *         <code>a</code> and <code>b</code> are equal.
  557.      */
  558.     public static boolean arrayequals(byte[] a,
  559.                                       byte[] b,
  560.                                       int count) {
  561.         for (int i = 0; i < count; i++) {
  562.             if (a[i] != b[i]) {
  563.                 return false;
  564.             }
  565.         }
  566.         return true;
  567.     }
  568.     /**
  569.      * Searches for a byte of specified value in the <code>buffer</code>,
  570.      * starting at the specified <code>position</code>.
  571.      *
  572.      * @param value The value to find.
  573.      * @param pos   The starting position for searching.
  574.      *
  575.      * @return The position of byte found, counting from beginning of the
  576.      *         <code>buffer</code>, or <code>-1</code> if not found.
  577.      */
  578.     protected int findByte(byte value,
  579.                            int pos) {
  580.         for (int i = pos; i < tail; i++) {
  581.             if (buffer[i] == value) {
  582.                 return i;
  583.             }
  584.         }
  585.         return -1;
  586.     }
  587.     /**
  588.      * Searches for the <code>boundary</code> in the <code>buffer</code>
  589.      * region delimited by <code>head</code> and <code>tail</code>.
  590.      *
  591.      * @return The position of the boundary found, counting from the
  592.      *         beginning of the <code>buffer</code>, or <code>-1</code> if
  593.      *         not found.
  594.      */
  595.     protected int findSeparator() {
  596.         int first;
  597.         int match = 0;
  598.         int maxpos = tail - boundaryLength;
  599.         for (first = head;
  600.              (first <= maxpos) && (match != boundaryLength);
  601.              first++) {
  602.             first = findByte(boundary[0], first);
  603.             if (first == -1 || (first > maxpos)) {
  604.                 return -1;
  605.             }
  606.             for (match = 1; match < boundaryLength; match++) {
  607.                 if (buffer[first + match] != boundary[match]) {
  608.                     break;
  609.                 }
  610.             }
  611.         }
  612.         if (match == boundaryLength) {
  613.             return first - 1;
  614.         }
  615.         return -1;
  616.     }
  617.     /**
  618.      * Returns a string representation of this object.
  619.      *
  620.      * @return The string representation of this object.
  621.      */
  622.     public String toString() {
  623.         StringBuffer sbTemp = new StringBuffer();
  624.         sbTemp.append("boundary='");
  625.         sbTemp.append(String.valueOf(boundary));
  626.         sbTemp.append("'nbufSize=");
  627.         sbTemp.append(bufSize);
  628.         return sbTemp.toString();
  629.     }
  630.     /**
  631.      * Thrown to indicate that the input stream fails to follow the
  632.      * required syntax.
  633.      */
  634.     public static class MalformedStreamException
  635.         extends IOException implements Serializable {
  636.         /**
  637.          * Constructs a <code>MalformedStreamException</code> with no
  638.          * detail message.
  639.          */
  640.         public MalformedStreamException() {
  641.             super();
  642.         }
  643.         /**
  644.          * Constructs an <code>MalformedStreamException</code> with
  645.          * the specified detail message.
  646.          *
  647.          * @param message The detail message.
  648.          */
  649.         public MalformedStreamException(String message) {
  650.             super(message);
  651.         }
  652.     }
  653.     /**
  654.      * Thrown upon attempt of setting an invalid boundary token.
  655.      */
  656.     public static class IllegalBoundaryException
  657.         extends IOException implements Serializable {
  658.         /**
  659.          * Constructs an <code>IllegalBoundaryException</code> with no
  660.          * detail message.
  661.          */
  662.         public IllegalBoundaryException() {
  663.             super();
  664.         }
  665.         /**
  666.          * Constructs an <code>IllegalBoundaryException</code> with
  667.          * the specified detail message.
  668.          *
  669.          * @param message The detail message.
  670.          */
  671.         public IllegalBoundaryException(String message) {
  672.             super(message);
  673.         }
  674.     }
  675.     // ------------------------------------------------------ Debugging methods
  676.     // These are the methods that were used to debug this stuff.
  677.     /*
  678.     // Dump data.
  679.     protected void dump()
  680.     {
  681.         System.out.println("01234567890");
  682.         byte[] temp = new byte[buffer.length];
  683.         for(int i=0; i<buffer.length; i++)
  684.         {
  685.             if (buffer[i] == 0x0D || buffer[i] == 0x0A)
  686.             {
  687.                 temp[i] = 0x21;
  688.             }
  689.             else
  690.             {
  691.                 temp[i] = buffer[i];
  692.             }
  693.         }
  694.         System.out.println(new String(temp));
  695.         int i;
  696.         for (i=0; i<head; i++)
  697.             System.out.print(" ");
  698.         System.out.println("h");
  699.         for (i=0; i<tail; i++)
  700.             System.out.print(" ");
  701.         System.out.println("t");
  702.         System.out.flush();
  703.     }
  704.     // Main routine, for testing purposes only.
  705.     //
  706.     // @param args A String[] with the command line arguments.
  707.     // @exception Exception, a generic exception.
  708.     public static void main( String[] args )
  709.         throws Exception
  710.     {
  711.         File boundaryFile = new File("boundary.dat");
  712.         int boundarySize = (int)boundaryFile.length();
  713.         byte[] boundary = new byte[boundarySize];
  714.         FileInputStream input = new FileInputStream(boundaryFile);
  715.         input.read(boundary,0,boundarySize);
  716.         input = new FileInputStream("multipart.dat");
  717.         MultipartStream chunks = new MultipartStream(input, boundary);
  718.         int i = 0;
  719.         String header;
  720.         OutputStream output;
  721.         boolean nextChunk = chunks.skipPreamble();
  722.         while (nextChunk)
  723.         {
  724.             header = chunks.readHeaders();
  725.             System.out.println("!"+header+"!");
  726.             System.out.println("wrote part"+i+".dat");
  727.             output = new FileOutputStream("part"+(i++)+".dat");
  728.             chunks.readBodyData(output);
  729.             nextChunk = chunks.readBoundary();
  730.         }
  731.     }
  732.     */
  733. }