Skip to content

Instantly share code, notes, and snippets.

@jorgejch
Last active May 24, 2018 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgejch/3c1e86518eea04c3b40b to your computer and use it in GitHub Desktop.
Save jorgejch/3c1e86518eea04c3b40b to your computer and use it in GitHub Desktop.
Class implements named pipe access and creation.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.channels.FileChannel;
// From: https://github.com/sioutisc/jdds/blob/master/JDDS/src/rtjdds/rtps/transport/NamedPipe.java
public class NamedPipe extends File {
private NamedPipe(String name) {
super(name);
}
public FileChannel getInputChannel() throws FileNotFoundException {
FileInputStream is = new FileInputStream(this);
return is.getChannel();
}
public FileChannel getOutputChannel() throws FileNotFoundException {
FileOutputStream os = new FileOutputStream(this);
return os.getChannel();
}
public static NamedPipe createPipe(String name) throws IOException {
Runtime.getRuntime().exec("mkfifo "+name);
NamedPipe pipe = new NamedPipe(name);
if (pipe.exists())
return new NamedPipe(name);
else
throw new IOException("Errors while creating the pipe");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment