1 10 11 package org.mule.providers; 12 13 import org.mule.umo.provider.MessageTypeNotSupportedException; 14 15 import java.io.IOException ; 16 import java.io.StringWriter ; 17 import java.io.Writer ; 18 19 23 public class WriterMessageAdapter extends AbstractMessageAdapter 24 { 25 28 private static final long serialVersionUID = -1065602752454818625L; 29 30 private final StringWriter writer; 31 32 public WriterMessageAdapter(Object message) throws MessageTypeNotSupportedException 33 { 34 if (message instanceof String ) 35 { 36 writer = new StringWriter (); 37 writer.write((String )message); 38 } 39 else if (message instanceof StringWriter ) 40 { 41 this.writer = (StringWriter )message; 42 } 43 else 44 { 45 throw new MessageTypeNotSupportedException(message, getClass()); 46 } 47 48 } 49 50 58 public String getPayloadAsString(String encoding) throws Exception 59 { 60 return writer.toString(); 61 } 62 63 69 public byte[] getPayloadAsBytes() throws Exception 70 { 71 return writer.toString().getBytes(); 72 } 73 74 77 public Object getPayload() 78 { 79 return writer.toString(); 80 } 81 82 public void write(String string) 83 { 84 writer.write(string); 85 } 86 87 public void write(String string, int offset, int len) 88 { 89 writer.write(string, offset, len); 90 } 91 92 public Writer getWriter() 93 { 94 return writer; 95 } 96 97 public void flush() 98 { 99 writer.flush(); 100 } 101 102 public void close() throws IOException 103 { 104 writer.close(); 105 } 106 } 107 | Popular Tags |