1 17 package com.sun.org.apache.xml.internal.security.utils; 18 19 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 28 29 34 public class JavaUtils { 35 36 37 static java.util.logging.Logger log = 38 java.util.logging.Logger.getLogger(JavaUtils.class.getName()); 39 40 private JavaUtils() { 41 } 43 52 public static byte[] getBytesFromFile(String fileName) 53 throws FileNotFoundException , IOException { 54 55 byte refBytes[] = null; 56 57 { 58 FileInputStream fisRef = new FileInputStream (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 79 public static void writeBytesToFilename(String filename, byte[] bytes) { 80 81 try { 82 if (filename != null && bytes != null) { 83 File f = new File (filename); 84 85 FileOutputStream fos = new FileOutputStream (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 ex) {} 93 } 94 95 105 public static byte[] getBytesFromStream(InputStream inputStream) throws IOException { 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 |