comparison core/src/luan/modules/url/MultiPartOutputStream.java @ 725:a741a3a33423

add url support for multipart/form-data
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 08 Jun 2016 23:13:10 -0600
parents
children
comparison
equal deleted inserted replaced
724:4f8e30a3ffd0 725:a741a3a33423
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18 // This horrible broken code from jetty is just here for me to look at. It isn't used. -fschmidt
19
20 package luan.modules.url;
21
22 import java.io.FilterOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26
27 /* ================================================================ */
28 /** Handle a multipart MIME response.
29 *
30 *
31 *
32 */
33 public class MultiPartOutputStream extends FilterOutputStream
34 {
35 /* ------------------------------------------------------------ */
36 private static final byte[] __CRLF={'\r','\n'};
37 private static final byte[] __DASHDASH={'-','-'};
38
39 public static String MULTIPART_MIXED="multipart/mixed";
40 public static String MULTIPART_X_MIXED_REPLACE="multipart/x-mixed-replace";
41 public static final String __ISO_8859_1="ISO-8859-1";
42
43 public static String newBoundary(Object obj) {
44 return "jetty"+System.identityHashCode(obj)+
45 Long.toString(System.currentTimeMillis(),36);
46 }
47
48 /* ------------------------------------------------------------ */
49 private final String boundary;
50 private final byte[] boundaryBytes;
51
52 /* ------------------------------------------------------------ */
53 private boolean inPart=false;
54
55 /* ------------------------------------------------------------ */
56 public MultiPartOutputStream(OutputStream out,String boundary)
57 throws IOException
58 {
59 super(out);
60
61 this.boundary = boundary;
62 boundaryBytes=boundary.getBytes(__ISO_8859_1);
63
64 inPart=false;
65 }
66
67
68
69 /* ------------------------------------------------------------ */
70 /** End the current part.
71 * @exception IOException IOException
72 */
73 @Override
74 public void close()
75 throws IOException
76 {
77 if (inPart)
78 out.write(__CRLF);
79 out.write(__DASHDASH);
80 out.write(boundaryBytes);
81 out.write(__DASHDASH);
82 out.write(__CRLF);
83 inPart=false;
84 super.close();
85 }
86
87 /* ------------------------------------------------------------ */
88 public String getBoundary()
89 {
90 return boundary;
91 }
92
93 public OutputStream getOut() {return out;}
94
95 /* ------------------------------------------------------------ */
96 /** Start creation of the next Content.
97 */
98 public void startPart(String contentType)
99 throws IOException
100 {
101 if (inPart)
102 out.write(__CRLF);
103 inPart=true;
104 out.write(__DASHDASH);
105 out.write(boundaryBytes);
106 out.write(__CRLF);
107 if (contentType != null)
108 out.write(("Content-Type: "+contentType).getBytes(__ISO_8859_1));
109 out.write(__CRLF);
110 out.write(__CRLF);
111 }
112
113 /* ------------------------------------------------------------ */
114 /** Start creation of the next Content.
115 */
116 public void startPart(String contentType, String[] headers)
117 throws IOException
118 {
119 if (inPart)
120 out.write(__CRLF);
121 inPart=true;
122 out.write(__DASHDASH);
123 out.write(boundaryBytes);
124 out.write(__CRLF);
125 if (contentType != null)
126 out.write(("Content-Type: "+contentType).getBytes(__ISO_8859_1));
127 out.write(__CRLF);
128 for (int i=0;headers!=null && i<headers.length;i++)
129 {
130 out.write(headers[i].getBytes(__ISO_8859_1));
131 out.write(__CRLF);
132 }
133 out.write(__CRLF);
134 }
135
136 /* ------------------------------------------------------------ */
137 @Override
138 public void write(byte[] b, int off, int len) throws IOException
139 {
140 out.write(b,off,len);
141 }
142 }
143
144
145
146