1 /* 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 */ 21 package com.sun.org.apache.xml.internal.security.utils; 22 23 import java.io.File; 24 import java.io.FileInputStream; 25 import java.io.FileNotFoundException; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.InputStream; 29 30 /** 31 * A collection of different, general-purpose methods for JAVA-specific things 32 * @author Christian Geuer-Pollmann 33 */ 34 public class JavaUtils { 35 36 /** {@link java.util.logging} logging facility */ 37 static java.util.logging.Logger log = 38 java.util.logging.Logger.getLogger(JavaUtils.class.getName()); 39 40 private JavaUtils() { 41 // we don't allow instantiation 42 } 43 44 /** 45 * Method getBytesFromFile 46 * 47 * @param fileName 48 * @return the bytes readed from the file 49 * 50 * @throws FileNotFoundException 51 * @throws IOException 52 */ 53 public static byte[] getBytesFromFile(String fileName) 54 throws FileNotFoundException, IOException { 55 56 byte refBytes[] = null; 57 58 FileInputStream fisRef = new FileInputStream(fileName); 59 try { 60 UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream(); 61 byte buf[] = new byte[1024]; 62 int len; 63 64 while ((len = fisRef.read(buf)) > 0) { 65 baos.write(buf, 0, len); 66 } 67 68 refBytes = baos.toByteArray(); 69 } finally { 70 fisRef.close(); 71 } 72 73 return refBytes; 74 } 75 76 /** 77 * Method writeBytesToFilename 78 * 79 * @param filename 80 * @param bytes 81 */ 82 public static void writeBytesToFilename(String filename, byte[] bytes) { 83 84 FileOutputStream fos = null; 85 try { 86 if (filename != null && bytes != null) { 87 File f = new File(filename); 88 89 fos = new FileOutputStream(f); 90 91 fos.write(bytes); 92 fos.close(); 93 } else { 94 log.log(java.util.logging.Level.FINE, "writeBytesToFilename got null byte[] pointed"); 95 } 96 } catch (IOException ex) { 97 if (fos != null) { 98 try { 99 fos.close(); 100 } catch (IOException ioe) {} 101 } 102 } 103 } 104 105 /** 106 * This method reads all bytes from the given InputStream till EOF and 107 * returns them as a byte array. 108 * 109 * @param inputStream 110 * @return the bytes readed from the stream 111 * 112 * @throws FileNotFoundException 113 * @throws IOException 114 */ 115 public static byte[] getBytesFromStream(InputStream inputStream) 116 throws IOException { 117 118 byte refBytes[] = null; 119 120 UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream(); 121 byte buf[] = new byte[1024]; 122 int len; 123 124 while ((len = inputStream.read(buf)) > 0) { 125 baos.write(buf, 0, len); 126 } 127 128 refBytes = baos.toByteArray(); 129 return refBytes; 130 } 131 }