View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
19  package org.eclipse.jetty.util;
20  
21  import java.io.FilterOutputStream;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  
25  
26  /* ================================================================ */
27  /** Handle a multipart MIME response.
28   *
29   * 
30   * 
31  */
32  public class MultiPartOutputStream extends FilterOutputStream
33  {
34      /* ------------------------------------------------------------ */
35      private static final byte[] __CRLF={'\r','\n'};
36      private static final byte[] __DASHDASH={'-','-'};
37      
38      public static String MULTIPART_MIXED="multipart/mixed";
39      public static String MULTIPART_X_MIXED_REPLACE="multipart/x-mixed-replace";
40      
41      /* ------------------------------------------------------------ */
42      private String boundary;
43      private byte[] boundaryBytes;
44  
45      /* ------------------------------------------------------------ */
46      private boolean inPart=false;    
47      
48      /* ------------------------------------------------------------ */
49      public MultiPartOutputStream(OutputStream out)
50      throws IOException
51      {
52          super(out);
53  
54          boundary = "jetty"+System.identityHashCode(this)+
55          Long.toString(System.currentTimeMillis(),36);
56          boundaryBytes=boundary.getBytes(StringUtil.__ISO_8859_1);
57  
58          inPart=false;
59      }
60  
61      
62  
63      /* ------------------------------------------------------------ */
64      /** End the current part.
65       * @exception IOException IOException
66       */
67      @Override
68      public void close()
69           throws IOException
70      {
71          if (inPart)
72              out.write(__CRLF);
73          out.write(__DASHDASH);
74          out.write(boundaryBytes);
75          out.write(__DASHDASH);
76          out.write(__CRLF);
77          inPart=false;
78          super.close();
79      }
80      
81      /* ------------------------------------------------------------ */
82      public String getBoundary()
83      {
84          return boundary;
85      }
86  
87      public OutputStream getOut() {return out;}
88      
89      /* ------------------------------------------------------------ */
90      /** Start creation of the next Content.
91       */
92      public void startPart(String contentType)
93           throws IOException
94      {
95          if (inPart)
96              out.write(__CRLF);
97          inPart=true;
98          out.write(__DASHDASH);
99          out.write(boundaryBytes);
100         out.write(__CRLF);
101         if (contentType != null)
102             out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
103         out.write(__CRLF);
104         out.write(__CRLF);
105     }
106         
107     /* ------------------------------------------------------------ */
108     /** Start creation of the next Content.
109      */
110     public void startPart(String contentType, String[] headers)
111          throws IOException
112     {
113         if (inPart)
114             out.write(__CRLF);
115         inPart=true;
116         out.write(__DASHDASH);
117         out.write(boundaryBytes);
118         out.write(__CRLF);
119         if (contentType != null)
120             out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1));
121         out.write(__CRLF);
122         for (int i=0;headers!=null && i<headers.length;i++)
123         {
124             out.write(headers[i].getBytes(StringUtil.__ISO_8859_1));
125             out.write(__CRLF);
126         }
127         out.write(__CRLF);
128     }
129     
130     /* ------------------------------------------------------------ */
131     @Override
132     public void write(byte[] b, int off, int len) throws IOException
133     {
134         out.write(b,off,len);
135     }
136 }
137 
138 
139 
140