Coverage Report - org.mule.transport.http.multipart.MultiPartOutputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiPartOutputStream
0%
0/43
0%
0/10
1.667
 
 1  
 /*
 2  
  * $Id: MultiPartOutputStream.java 20320 2010-11-24 15:03:31Z dfeist $
 3  
  * -------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 package org.mule.transport.http.multipart;
 11  
 
 12  
 import java.io.FilterOutputStream;
 13  
 import java.io.IOException;
 14  
 import java.io.OutputStream;
 15  
 
 16  
 /* ================================================================ */
 17  
 /** Handle a multipart MIME response.
 18  
  *
 19  
  * @author Greg Wilkins
 20  
  * @author Jim Crossley
 21  
 */
 22  
 public class MultiPartOutputStream extends FilterOutputStream
 23  
 {
 24  
     /* ------------------------------------------------------------ */
 25  
     private static byte[] __CRLF;
 26  
     private static byte[] __DASHDASH;
 27  
     private String encoding;
 28  
    
 29  
 
 30  
     /* ------------------------------------------------------------ */
 31  
     private String boundary;
 32  
     private byte[] boundaryBytes;
 33  
 
 34  
     /* ------------------------------------------------------------ */
 35  0
     private boolean inPart=false;    
 36  
     
 37  
     /* ------------------------------------------------------------ */
 38  
     public MultiPartOutputStream(OutputStream out, String encoding)
 39  
     throws IOException
 40  
     {
 41  0
         super(out);
 42  0
         this.encoding = encoding;
 43  
         
 44  0
         __CRLF="\015\012".getBytes(encoding);
 45  0
         __DASHDASH="--".getBytes(encoding);
 46  
         
 47  0
         boundary = "mule"+System.identityHashCode(this)+
 48  
         Long.toString(System.currentTimeMillis(),36);
 49  0
         boundaryBytes=boundary.getBytes(encoding);
 50  
 
 51  0
         inPart=false;
 52  0
     }
 53  
 
 54  
     
 55  
 
 56  
     /* ------------------------------------------------------------ */
 57  
     /** End the current part.
 58  
      * @exception IOException IOException
 59  
      */
 60  
     public void close()
 61  
          throws IOException
 62  
     {
 63  0
         if (inPart)
 64  0
             out.write(__CRLF);
 65  0
         out.write(__DASHDASH);
 66  0
         out.write(boundaryBytes);
 67  0
         out.write(__DASHDASH);
 68  0
         out.write(__CRLF);
 69  0
         inPart=false;
 70  0
         super.close();
 71  0
     }
 72  
     
 73  
     /* ------------------------------------------------------------ */
 74  
     public String getBoundary()
 75  
     {
 76  0
         return boundary;
 77  
     }
 78  
 
 79  0
     public OutputStream getOut() {return out;}
 80  
     
 81  
     /* ------------------------------------------------------------ */
 82  
     /** Start creation of the next Content.
 83  
      */
 84  
     public void startPart(String contentType)
 85  
          throws IOException
 86  
     {
 87  0
         if (inPart)
 88  0
             out.write(__CRLF);
 89  0
         inPart=true;
 90  0
         out.write(__DASHDASH);
 91  0
         out.write(boundaryBytes);
 92  0
         out.write(__CRLF);
 93  0
         out.write(("Content-Type: "+contentType).getBytes(encoding));
 94  0
         out.write(__CRLF);
 95  0
         out.write(__CRLF);
 96  0
     }
 97  
         
 98  
     /* ------------------------------------------------------------ */
 99  
     /** Start creation of the next Content.
 100  
      */
 101  
     public void startPart(String contentType, String[] headers)
 102  
          throws IOException
 103  
     {
 104  0
         if (inPart)
 105  0
             out.write(__CRLF);
 106  0
         inPart=true;
 107  0
         out.write(__DASHDASH);
 108  0
         out.write(boundaryBytes);
 109  0
         out.write(__CRLF);
 110  0
         out.write(("Content-Type: "+contentType).getBytes(encoding));
 111  0
         out.write(__CRLF);
 112  0
         for (int i=0;headers!=null && i<headers.length;i++)
 113  
         {
 114  0
             out.write(headers[i].getBytes(encoding));
 115  0
             out.write(__CRLF);
 116  
         }
 117  0
         out.write(__CRLF);
 118  0
     }
 119  
     
 120  
 }