Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix linux pdf opening again #5945

Merged
merged 6 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import java.util.Optional;
import java.util.StringJoiner;

import org.jabref.JabRefExecutorService;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.util.StreamGobbler;
import org.jabref.preferences.JabRefPreferences;

import org.slf4j.Logger;
Expand All @@ -22,6 +24,7 @@
import static org.jabref.preferences.JabRefPreferences.USE_PDF_READER;

public class Linux implements NativeDesktop {

private static final Logger LOGGER = LoggerFactory.getLogger(Linux.class);

@Override
Expand All @@ -34,14 +37,13 @@ public void openFile(String filePath, String fileType) throws IOException {
} else {
viewer = "xdg-open";
}
String[] cmdArray = { viewer, filePath };
Process p = Runtime.getRuntime().exec(cmdArray);
// When the stream is full at some point, then blocks the execution of the program
// See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process.
BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
line = in.readLine();
LOGGER.debug("Received output: " + line);
ProcessBuilder processBuilder = new ProcessBuilder(viewer, filePath);
Process process = processBuilder.start();
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
}

@Override
Expand All @@ -53,17 +55,18 @@ public void openFileWithApplication(String filePath, String application) throws
} else {
openWith = new String[] {"xdg-open"};
}

String[] cmdArray = new String[openWith.length + 1];
System.arraycopy(openWith, 0, cmdArray, 0, openWith.length);
cmdArray[cmdArray.length - 1] = filePath;
Process p = Runtime.getRuntime().exec(cmdArray);
// When the stream is full at some point, then blocks the execution of the program
// See https://stackoverflow.com/questions/10981969/why-is-going-through-geterrorstream-necessary-to-run-a-process.
BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
line = in.readLine();
LOGGER.debug("Received output: " + line);

ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
Process process = processBuilder.start();

StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
}

@Override
Expand Down Expand Up @@ -118,7 +121,7 @@ public void openPdfWithParameters(String filePath, List<String> parameters) thro

openFileWithApplication(filePath, sj.toString());
} else {
openFile( filePath, "PDF");
openFile(filePath, "PDF");
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/jabref/gui/util/StreamGobbler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jabref.gui.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StreamGobbler implements Runnable {

private static final Logger LOGGER = LoggerFactory.getLogger(StreamGobbler.class);

private InputStream inputStream;
private Consumer<String> consumer;

public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}

@Override
public void run() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
reader.lines().forEach(consumer);
} catch (IOException e) {
LOGGER.error("Error when reading process stream from external application", e);
}
}
}