]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/MultiPartWriter.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / util / MultiPartWriter.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.util;
20
21 import java.io.FilterWriter;
22 import java.io.IOException;
23 import java.io.Writer;
24
25
26 /* ================================================================ */
27 /** Handle a multipart MIME response.
28  *
29  * 
30  * 
31 */
32 public class MultiPartWriter extends FilterWriter
33 {
34     /* ------------------------------------------------------------ */
35     private final static String __CRLF="\015\012";
36     private final static String __DASHDASH="--";
37     
38     public static final String MULTIPART_MIXED=MultiPartOutputStream.MULTIPART_MIXED;
39     public static final String MULTIPART_X_MIXED_REPLACE=MultiPartOutputStream.MULTIPART_X_MIXED_REPLACE;
40     
41     /* ------------------------------------------------------------ */
42     private String boundary;
43
44     /* ------------------------------------------------------------ */
45     private boolean inPart=false;    
46     
47     /* ------------------------------------------------------------ */
48     public MultiPartWriter(Writer out)
49          throws IOException
50     {
51         super(out);
52         boundary = "jetty"+System.identityHashCode(this)+
53         Long.toString(System.currentTimeMillis(),36);
54         
55         inPart=false;
56     }
57
58     /* ------------------------------------------------------------ */
59     /** End the current part.
60      * @exception IOException IOException
61      */
62     @Override
63     public void close()
64          throws IOException
65     {
66         try
67         {
68             if (inPart)
69                 out.write(__CRLF);
70             out.write(__DASHDASH);
71             out.write(boundary);
72             out.write(__DASHDASH);
73             out.write(__CRLF);
74             inPart=false;
75         }
76         finally
77         {
78             super.close();
79         }
80     }
81     
82     /* ------------------------------------------------------------ */
83     public String getBoundary()
84     {
85         return boundary;
86     }
87     
88     /* ------------------------------------------------------------ */
89     /** Start creation of the next Content.
90      */
91     public void startPart(String contentType)
92          throws IOException
93     {
94         if (inPart)
95             out.write(__CRLF);
96         out.write(__DASHDASH);
97         out.write(boundary);
98         out.write(__CRLF);
99         out.write("Content-Type: ");
100         out.write(contentType);
101         out.write(__CRLF);
102         out.write(__CRLF);
103         inPart=true;
104     }
105     
106     /* ------------------------------------------------------------ */
107     /** end creation of the next Content.
108      */
109     public void endPart()
110          throws IOException
111     {
112         if (inPart)
113             out.write(__CRLF);
114         inPart=false;
115     }
116         
117     /* ------------------------------------------------------------ */
118     /** Start creation of the next Content.
119      */
120     public void startPart(String contentType, String[] headers)
121          throws IOException
122     {
123         if (inPart)
124             out.write(__CRLF);
125         out.write(__DASHDASH);
126         out.write(boundary);
127         out.write(__CRLF);
128         out.write("Content-Type: ");
129         out.write(contentType);
130         out.write(__CRLF);
131         for (int i=0;headers!=null && i<headers.length;i++)
132         {
133             out.write(headers[i]);
134             out.write(__CRLF);
135         }
136         out.write(__CRLF);
137         inPart=true;
138     }
139     
140 }
141
142
143
144