Implement Java bindings for pipe io client
This commit is contained in:
		
							
								
								
									
										26
									
								
								clients/java/PipeIO.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								clients/java/PipeIO.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class PipeIO implements AutoCloseable {
 | 
			
		||||
    protected PipeReader reader;
 | 
			
		||||
    protected PipeWriter writer;
 | 
			
		||||
 | 
			
		||||
    public PipeIO(String in_pipe_path, String out_pipe_path) throws IOException {
 | 
			
		||||
        this.reader = new PipeReader(in_pipe_path);
 | 
			
		||||
        this.writer = new PipeWriter(out_pipe_path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void close() throws IOException {
 | 
			
		||||
        this.reader.close();
 | 
			
		||||
        this.writer.close();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void run() throws IOException {
 | 
			
		||||
        String msg;
 | 
			
		||||
        while ((msg = this.reader.recvMessage()) != null) {
 | 
			
		||||
            this.handleMessage(msg);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void handleMessage(String msg) throws IOException {}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								clients/java/PipeReader.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								clients/java/PipeReader.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
import java.io.BufferedReader;
 | 
			
		||||
import java.io.FileReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.lang.AutoCloseable;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class PipeReader implements AutoCloseable {
 | 
			
		||||
    private BufferedReader pipe;
 | 
			
		||||
 | 
			
		||||
    public PipeReader(String pipe_path) throws IOException {
 | 
			
		||||
        this.pipe = new BufferedReader(new FileReader(pipe_path));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void close() throws IOException {
 | 
			
		||||
        this.pipe.close();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String recvMessage() throws IOException {
 | 
			
		||||
        return this.pipe.readLine();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								clients/java/PipeWriter.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								clients/java/PipeWriter.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
import java.io.BufferedWriter;
 | 
			
		||||
import java.io.FileWriter;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.lang.AutoCloseable;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class PipeWriter implements AutoCloseable {
 | 
			
		||||
    private BufferedWriter pipe;
 | 
			
		||||
 | 
			
		||||
    public PipeWriter(String pipe_path) throws IOException {
 | 
			
		||||
        this.pipe = new BufferedWriter(new FileWriter(pipe_path));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void close() throws IOException {
 | 
			
		||||
        this.pipe.close();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void sendMessage(String msg) throws IOException {
 | 
			
		||||
        this.pipe.write(msg + "\n");
 | 
			
		||||
        this.pipe.flush();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								clients/java/Test.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								clients/java/Test.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class Test extends PipeIO {
 | 
			
		||||
    public Test(String in_pipe_path, String out_pipe_path) throws IOException {
 | 
			
		||||
        super(in_pipe_path, out_pipe_path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void handleMessage(String msg) throws IOException {
 | 
			
		||||
        this.writer.sendMessage(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        try(Test test = new Test("in", "out")) {
 | 
			
		||||
            test.run();
 | 
			
		||||
        }
 | 
			
		||||
        catch (IOException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user