1 package net.sourceforge.jcetaglib.tools; 2 3 import org.bouncycastle.util.encoders.Base64; 4 5 import java.io.*; 6 7 12 public class FileTools { 13 14 16 17 public FileTools() { 18 } 19 20 33 public static byte[] getBytesFromPEM(byte[] inbuf, String beginKey, String endKey) 34 throws IOException { 35 ByteArrayInputStream instream = new ByteArrayInputStream(inbuf); 36 BufferedReader bufRdr = new BufferedReader(new InputStreamReader(instream)); 37 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); 38 PrintStream opstr = new PrintStream(ostr); 39 String temp; 40 while ((temp = bufRdr.readLine()) != null && 41 !temp.equals(beginKey)) 42 continue; 43 if (temp == null) 44 throw new IOException("Error in input buffer, missing " + beginKey + " boundary"); 45 while ((temp = bufRdr.readLine()) != null && 46 !temp.equals(endKey)) 47 opstr.print(temp); 48 if (temp == null) 49 throw new IOException("Error in input buffer, missing " + endKey + " boundary"); 50 opstr.close(); 51 52 byte[] bytes = Base64.decode(ostr.toByteArray()); 53 54 return bytes; 55 } 57 64 public static byte[] readFiletoBuffer(String file) throws IOException { 65 ByteArrayOutputStream os = new ByteArrayOutputStream(); 66 InputStream in = new FileInputStream(file); 67 int len = 0; 68 byte buf[] = new byte[1024]; 69 while ((len = in.read(buf)) > 0) 70 os.write(buf, 0, len); 71 in.close(); 72 os.close(); 73 return os.toByteArray(); 74 } 76 } 78 | Popular Tags |