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 net.wimpi.modbus.msg.ModbusResponse; import net.wimpi.modbus.msg.ReadCoilsResponse; import net.wimpi.modbus.io.BytesInputStream; import net.wimpi.modbus.ModbusIOException; import net.wimpi.modbus.util.ModbusUtil; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.DataInputStream; /** * org.mule.transformer.simple * * @author Christopher Cheng * @version $Id: Apr 23, 2009 6:07:30 PM */ public class ByteArrayToModbusResponse extends AbstractTransformer { public ByteArrayToModbusResponse() { registerSourceType(byte[].class); setReturnClass(ModbusResponse.class); } public Object doTransform(Object src, String outputEncoding) throws TransformerException { try { BytesInputStream m_ByteIn = new BytesInputStream((byte[]) src); DataInputStream m_Input = new DataInputStream(m_ByteIn); byte[] buffer = m_ByteIn.getBuffer(); //read to byte length of message if (m_Input.read(buffer, 0, 6) == -1) { throw new ModbusIOException("Premature end of stream (Header truncated)."); } //extract length of bytes following in message int bf = ModbusUtil.registerToShort(buffer, 4); //read rest if (m_Input.read(buffer, 6, bf) == -1) { throw new ModbusIOException("Premature end of stream (Message truncated)."); } m_ByteIn.reset(buffer, (6 + bf)); m_ByteIn.skip(7); int functionCode = m_ByteIn.readUnsignedByte(); m_ByteIn.reset(); ModbusResponse res = ModbusResponse.createModbusResponse(functionCode); res.readFrom(m_ByteIn); return res; } catch (Exception e) { e.printStackTrace(System.out); return null; } } }