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