进程通道通讯,实现传转对象

Main.java
/*
 * 主程序,用于调用exe
 *
 */
public class Main
{

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException
	{
		disport();
	}
	
	public static void disport() throws IOException, ClassNotFoundException, InterruptedException
	{
		Message message = new MyMessage();
		message.setCmd(10000);
		
		Process process = Runtime.getRuntime().exec("java -cp c:/exe.jar; com.java.exe.Exe");
		//发送
		OutputStream out = process.getOutputStream();
		out.write(ObjectTran.objectToByteArray(message));
		out.flush();
		out.close();
		
		
		//接收
		InputStream in = process.getInputStream();
		InputStream isr = new BufferedInputStream(in);
		ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
		
		byte[] b = new byte[1];
		while((isr.read(b)) != -1)
		{
			bout.write(b);
		}
		byte[] allB = bout.toByteArray();
		bout.flush();
		bout.close();
		Object obj = ObjectTran.byteArrayToObject(allB);
		if(obj instanceof Entity)
		{
			Entity en = (Entity)obj;
			System.out.println(en.getName());
			System.out.println(en.getAge());
		}
		
	}
}


Exe.java
/*
 * 模仿exe程序
 *
 */
public class Exe
{

	public static void main(String[] args) throws IOException, ClassNotFoundException
	{
		exe();
	}

	
	public static void exe() throws IOException, ClassNotFoundException
	{
		//接收
		InputStream in = System.in;
		InputStream isr = new BufferedInputStream(in);
		ByteArrayOutputStream out = new ByteArrayOutputStream(); 
		
		byte[] b = new byte[1];
		while((isr.read(b)) != -1)
		{
			out.write(b);
		}
		out.flush();
		out.close();
		byte[] allB = out.toByteArray();
		
		
		//处理
		Object obj = ObjectTran.byteArrayToObject(allB);

		if(!(obj instanceof Message))
		{
			return;
		}
		
		Message message = (Message)obj;
		
		int cmd = message.getCmd();
		Entity en = new Entity();
		
		if(cmd == 10000)
		{
			en.setAge(20);
			en.setName("admin");
		}
		
		//发送
		OutputStream sout = System.out;
		sout.write(ObjectTran.objectToByteArray(en));
		sout.flush();
		sout.close();
	}
}


ObjectTran.java
/** 转换工具,byte[]与object互转 **/
public class ObjectTran
{
	/**
	* 将Object转为byte[]
	**/
	public static byte[] objectToByteArray(Object obj) throws IOException
	{
		ByteArrayOutputStream out = new ByteArrayOutputStream(); 
		ObjectOutputStream ot = new ObjectOutputStream(out); 
		ot.writeObject(obj); 
		ot.flush(); 
		ot.close(); 
		return out.toByteArray(); 
	}
	
	/**
	* 将byte[]转为Object
	**/
	public static Object byteArrayToObject(byte[] b) throws IOException, ClassNotFoundException
	{
		ByteArrayInputStream in = new ByteArrayInputStream(b); 
		ObjectInputStream oi = new ObjectInputStream(in); 
		Object o = oi.readObject(); 
		oi.close(); 
		return o; 

	}
	
	
	/** 测试 **/
	public static void main(String[] args) throws IOException, ClassNotFoundException
	{
		Entity entity = new Entity();
		entity.setAge(11);
		entity.setName("xie");
		byte[] b = objectToByteArray(entity);
		
		Object obj = byteArrayToObject(b);
		Entity e = (Entity)obj;
		System.out.println(e.getName());
		System.out.println(e.getAge());
	}

}


你可能感兴趣的:(java)