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 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
108
109 public void endPart()
110 throws IOException
111 {
112 if (inPart)
113 out.write(__CRLF);
114 inPart=false;
115 }
116
117
118
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