KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > tools > FileTools


1 package net.sourceforge.jcetaglib.tools;
2
3 import org.bouncycastle.util.encoders.Base64;
4
5 import java.io.*;
6
7 /**
8  * Tools to handle some common file operations.
9  *
10  * @version $Id: FileTools.java,v 1.4 2004/04/15 07:28:36 hamgert Exp $
11  */

12 public class FileTools {
13
14     //private static Category cat = Category.getInstance(FileTools.class.getName());
15

16     /** Creates new FileTools */
17     public FileTools() {
18     }
19
20     /**
21      * Reads binary bytes from a PEM-file. The PEM-file may contain other stuff,
22      * the first item between beginKey and endKey is read.
23      * <p>Example:
24      *<pre>
25      *-----BEGIN CERTIFICATE REQUEST-----
26      *<base64 encoded PKCS10 certification request>
27      *-----END CERTIFICATE REQUEST-----
28      *
29      * @param inbuf input buffer containing PEM-formatted stuff.
30      * @return byte[] containing binary Base64 decoded bytes.
31      * @exception IOException if the PEM file does not contain the right keys.
32      */

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

57     /**
58      * Helpfunction to read a file to a byte array.
59      *
60      *@param file filename of file.
61      *@return byte[] containing the contents of the file.
62      *@exception IOException if the file does not exist or cannot be read.
63      **/

64     public static byte[] readFiletoBuffer(String JavaDoc 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     } // readFiletoBuffer
75

76 } // FileTools
77

78
Popular Tags