KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > FileTools


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.util;
15
16 import java.io.*;
17
18 import org.apache.log4j.Logger;
19
20
21 /**
22  * Tools to handle some common file operations.
23  *
24  * @version $Id: FileTools.java,v 1.1 2006/01/17 20:32:19 anatom Exp $
25  */

26 public class FileTools {
27     private static Logger log = Logger.getLogger(FileTools.class);
28
29     /**
30      * Creates new FileTools
31      */

32     public FileTools() {
33     }
34
35     /**
36      * Reads binary bytes from a PEM-file. The PEM-file may contain other stuff, the first item
37      * between beginKey and endKey is read. Example: <code>-----BEGIN CERTIFICATE REQUEST-----
38      * base64 encoded PKCS10 certification request -----END CERTIFICATE REQUEST----- </code>
39      *
40      * @param inbuf input buffer containing PEM-formatted stuff.
41      * @param beginKey begin line of PEM message
42      * @param endKey end line of PEM message
43      *
44      * @return byte[] containing binary Base64 decoded bytes.
45      *
46      * @throws IOException if the PEM file does not contain the right keys.
47      */

48     public static byte[] getBytesFromPEM(byte[] inbuf, String JavaDoc beginKey, String JavaDoc 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 JavaDoc 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     } // getBytesfromPEM
82

83     /**
84      * Helpfunction to read a file to a byte array.
85      *
86      * @param file filename of file.
87      *
88      * @return byte[] containing the contents of the file.
89      *
90      * @throws IOException if the file does not exist or cannot be read.
91      */

92     public static byte[] readFiletoBuffer(String JavaDoc 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     } // readFiletoBuffer
108
} // FileTools
109
Popular Tags