1 13 14 package org.ejbca.util; 15 16 import java.io.*; 17 18 import org.apache.log4j.Logger; 19 20 21 26 public class FileTools { 27 private static Logger log = Logger.getLogger(FileTools.class); 28 29 32 public FileTools() { 33 } 34 35 48 public static byte[] getBytesFromPEM(byte[] inbuf, String beginKey, String endKey) 49 throws IOException { 50 log.debug(">getBytesFromPEM"); 51 52 ByteArrayInputStream instream = new ByteArrayInputStream(inbuf); 53 BufferedReader bufRdr = new BufferedReader(new InputStreamReader(instream)); 54 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); 55 PrintStream opstr = new PrintStream(ostr); 56 String temp; 57 58 while (((temp = bufRdr.readLine()) != null) && !temp.equals(beginKey)) { 59 continue; 60 } 61 62 if (temp == null) { 63 throw new IOException("Error in input buffer, missing " + beginKey + " boundary"); 64 } 65 66 while (((temp = bufRdr.readLine()) != null) && !temp.equals(endKey)) { 67 opstr.print(temp); 68 } 69 70 if (temp == null) { 71 throw new IOException("Error in input buffer, missing " + endKey + " boundary"); 72 } 73 74 opstr.close(); 75 76 byte[] bytes = Base64.decode(ostr.toByteArray()); 77 78 log.debug("<getBytesFromPEM"); 79 80 return bytes; 81 } 83 92 public static byte[] readFiletoBuffer(String file) 93 throws IOException { 94 ByteArrayOutputStream os = new ByteArrayOutputStream(); 95 InputStream in = new FileInputStream(file); 96 int len = 0; 97 byte[] buf = new byte[1024]; 98 99 while ((len = in.read(buf)) > 0) { 100 os.write(buf, 0, len); 101 } 102 103 in.close(); 104 os.close(); 105 106 return os.toByteArray(); 107 } } | Popular Tags |