View Javadoc

1   // ========================================================================
2   // Copyright (c) 1996-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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  /** Handle a multipart MIME response.
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      /** End the current part.
69       * @exception IOException IOException
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      /** Start creation of the next Content.
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     /** Start creation of the next Content.
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