OneAdministrator 8 سال پیش
والد
کامیت
effb733021

+ 26 - 86
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

@@ -1,40 +1,41 @@
 package com.github.jhonnymertz.wkhtmltopdf.wrapper;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.page.Page;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.page.PageType;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
 import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Params;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang3.StringUtils;
-
-import java.io.*;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
 
-public class Pdf implements PdfService {
+public class Pdf {
 
     private static final String STDINOUT = "-";
 
-    private WrapperConfig wrapperConfig;
+    private final WrapperConfig wrapperConfig;
 
-    private Params params;
+    private final Params params;
 
-    private List<Page> pages;
+    private final List<Page> pages;
 
     private boolean hasToc = false;
 
+    public Pdf() {
+      this(new WrapperConfig());
+    }
+
     public Pdf(WrapperConfig wrapperConfig) {
         this.wrapperConfig = wrapperConfig;
         this.params = new Params();
         this.pages = new ArrayList<Page>();
     }
 
-    public Pdf() {
-        this(new WrapperConfig());
-    }
-
     public void addPage(String source, PageType type) {
         this.pages.add(new Page(source, type));
     }
@@ -43,58 +44,34 @@ public class Pdf implements PdfService {
         this.hasToc = true;
     }
 
-    public void addParam(Param param) {
-        params.add(param);
-    }
-
-    public void addParam(Param... params) {
-        for (Param param : params) {
-            addParam(param);
-        }
+    public void addParam(Param param, Param... params) {
+        this.params.add( param, params );
     }
 
     public void saveAs(String path) throws IOException, InterruptedException {
         saveAs(path, getPDF());
     }
 
-    private File saveAs(String path, byte[] document) throws IOException {
+    private static File saveAs(String path, byte[] document) throws IOException {
         File file = new File(path);
-
-        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
-        bufferedOutputStream.write(document);
-        bufferedOutputStream.flush();
-        bufferedOutputStream.close();
+        FileUtils.writeByteArrayToFile( file, document );
 
         return file;
     }
 
     public byte[] getPDF() throws IOException, InterruptedException {
-        Runtime runtime = Runtime.getRuntime();
-        Process process = runtime.exec(getCommandAsArray());
+        Process process = Runtime.getRuntime().exec(getCommandAsArray());
 
-        StreamEater outputStreamEater = new StreamEater(process.getInputStream());
-        outputStreamEater.start();
-
-        StreamEater errorStreamEater = new StreamEater(process.getErrorStream());
-        errorStreamEater.start();
-
-        outputStreamEater.join();
-        errorStreamEater.join();
+        byte[] inputBytes = IOUtils.toByteArray( process.getInputStream() );
+        byte[] errorBytes = IOUtils.toByteArray( process.getErrorStream() );
+        
         process.waitFor();
 
         if (process.exitValue() != 0) {
-            throw new RuntimeException("Process (" + getCommand() + ") exited with status code " + process.exitValue() + ":\n" + new String(errorStreamEater.getBytes()));
-        }
-
-        if (outputStreamEater.getError() != null) {
-            throw outputStreamEater.getError();
-        }
-
-        if (errorStreamEater.getError() != null) {
-            throw errorStreamEater.getError();
+            throw new RuntimeException("Process (" + getCommand() + ") exited with status code " + process.exitValue() + ":\n" + new String(errorBytes));
         }
 
-        return outputStreamEater.getBytes();
+        return inputBytes;
     }
 
     private String[] getCommandAsArray() throws IOException {
@@ -129,41 +106,4 @@ public class Pdf implements PdfService {
         return StringUtils.join(getCommandAsArray(), " ");
     }
 
-    private class StreamEater extends Thread {
-
-        private InputStream stream;
-        private ByteArrayOutputStream bytes;
-
-        private IOException error;
-
-        public StreamEater(InputStream stream) {
-            this.stream = stream;
-
-            bytes = new ByteArrayOutputStream();
-        }
-
-        public void run() {
-            try {
-                int bytesRead = stream.read();
-                while (bytesRead >= 0) {
-                    bytes.write(bytesRead);
-                    bytesRead = stream.read();
-                }
-
-                stream.close();
-            } catch (IOException e) {
-                e.printStackTrace();
-
-                error = e;
-            }
-        }
-
-        public IOException getError() {
-            return error;
-        }
-
-        public byte[] getBytes() {
-            return bytes.toByteArray();
-        }
-    }
 }

+ 10 - 22
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/configurations/WrapperConfig.java

@@ -1,8 +1,8 @@
 package com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import org.apache.commons.io.IOUtils;
 
 public class WrapperConfig {
 
@@ -10,12 +10,12 @@ public class WrapperConfig {
 
     private String wkhtmltopdfCommand = "wkhtmltopdf";
 
-    public WrapperConfig(String wkhtmltopdfCommand) {
-        this.wkhtmltopdfCommand = wkhtmltopdfCommand;
+    public WrapperConfig() {
+      setWkhtmltopdfCommand(findExecutable());
     }
 
-    public WrapperConfig() {
-        this.wkhtmltopdfCommand = findExecutable();
+    public WrapperConfig(String wkhtmltopdfCommand) {
+        setWkhtmltopdfCommand(wkhtmltopdfCommand);
     }
 
     public String getWkhtmltopdfCommand() {
@@ -32,32 +32,20 @@ public class WrapperConfig {
      * @return the wkhtmltopdf command according to the OS
      */
     public String findExecutable() {
-
         try {
-
             String osname = System.getProperty("os.name").toLowerCase();
 
-            String cmd;
-            if (osname.contains("windows"))
-                cmd = "where wkhtmltopdf";
-            else cmd = "which wkhtmltopdf";
+            String cmd = osname.contains("windows") ? "where wkhtmltopdf" : "which wkhtmltopdf";
 
             Process p = Runtime.getRuntime().exec(cmd);
             p.waitFor();
 
-            BufferedReader reader =
-                    new BufferedReader(new InputStreamReader(p.getInputStream()));
-
-            StringBuilder sb = new StringBuilder();
-            String line;
-            while ((line = reader.readLine()) != null) {
-                sb.append(line);
-            }
+            String text = IOUtils.toString(p.getInputStream(), Charset.defaultCharset());
 
-            if (sb.toString().isEmpty())
+            if (text.isEmpty())
                 throw new RuntimeException();
 
-            setWkhtmltopdfCommand(sb.toString());
+            setWkhtmltopdfCommand(text);
         } catch (InterruptedException e) {
             e.printStackTrace();
         } catch (IOException e) {

+ 7 - 11
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/configurations/XvfbConfig.java

@@ -9,22 +9,18 @@ import java.util.List;
 public class XvfbConfig {
 
     private String command;
-    private Params params;
+    private final Params params = new Params();
 
-    public XvfbConfig(String command) {
-        this.command = command;
-        params = new Params();
+    public XvfbConfig() {
+        this("xvfb-run");
     }
 
-    public XvfbConfig() {
-        command = "xvfb-run";
-        params = new Params();
+    public XvfbConfig(String command) {
+        setCommand(command);
     }
 
-    public void addParams(Param... params) {
-        for (Param param : params) {
-            this.params.add(param);
-        }
+    public void addParams(Param param, Param... params) {
+        this.params.add(param, params);
     }
 
     public String getCommand() {

+ 2 - 3
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/params/Param.java

@@ -8,11 +8,10 @@ public class Param {
     private String key;
 
     // Some commands accept more than one value such as cookies and headers
-    private List<String> values;
+    private List<String> values = new ArrayList<String>();
 
     public Param(String key, String... valueArray) {
         this.key = key;
-        this.values = new ArrayList<String>();
         for (String value : valueArray) {
             values.add(value);
         }
@@ -46,7 +45,7 @@ public class Param {
 
     @Deprecated
     public void setValue(String value) {
-        if (values.size() == 0) {
+        if (values.isEmpty()) {
             values.add(value);
         } else {
             values.set(0, value);

+ 8 - 14
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/params/Params.java

@@ -1,24 +1,22 @@
 package com.github.jhonnymertz.wkhtmltopdf.wrapper.params;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
+import org.apache.commons.lang3.StringUtils;
 
 public class Params {
 
-    private List<Param> params;
+    private Collection<Param> params;
 
     public Params() {
         this.params = new ArrayList<Param>();
     }
 
-    public void add(Param param) {
-        params.add(param);
-    }
-
-    public void add(Param... params) {
-        for (Param param : params) {
-            add(param);
-        }
+    public void add(Param param, Param... params) {
+        this.params.add(param);
+        this.params.addAll( Arrays.asList( params ) );
     }
 
     public List<String> getParamsAsStringList() {
@@ -39,11 +37,7 @@ public class Params {
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder();
-        for (Param param : params) {
-            sb.append(param);
-        }
-        return sb.toString();
+      return StringUtils.join(params, "");
     }
 
 }

+ 1 - 1
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/params/Symbol.java

@@ -4,7 +4,7 @@ public enum Symbol {
 
     separator(" "), param("");
 
-    private String symbol;
+    private final String symbol;
 
     Symbol(String symbol) {
         this.symbol = symbol;