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