1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.util;
15
16 import java.io.FilterWriter;
17 import java.io.IOException;
18 import java.io.Writer;
19
20
21
22
23
24
25
26
27 public class MultiPartWriter extends FilterWriter
28 {
29
30 private final static String __CRLF="\015\012";
31 private final static String __DASHDASH="--";
32
33 public static String MULTIPART_MIXED=MultiPartOutputStream.MULTIPART_MIXED;
34 public static String MULTIPART_X_MIXED_REPLACE=MultiPartOutputStream.MULTIPART_X_MIXED_REPLACE;
35
36
37 private String boundary;
38
39
40 private boolean inPart=false;
41
42
43 public MultiPartWriter(Writer out)
44 throws IOException
45 {
46 super(out);
47 boundary = "jetty"+System.identityHashCode(this)+
48 Long.toString(System.currentTimeMillis(),36);
49
50 inPart=false;
51 }
52
53
54
55
56
57 public void close()
58 throws IOException
59 {
60 if (inPart)
61 out.write(__CRLF);
62 out.write(__DASHDASH);
63 out.write(boundary);
64 out.write(__DASHDASH);
65 out.write(__CRLF);
66 inPart=false;
67 super.close();
68 }
69
70
71 public String getBoundary()
72 {
73 return boundary;
74 }
75
76
77
78
79 public void startPart(String contentType)
80 throws IOException
81 {
82 if (inPart)
83 out.write(__CRLF);
84 out.write(__DASHDASH);
85 out.write(boundary);
86 out.write(__CRLF);
87 out.write("Content-Type: ");
88 out.write(contentType);
89 out.write(__CRLF);
90 out.write(__CRLF);
91 inPart=true;
92 }
93
94
95
96
97 public void endPart()
98 throws IOException
99 {
100 if (inPart)
101 out.write(__CRLF);
102 inPart=false;
103 }
104
105
106
107
108 public void startPart(String contentType, String[] headers)
109 throws IOException
110 {
111 if (inPart)
112 out.write(__CRLF);
113 out.write(__DASHDASH);
114 out.write(boundary);
115 out.write(__CRLF);
116 out.write("Content-Type: ");
117 out.write(contentType);
118 out.write(__CRLF);
119 for (int i=0;headers!=null && i<headers.length;i++)
120 {
121 out.write(headers[i]);
122 out.write(__CRLF);
123 }
124 out.write(__CRLF);
125 inPart=true;
126 }
127
128 }
129
130
131
132