Downloading long files freezes the computer - java

I have made a launcher for my game Privateers. It works perfectly, downloads everything needed - But, for some reason, it freezes the entire computer!
The freeze usually occurs when doing something, if I afk at my computer until the download completes, then nothing happens. However, when I tested it on my mothers computer playing the game "World Of Tanks", the computer froze almost immediately. If I play a game then the launcher also has a tendency to freeze my computer.
I use windows 8, my mother uses windows 7.
On my own computer, when this happens I am able to move the mouse very slowly (between 30 second to 2 minute delay), alt+tab won't work, control+alt+delete will work (but when opening task manager the task manager does not appear).
On my mothers' computer it is basically the same except that EVERYTHING is frozen 100% except for the mouse which is working fine.
It only happens when downloading large (5MB+) files. When my launcher downloads smaller files there is no issue.
I use the following code to download files:
void download(String source, String destination, int size) {
File ofile = new File(System.getProperty("user.dir") + "", destination);
System.out.printf("\nDownloading\n\t%s\nTo\n\t%s\n", source, destination);
try {
if (ofile.exists()) ofile.delete();
if (!ofile.createNewFile()) {
throw new IOException("Can't create " + ofile.getAbsolutePath());
}
int inChar = 0;
URL url = new URL(source);
InputStream input = url.openStream();
FileOutputStream fos = new FileOutputStream(ofile);
for (int i = 0; i < size && inChar != -1; i++) {
int percentage = (int) ((i * 100.0f) / size);
progressBar.setValue(((int) ((percentage * 100.0f) / 100)));
fr.setTitle(ofile.getName() + ": " + progressBar.getValue() + "%" + " Total: " + oprogressBar.getValue() + "%");
inChar = input.read();
fos.write(inChar);
}
input.close();
fos.close();
System.out.println("Downloaded " + ofile.getAbsolutePath());
} catch (EOFException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I have been unable to find a duplicate of this happening when searching on the internet. Any help is appreciated.

Maybe multithreading will help you out over here.
See more about it in this post

Buffer the input stream, or read more than one character at a time, or both.

Related

Java get current line number when writing to a file

I am writing an application that writes to a text file.
In this text file, at the beginning of each line, is the line number
How do I get the current line number so that I can write it in my file?
I've thought of a simple counter, but when I terminate and restart my project the counter would reset back to 1.
I've tried LineNumberReader, but that keeps on giving me 0.
Is there any way to get the current line number when writing to a file?
Thanks
try {
FileWriter writer = new FileWriter("src\\book_store\\transaction.txt", true);
read = new FileReader("src\\book_store\\transaction.txt");
line = new LineNumberReader(read);
for(int i = 0; i < output.size(); i++) {
line.readLine();
temp = line.getLineNumber() + " " + timeStamp2 + " " + output.get(i) + " " + timeStamp + "\n";
output.set(i, temp);
writer.write(output.get(i));
}
writer.close();
read.close();
line.close();
} catch (IOException ex) {
System.out.println("File not found.");
}
The mistake you are making is that while you are writing to a file through FileWriter, you are trying to read the same file using FileReader.
This could potentially work if you fsynced the file after each line. However, this is not what you really want to do. As you see from the comments, people get confused as you are complicating the problem.
You already have the counter. It’s the i loop variable. You might add +1, if you want to start from 1.

Phantom Js does not wait for page to be load or all jquery code to be executed

I am trying to convert a webpage into pdf but web page is getting load and some jquery function needs to executed on page to bring dynamic data which takes time hence page loading is not a problem here but dynamic addition of data via jquery is troubling me alot. below is code which i am using to generate pdf
String pathToPhantomJS = "/usr/bin/phantomjs" //path to your phantom js
String pathToRasterizeJS = "/home/tothenew/Desktop/rasterize.js" //path to your rasterize.js
String paperSize = "A4"
String url = "https://www.google.co.in/" //url of your web page to which you want to convert into pdf
File outputFile = File.createTempFile("sample", ".pdf") //file in which you want to save your pdf
//TODO: also do exception handling stuff . i am not doing this for simplicity
Process process = Runtime.getRuntime().exec(pathToPhantomJS + " " + pathToRasterizeJS + " " + url + " " + outputFile.absolutePath + " " + paperSize);
int exitStatus = process.waitFor(); //do a wait here to prevent it running for ever
if (exitStatus != 0) {
log.error("EXIT-STATUS - " + process.toString());
}
is there is way in java i can tell wait for page to be load or what should i do so that phantom js will capture the webpage only after its get loaded completely.
According to this Stack Overflow post, this is a problem with phantomjs, not Java. However, If you are just trying to get a webpage as PDF, there are other options. After a bit of google-fu, I found this website which supplies a flexible command-line tool with ample documentation, as well as a C library. Seeing as you are already using a command-line tool to get your page, implementing this one should be no problem in your code. Here is a short example:
String wkhtmltopdf_path = "/usr/local/bin/wkhtmltopdf";
String paperSize = "A4";
String url = "https://www.google.com/";
// This is where your output file is/was defined, now with error handling
File outputFile = null;
try {
//file in which you want to save your pdf
outputFile = File.createTempFile("sample", ".pdf");
System.out.println(outputFile.getAbsolutePath()); // Show output file path, remove this in production
} catch (IOException e1) {
e1.printStackTrace();
}
// This is where your process runs, with error handling
Process process = null;
try {
process = Runtime.getRuntime().exec(String.format("%s -s %s %s %s", wkhtmltopdf_path, paperSize, url, outputFile.getAbsolutePath()));
} catch (IOException e) {
e.printStackTrace(); // Do your error handling here
}
// This is where your exitStatus and waitFor() was/is, with error handling
int exitStatus = 0;
try {
//do a wait here to prevent it running for ever
exitStatus = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace(); // Do your error handling here
}
if (exitStatus != 0) {
// Do error handling here
}

Converting huge amount of small PDF files to PNG

I need to transform over 500k pdf files to png (with the sufficient density) tpo be able to treat it later (reading a QR code embedded in the pdf). The files normally don't surpass 200kb
I tried using magick to convert it (first using convert and then mogrify), but figured that it will take days to finish and figured that maybe doing it with threads was better. So i implemented a little app in java that creates n threads and executes in the windows shell (Runtime.getRuntime().exec()) a builded command with the file and target and all that.
Problem is it kills my pc. Apparently magick uses multithreads for processing each images and since some of those are taken by the script I did, it takes longer and takes resources that the jvm wouldnt normally take. Here is my code:
public class SuperPdfToPngConverter {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Setting Up Environment...");
System.out.println("Reading existing files...");
// To change according to our needs
String targetFolder = "D:\\Digest";
// Execution parameters
ArrayList<String> myList = getTodoList(targetFolder);
ArrayList<String> rejected = new ArrayList<String>();
System.out.println("I've found " + myList.size() + " documents pending to be converted.");
System.out.println(" Treating files... ");
int count = 1;
ExecutorService executor = Executors.newFixedThreadPool(16);
for (String fileId : myList) {
System.out.println("Queueing file " + count + ", " + fileId);
Runnable worker = new WorkerThread(fileId, targetFolder, true, 150);
executor.execute(worker);
//rejected.add(fileId);
count++;
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished");
System.out.println(
"Treated " + (count - 1) + " documents; ");
}
And the working part on each thread goes like this:
#Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. File = "+ fileName);
processCommand();
System.out.println(Thread.currentThread().getName()+" End.");
}
private void processCommand() {
String fileNamePng = fileName.replace(".pdf", ".png");
String cmd = "magick convert -density " + density + "x" + density + " " + fileName + " " + fileNamePng;
System.out.println(cmd);
try {
Runtime.getRuntime().exec(cmd, null, new File(targetPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Document " + fileName + " processed.");
}
I was wondering what can I do to make this run a little bit faster and stable. I don't care much about how long it takes (as long as its reasonable).
Do you think my approach is good? Is it better if I use a library rather than a tool like magick?
Thank you for your insight.

What is causing my output to be so messy?

I am currently reading sensor values from an android device, and streaming them to a pc client connected.
bzero(buffer,256);
//fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
while(1){
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
sleep(1);
printf("%s\n",buffer);
}
close(sockfd);
return 0;}
Code for the PC client that outputs the data.
while(!stopFlag){
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true);
// out.printf(String.valueOf(accel[0]),accel[1],accel[2]);
out.printf(String.valueOf(d.format(accel[0]))+" "+String.valueOf(d.format(accel[1]))+" "+String.valueOf(d.format(accel[2])) + " Accelerometer " + "\n");
out.printf(String.valueOf(d.format(gyro[0]))+" "+String.valueOf(d.format(gyro[1]))+" "+String.valueOf(d.format(gyro[2]))+ " Gyroscope " + "\n");
out.printf(String.valueOf(d.format(magnet[0]))+" "+String.valueOf(d.format(magnet[1]))+" "+String.valueOf(d.format(magnet[2]))+" Magnetometer "+ "\n\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Code Snippet of sensor values being sent.
How would I fix this or what's a cleaner method to output the data? [SOLVED]
Also how do I write the output as a csv formatted file. I've looked around but still not too sure how to read my sensor values, since I have a 4x4, being the sensor values and name of the sensor as seen in the image above.
If I were to add a delay/sleep function on the client side, after closing the sockets on the application side, the client would still show incoming values, any ideas on why ?
Any help or advice would be greatly appreciated!
Regards
Roger has provided me with correct answer, not sure where his post is now though.
Removing the \n (new line) fixed the issue, data now doesn't flow to next line.

Best way to call terminal command repeatedly

I'm using mencoder to split files and I'd like to turn this into an Object Oriented approach, if possible, using Java or similar, for example. But I'm not sure the best way, so I leave it in the open. Here is what I need:
I have an excel file with start times and end times, and I need to extract out the appropriate clips from a video file. In the terminal (I'm on Mac OS X) I've had success using, for example:
mencoder -ss 0 -endpos 10 MyVideo.avi -oac copy -ovc copy -o Output.avi
Which creates the video Output.avi by clipping the first 10 seconds of the video MyVideo.avi.
But, like I said, I want to make it so that a program reads in from an excel file, and calls this mencoder command multiple times (over 100) for each of the start times and end times.
I know how to read in the excel file in Java, but I'm not sure it is best to call this command from Java. Plus, I'd like to be able to see the output of mencoder (because it prints out a nice percentage so you know about how much longer a single command will take). Is this type of thing feasible to do in a shell script? I would really like to use Java if possible, since I have many years of experience in Java and no experience in shell scripting.
UPDATE
Here is what I've tried in Java, but it freezes at in.readLine()
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd ..");
out.println("pwd");
String video = "/Users/MyFolder/MyFile.avi";
String output = "/Users/MyFolder/output.avi";
int start = 0;
int end = 6;
String cmd = "mencoder -ss " + start +
" -endpos " + end +
" " + video + " -oac copy -ovc copy -o " + output;
out.println(cmd);
try {
String line;
System.out.println("top");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.println("end");
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
I'm not quite sure about mencoders multicore-capabilities, but I think with Java you can use Multiple Threads to get the maximal power of all cpu-cores.
You shouldn't use Runtime like your using it.
When using Runtime, you should not run bash and send commands via inputstream like when you are typing commands on a terminal.
Runtime.getRuntime().exec("mencoder -ss " + start +
" -endpos " + end +
" " + video + " -oac copy -ovc copy -o " + output);
To get the Output, you can use the inputStream
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String[],%20java.io.File%29
With this command you can also set the Workingdirectory where your command is executed.
I also prefer the version with the String[] as parameters. It's much more readable, than the a concatenated String.

Categories