View Javadoc

1   package org.rpi.mplayer;
2   
3   import java.io.PrintStream;
4   
5   import org.apache.log4j.Logger;
6   
7   public class InputWriter  {
8   	
9   	private Logger log = Logger.getLogger(InputWriter.class);
10  	
11  	private Process process = null;
12  	
13  	/** Stream used to send commands to mplayer */
14  	private volatile PrintStream streamToProcess;
15  	private boolean stopSendingCommands = false;
16  	
17  
18  	
19  	public InputWriter(Process process)
20  	{
21  		this.process = process;//MPlayerEngine.getInstance().getProcess();
22  	}
23  	
24  	/***
25  	 * Send Command to MPlayer
26  	 * @param command
27  	 */
28  	public synchronized void sendCommand(final String command) {
29  		if (!this.isStopSendingCommands()) {
30  			if (this.process != null) {
31  				if (this.streamToProcess == null) {
32  					this.streamToProcess = new PrintStream(
33  							this.process.getOutputStream());
34  				}
35  				if (this.streamToProcess != null) {
36  					this.streamToProcess.print(command);
37  				}
38  				if (this.streamToProcess != null) {
39  					this.streamToProcess.print('\n');
40  				}
41  				if (this.streamToProcess != null) {
42  					this.streamToProcess.flush();
43  					//log.debug("Sent Command: " + command);
44  				}
45  			}
46  		}
47  	}
48  
49  	/**
50  	 * @return the stopSendingCommands
51  	 */
52  	private boolean isStopSendingCommands() {
53  		return stopSendingCommands;
54  	}
55  
56  	/**
57  	 * @param stopSendingCommands the stopSendingCommands to set
58  	 */
59  	public void setStopSendingCommands(boolean stopSendingCommands) {
60  		this.stopSendingCommands = stopSendingCommands;
61  	}
62  	
63  	/***
64  	 * 
65  	 */
66  	public void dispose()
67  	{
68  		try
69  		{
70  		this.streamToProcess = null;
71  		}
72  		catch(Exception e)
73  		{
74  			log.error("Error Closing BufferedWriter");
75  		}
76  		finally
77  		{
78  			log.debug("Closing BufferedWriter");
79  			CloseMe.close(streamToProcess);
80  		}
81  		}
82  	
83  	
84  
85  
86  }