src/share/classes/com/sun/org/apache/xml/internal/security/utils/JavaUtils.java

Print this page


   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 }
   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 }