Java tutorial
/* * Generated by the Mule project wizard. http://mule.mulesource.org * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */ package org.mule.transformer.simple; import org.mule.transformer.AbstractTransformer; import org.mule.api.transformer.TransformerException; import org.apache.commons.io.output.ByteArrayOutputStream; import net.wimpi.modbus.msg.ModbusRequest; import java.io.DataOutput; import java.io.DataOutputStream; /** * org.mule.transformer.simple * * @author Christopher Cheng * @version $Id: Apr 23, 2009 6:09:16 PM */ public class ModbusRequestToByteArray extends AbstractTransformer { public ModbusRequestToByteArray() { registerSourceType(ModbusRequest.class); setReturnClass(byte[].class); } public Object doTransform(Object src, String outputEncoding) throws TransformerException { ModbusRequest request = (ModbusRequest) src; ByteArrayOutputStream os = new ByteArrayOutputStream(); DataOutput dout = new DataOutputStream(os); try { if (!request.isHeadless()) { dout.writeShort(request.getTransactionID()); dout.writeShort(request.getProtocolID()); dout.writeShort(request.getDataLength()); } dout.writeByte(request.getUnitID()); dout.writeByte(request.getFunctionCode()); request.writeData(dout); return os.toByteArray(); } catch (Exception e) { e.printStackTrace(System.out); return null; } } }