1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
60
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
90
91
92
93 public void startPart(String contentType)
94 throws IOException
95 {
96 if (inPart)
97 out.write(__CRLF);
98 out.write(__DASHDASH);
99 out.write(boundary);
100 out.write(__CRLF);
101 out.write("Content-Type: ");
102 out.write(contentType);
103 out.write(__CRLF);
104 out.write(__CRLF);
105 inPart=true;
106 }
107
108
109
110
111
112 public void endPart()
113 throws IOException
114 {
115 if (inPart)
116 out.write(__CRLF);
117 inPart=false;
118 }
119
120
121
122
123
124
125
126 public void startPart(String contentType, String[] headers)
127 throws IOException
128 {
129 if (inPart)
130 out.write(__CRLF);
131 out.write(__DASHDASH);
132 out.write(boundary);
133 out.write(__CRLF);
134 out.write("Content-Type: ");
135 out.write(contentType);
136 out.write(__CRLF);
137 for (int i=0;headers!=null && i<headers.length;i++)
138 {
139 out.write(headers[i]);
140 out.write(__CRLF);
141 }
142 out.write(__CRLF);
143 inPart=true;
144 }
145
146 }
147
148
149
150