KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > JavaUtils


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils;
18
19
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28
29 /**
30  * A collection of different, general-purpose methods for JAVA-specific things
31  * @author Christian Geuer-Pollmann
32  *
33  */

34 public class JavaUtils {
35
36    /** {@link java.util.logging} logging facility */
37     static java.util.logging.Logger JavaDoc log =
38         java.util.logging.Logger.getLogger(JavaUtils.class.getName());
39
40    private JavaUtils() {
41      // we don't allow instantiation
42
}
43    /**
44     * Method getBytesFromFile
45     *
46     * @param fileName
47     * @return the bytes readed from the file
48     *
49     * @throws FileNotFoundException
50     * @throws IOException
51     */

52    public static byte[] getBytesFromFile(String JavaDoc fileName)
53            throws FileNotFoundException JavaDoc, IOException JavaDoc {
54
55       byte refBytes[] = null;
56
57       {
58          FileInputStream JavaDoc fisRef = new FileInputStream JavaDoc(fileName);
59          UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream();
60          byte buf[] = new byte[1024];
61          int len;
62
63          while ((len = fisRef.read(buf)) > 0) {
64             baos.write(buf, 0, len);
65          }
66
67          refBytes = baos.toByteArray();
68       }
69
70       return refBytes;
71    }
72
73    /**
74     * Method writeBytesToFilename
75     *
76     * @param filename
77     * @param bytes
78     */

79    public static void writeBytesToFilename(String JavaDoc filename, byte[] bytes) {
80
81       try {
82          if (filename != null && bytes != null) {
83             File JavaDoc f = new File JavaDoc(filename);
84
85             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(f);
86
87             fos.write(bytes);
88             fos.close();
89          } else {
90             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "writeBytesToFilename got null byte[] pointed");
91          }
92       } catch (Exception JavaDoc ex) {}
93    }
94
95    /**
96     * This method reads all bytes from the given InputStream till EOF and returns
97     * them as a byte array.
98     *
99     * @param inputStream
100     * @return the bytes readed from the stream
101     *
102     * @throws FileNotFoundException
103     * @throws IOException
104     */

105    public static byte[] getBytesFromStream(InputStream JavaDoc inputStream) throws IOException JavaDoc {
106
107       byte refBytes[] = null;
108
109       {
110          UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream();
111          byte buf[] = new byte[1024];
112          int len;
113
114          while ((len = inputStream.read(buf)) > 0) {
115             baos.write(buf, 0, len);
116          }
117
118          refBytes = baos.toByteArray();
119       }
120
121       return refBytes;
122    }
123 }
124
Popular Tags