|
|
|
@ -123,8 +123,8 @@ public class Ghostscript implements AutoCloseable {
|
|
|
|
|
stdIn = inputStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized void run(String[] args) throws IOException {
|
|
|
|
|
run(args, null, null);
|
|
|
|
|
public synchronized int run(String[] args) throws IOException {
|
|
|
|
|
return run(args, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -133,20 +133,24 @@ public class Ghostscript implements AutoCloseable {
|
|
|
|
|
* @param args Interpreter parameters. Use the same as Ghostscript command line arguments.
|
|
|
|
|
* @param runString run a string for the Ghostscript interpreter.
|
|
|
|
|
* @param fileName run a postscript file for the Ghostscript interpreter.
|
|
|
|
|
* @return the ghostscript return code;
|
|
|
|
|
* @throws IOException if initialize fails
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void run(String[] args,
|
|
|
|
|
String runString,
|
|
|
|
|
String fileName) throws IOException {
|
|
|
|
|
public synchronized int run(String[] args,
|
|
|
|
|
String runString,
|
|
|
|
|
String fileName) throws IOException {
|
|
|
|
|
GhostscriptLibrary.gs_main_instance.ByReference nativeInstanceByRef = null;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
boolean quit = false;
|
|
|
|
|
try {
|
|
|
|
|
nativeInstanceByRef = new GhostscriptLibrary.gs_main_instance.ByReference();
|
|
|
|
|
int result = libraryInstance.gsapi_new_instance(nativeInstanceByRef.getPointer(), null);
|
|
|
|
|
if (result != 0) {
|
|
|
|
|
ret = libraryInstance.gsapi_new_instance(nativeInstanceByRef.getPointer(), null);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
nativeInstanceByRef = null;
|
|
|
|
|
throw new IOException("can not call Ghostscript gsapi_new_instance, error code " + result);
|
|
|
|
|
throw new IOException("can not call Ghostscript gsapi_new_instance, error code = " + ret);
|
|
|
|
|
}
|
|
|
|
|
logger.log(Level.INFO, "ghostscript instance " + nativeInstanceByRef + " created");
|
|
|
|
|
Pointer pointer = nativeInstanceByRef.getValue();
|
|
|
|
|
GhostscriptLibrary.stdin_fn stdinCallback = null;
|
|
|
|
|
if (getStdIn() != null) {
|
|
|
|
|
stdinCallback = (caller_handle, buf, len) -> {
|
|
|
|
@ -189,22 +193,21 @@ public class Ghostscript implements AutoCloseable {
|
|
|
|
|
return len;
|
|
|
|
|
};
|
|
|
|
|
logger.log(Level.FINE, "setting gsapi_set_stdio");
|
|
|
|
|
result = libraryInstance.gsapi_set_stdio(nativeInstanceByRef.getValue(), stdinCallback,
|
|
|
|
|
stdoutCallback, stderrCallback);
|
|
|
|
|
if (result != 0) {
|
|
|
|
|
throw new IOException("can not set stdio on Ghostscript interpreter, error code " + result);
|
|
|
|
|
ret = libraryInstance.gsapi_set_stdio(pointer, stdinCallback, stdoutCallback, stderrCallback);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
throw new IOException("can not set stdio on Ghostscript interpreter, error code " + ret);
|
|
|
|
|
}
|
|
|
|
|
logger.log(Level.FINE, "gsapi_init_with_args = " + (args != null ? Arrays.asList(args) : "null"));
|
|
|
|
|
result = libraryInstance.gsapi_init_with_args(nativeInstanceByRef.getValue(),
|
|
|
|
|
args != null ? args.length : 0, args);
|
|
|
|
|
logger.log(Level.FINE, "gsapi_init_with_args result = " + result);
|
|
|
|
|
if (result == ErrorCodes.gs_error_Quit) {
|
|
|
|
|
result = 0;
|
|
|
|
|
ret = libraryInstance.gsapi_init_with_args(pointer, args != null ? args.length : 0, args);
|
|
|
|
|
logger.log(Level.FINE, "gsapi_init_with_args return code = " + ret);
|
|
|
|
|
if (ret == ErrorCodes.gs_error_Quit) {
|
|
|
|
|
quit = true;
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
if (result == 0) {
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
if (runString != null) {
|
|
|
|
|
IntByReference exitCode = new IntByReference();
|
|
|
|
|
libraryInstance.gsapi_run_string_begin(nativeInstanceByRef.getValue(), 0, exitCode);
|
|
|
|
|
ret = libraryInstance.gsapi_run_string_begin(pointer, 0, exitCode);
|
|
|
|
|
if (exitCode.getValue() != 0) {
|
|
|
|
|
throw new IOException("can not run command on Ghostscript interpreter. gsapi_run_string_begin failed with error code "
|
|
|
|
|
+ exitCode.getValue());
|
|
|
|
@ -212,14 +215,13 @@ public class Ghostscript implements AutoCloseable {
|
|
|
|
|
String[] slices = runString.split("\n");
|
|
|
|
|
for (String slice1 : slices) {
|
|
|
|
|
String slice = slice1 + "\n";
|
|
|
|
|
libraryInstance.gsapi_run_string_continue(nativeInstanceByRef.getValue(), slice, slice.length(),
|
|
|
|
|
0, exitCode);
|
|
|
|
|
libraryInstance.gsapi_run_string_continue(pointer, slice, slice.length(), 0, exitCode);
|
|
|
|
|
if (exitCode.getValue() != 0) {
|
|
|
|
|
throw new IOException("can not run command on Ghostscript interpreter. gsapi_run_string_continue failed with error code "
|
|
|
|
|
+ exitCode.getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
libraryInstance.gsapi_run_string_end(nativeInstanceByRef.getValue(), 0, exitCode);
|
|
|
|
|
ret = libraryInstance.gsapi_run_string_end(pointer, 0, exitCode);
|
|
|
|
|
if (exitCode.getValue() != 0) {
|
|
|
|
|
throw new IOException("can not run command on Ghostscript interpreter. gsapi_run_string_end failed with error code "
|
|
|
|
|
+ exitCode.getValue());
|
|
|
|
@ -228,32 +230,36 @@ public class Ghostscript implements AutoCloseable {
|
|
|
|
|
}
|
|
|
|
|
if (fileName != null) {
|
|
|
|
|
IntByReference exitCode = new IntByReference();
|
|
|
|
|
libraryInstance.gsapi_run_file(nativeInstanceByRef.getValue(), fileName, 0, exitCode);
|
|
|
|
|
ret = libraryInstance.gsapi_run_file(pointer, fileName, 0, exitCode);
|
|
|
|
|
if (exitCode.getValue() != 0) {
|
|
|
|
|
throw new IOException("can not run file on Ghostscript interpreter, error code " + exitCode.getValue());
|
|
|
|
|
throw new IOException("can not run file on Ghostscript interpreter, error code = " + exitCode.getValue());
|
|
|
|
|
}
|
|
|
|
|
logger.log(Level.FINE, "file completed: " + fileName);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
if (result < 0) {
|
|
|
|
|
throw new IOException("can not initialize Ghostscript interpreter, error code " + result);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
throw new IOException("can not initialize Ghostscript interpreter, error code = " + ret);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (nativeInstanceByRef != null) {
|
|
|
|
|
Pointer pointer = nativeInstanceByRef.getValue();
|
|
|
|
|
if (pointer != null) {
|
|
|
|
|
int result = libraryInstance.gsapi_exit(pointer);
|
|
|
|
|
if (result != 0) {
|
|
|
|
|
logger.log(Level.SEVERE, "can not call Ghostscript gsapi_exit, error code " + result);
|
|
|
|
|
if (!quit && ret >= 0) {
|
|
|
|
|
logger.log(Level.INFO, "exiting ghostscript instance " + libraryInstance);
|
|
|
|
|
ret = libraryInstance.gsapi_exit(pointer);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
logger.log(Level.SEVERE, "can not call Ghostscript gsapi_exit, error code " + ret);
|
|
|
|
|
}
|
|
|
|
|
logger.log(Level.INFO, "deleting ghostscript instance " + pointer);
|
|
|
|
|
libraryInstance.gsapi_delete_instance(pointer);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
logger.log(Level.WARNING, "no pointer to exit");
|
|
|
|
|
}
|
|
|
|
|
libraryInstance.gsapi_delete_instance(nativeInstanceByRef.getValue());
|
|
|
|
|
logger.log(Level.INFO, "ghostscript instance " + nativeInstanceByRef + " deleted");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|