1 10 11 package org.mule.transformers.codec; 12 13 import org.mule.config.i18n.Message; 14 import org.mule.config.i18n.Messages; 15 import org.mule.transformers.AbstractTransformer; 16 import org.mule.umo.transformer.TransformerException; 17 18 import java.io.IOException ; 19 import java.io.UnsupportedEncodingException ; 20 21 28 public class UCDecoder extends AbstractTransformer 29 { 30 33 private static final long serialVersionUID = -5063055816071537443L; 34 35 private sun.misc.UCDecoder decoder; 36 37 public UCDecoder() 38 { 39 registerSourceType(String .class); 40 registerSourceType(byte[].class); 41 setReturnClass(String .class); 42 decoder = new sun.misc.UCDecoder(); 43 } 44 45 50 public Object doTransform(Object src, String encoding) throws TransformerException 51 { 52 String data = null; 53 if (src instanceof byte[]) 54 { 55 if (encoding != null) 56 { 57 try 58 { 59 data = new String ((byte[])src, encoding); 60 } 61 catch (UnsupportedEncodingException ex) 62 { 63 data = new String ((byte[])src); 64 } 65 } 66 else 67 { 68 data = new String ((byte[])src); 69 } 70 } 71 else 72 { 73 data = (String )src; 74 } 75 try 76 { 77 byte[] result = decoder.decodeBuffer(data); 78 if (getReturnClass().equals(String .class)) 79 { 80 if (encoding != null) 81 { 82 return new String (result, encoding); 83 } 84 else 85 { 86 return new String (result); 87 } 88 } 89 return result; 90 } 91 catch (IOException e) 92 { 93 throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X, "UU Encoding"), 94 this, e); 95 } 96 } 97 } 98 | Popular Tags |