Coverage Report - org.mule.providers.streaming.OutStreamMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
OutStreamMessageAdapter
0%
0/40
0%
0/10
1.769
 
 1  
 /*
 2  
  * $Id: OutStreamMessageAdapter.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
 
 11  
 package org.mule.providers.streaming;
 12  
 
 13  
 import org.mule.impl.ThreadSafeAccess;
 14  
 import org.mule.providers.AbstractMessageAdapter;
 15  
 import org.mule.umo.provider.MessageTypeNotSupportedException;
 16  
 import org.mule.util.StringMessageUtils;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.OutputStream;
 20  
 
 21  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 22  
 
 23  
 /**
 24  
  * <code>StreamMessageAdapter</code> wraps an java.io.OutputStream and allows meta
 25  
  * information to be associated with the stream.
 26  
  */
 27  
 public class OutStreamMessageAdapter extends AbstractMessageAdapter
 28  
 {
 29  
     /**
 30  
      * Serial version
 31  
      */
 32  
     private static final long serialVersionUID = -299373598028203772L;
 33  
 
 34  
     private final OutputStream out;
 35  
 
 36  
     public OutStreamMessageAdapter(Object message) throws MessageTypeNotSupportedException
 37  0
     {
 38  
         try
 39  
         {
 40  0
             if (message instanceof OutputStream)
 41  
             {
 42  0
                 out = (OutputStream) message;
 43  
             }
 44  0
             else if (message instanceof String)
 45  
             {
 46  0
                 out = new ByteArrayOutputStream(message.toString().length());
 47  0
                 out.write(StringMessageUtils.getBytes(message.toString()));
 48  
             }
 49  0
             else if (message instanceof byte[])
 50  
             {
 51  0
                 out = new ByteArrayOutputStream(((byte[]) message).length);
 52  0
                 out.write((byte[]) message);
 53  
 
 54  
             }
 55  
             else
 56  
             {
 57  0
                 throw new MessageTypeNotSupportedException(message, getClass());
 58  
             }
 59  
         }
 60  0
         catch (IOException e)
 61  
         {
 62  0
             throw new MessageTypeNotSupportedException(message, getClass(), e);
 63  0
         }
 64  0
     }
 65  
 
 66  
     protected OutStreamMessageAdapter(OutStreamMessageAdapter template)
 67  
     {
 68  0
         super(template);
 69  0
         out = template.out;
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Converts the message implementation into a String representation
 74  
      * 
 75  
      * @param encoding The encoding to use when transforming the message (if
 76  
      *            necessary). The parameter is used when converting from a byte array
 77  
      * @return String representation of the message payload
 78  
      * @throws Exception Implementation may throw an endpoint specific exception
 79  
      */
 80  
     public String getPayloadAsString(String encoding) throws Exception
 81  
     {
 82  0
         if (out instanceof ByteArrayOutputStream)
 83  
         {
 84  0
             return StringMessageUtils.getString(((ByteArrayOutputStream) out).toByteArray(), encoding);
 85  
         }
 86  
         else
 87  
         {
 88  0
             logger.warn("Attempting to get the String contents of a non-ByteArray output stream");
 89  0
             return out.toString();
 90  
         }
 91  
     }
 92  
 
 93  
     /**
 94  
      * Converts the message implementation into a String representation
 95  
      * 
 96  
      * @return String representation of the message
 97  
      * @throws Exception Implemetation may throw an endpoint specific exception
 98  
      */
 99  
     public byte[] getPayloadAsBytes() throws Exception
 100  
     {
 101  0
         if (out instanceof ByteArrayOutputStream)
 102  
         {
 103  0
             return ((ByteArrayOutputStream) out).toByteArray();
 104  
         }
 105  
         else
 106  
         {
 107  0
             logger.warn("Attempting to get the bytes of a non-ByteArray output stream");
 108  0
             return StringMessageUtils.getBytes(out.toString());
 109  
         }
 110  
     }
 111  
 
 112  
     /**
 113  
      * @return the current message
 114  
      */
 115  
     public Object getPayload()
 116  
     {
 117  0
         return out;
 118  
     }
 119  
 
 120  
     public void write(String string) throws IOException
 121  
     {
 122  0
         out.write(StringMessageUtils.getBytes(string));
 123  0
     }
 124  
 
 125  
     public void write(String string, int offset, int len) throws IOException
 126  
     {
 127  0
         out.write(StringMessageUtils.getBytes(string), offset, len);
 128  0
     }
 129  
 
 130  
     public void write(byte[] bytes) throws IOException
 131  
     {
 132  0
         out.write(bytes);
 133  0
     }
 134  
 
 135  
     public void write(byte[] bytes, int offset, int len) throws IOException
 136  
     {
 137  0
         out.write(bytes, offset, len);
 138  0
     }
 139  
 
 140  
     public OutputStream getStream()
 141  
     {
 142  0
         return out;
 143  
     }
 144  
 
 145  
     public void flush() throws IOException
 146  
     {
 147  0
         out.flush();
 148  0
     }
 149  
 
 150  
     public void close() throws IOException
 151  
     {
 152  0
         out.close();
 153  0
     }
 154  
 
 155  
     public ThreadSafeAccess newThreadCopy()
 156  
     {
 157  0
         return new OutStreamMessageAdapter(this);
 158  
     }
 159  
     
 160  
 }