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 public void close()
72 throws IOException
73 {
74 if (inPart)
75 out.write(__CRLF);
76 out.write(__DASHDASH);
77 out.write(boundaryBytes);
78 out.write(__DASHDASH);
79 out.write(__CRLF);
80 inPart=false;
81 super.close();
82 }
83
84
85 public String getBoundary()
86 {
87 return boundary;
88 }
89
90 public OutputStream getOut() {return out;}
91
92
93
94
95 public void startPart(String contentType)
96 throws IOException
97 {
98 if (inPart)
99 out.write(__CRLF);
100 inPart=true;
101 out.write(__DASHDASH);
102 out.write(boundaryBytes);
103 out.write(__CRLF);
104 out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
105 out.write(__CRLF);
106 out.write(__CRLF);
107 }
108
109
110
111
112 public void startPart(String contentType, String[] headers)
113 throws IOException
114 {
115 if (inPart)
116 out.write(__CRLF);
117 inPart=true;
118 out.write(__DASHDASH);
119 out.write(boundaryBytes);
120 out.write(__CRLF);
121 out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
122 out.write(__CRLF);
123 for (int i=0;headers!=null && i<headers.length;i++)
124 {
125 out.write(headers[i].getBytes(StringUtil.__ISO_8859_1));
126 out.write(__CRLF);
127 }
128 out.write(__CRLF);
129 }
130
131 }
132
133
134
135