blob: 52eb6ac556a25de46a7647bb5f28ca88e9868fae [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21package com.sun.org.apache.xml.internal.security.utils;
22
23
24
25import java.io.File;
26import java.io.FileInputStream;
27import java.io.FileNotFoundException;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.io.InputStream;
31
32
33/**
34 * A collection of different, general-purpose methods for JAVA-specific things
35 * @author Christian Geuer-Pollmann
36 *
37 */
38public class JavaUtils {
39
40 /** {@link java.util.logging} logging facility */
41 static java.util.logging.Logger log =
42 java.util.logging.Logger.getLogger(JavaUtils.class.getName());
43
44 private JavaUtils() {
45 // we don't allow instantiation
46 }
47 /**
48 * Method getBytesFromFile
49 *
50 * @param fileName
51 * @return the bytes readed from the file
52 *
53 * @throws FileNotFoundException
54 * @throws IOException
55 */
56 public static byte[] getBytesFromFile(String fileName)
57 throws FileNotFoundException, IOException {
58
59 byte refBytes[] = null;
60
61 {
62 FileInputStream fisRef = new FileInputStream(fileName);
63 UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream();
64 byte buf[] = new byte[1024];
65 int len;
66
67 while ((len = fisRef.read(buf)) > 0) {
68 baos.write(buf, 0, len);
69 }
70
71 refBytes = baos.toByteArray();
72 }
73
74 return refBytes;
75 }
76
77 /**
78 * Method writeBytesToFilename
79 *
80 * @param filename
81 * @param bytes
82 */
83 public static void writeBytesToFilename(String filename, byte[] bytes) {
84
85 try {
86 if (filename != null && bytes != null) {
87 File f = new File(filename);
88
89 FileOutputStream fos = new FileOutputStream(f);
90
91 fos.write(bytes);
92 fos.close();
93 } else {
94 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "writeBytesToFilename got null byte[] pointed");
95 }
96 } catch (Exception ex) {}
97 }
98
99 /**
100 * This method reads all bytes from the given InputStream till EOF and returns
101 * them as a byte array.
102 *
103 * @param inputStream
104 * @return the bytes readed from the stream
105 *
106 * @throws FileNotFoundException
107 * @throws IOException
108 */
109 public static byte[] getBytesFromStream(InputStream inputStream) throws IOException {
110
111 byte refBytes[] = null;
112
113 {
114 UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream();
115 byte buf[] = new byte[1024];
116 int len;
117
118 while ((len = inputStream.read(buf)) > 0) {
119 baos.write(buf, 0, len);
120 }
121
122 refBytes = baos.toByteArray();
123 }
124
125 return refBytes;
126 }
127}