1 24 package org.objectweb.jalisto.se.impl.util; 25 26 import org.objectweb.jalisto.se.impl.LogicalOid; 27 import org.objectweb.jalisto.se.api.Session; 28 29 import java.util.Properties ; 30 import java.util.Iterator ; 31 import java.io.*; 32 import java.net.URL ; 33 34 public class JalistoUtils { 35 36 public static File loadPropertiesFileIn(String propertiesFilePath, Properties prop) throws IOException { 37 boolean isLoaded = false; 38 File propfile = null; 39 if (!propertiesFilePath.equalsIgnoreCase("")) { 40 propfile = new File(propertiesFilePath); 41 if (propfile.exists() && (!propfile.isDirectory())) { 42 FileInputStream fis = new FileInputStream(propfile); 43 prop.load(fis); 44 isLoaded = true; 45 } 46 if (!isLoaded) { 47 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 48 InputStream testIS = classLoader.getResourceAsStream(propertiesFilePath); 49 URL url = classLoader.getResource(propertiesFilePath); 50 propfile = new File(url.getFile()); 51 prop.load(testIS); 52 } 53 } 54 return propfile; 55 } 56 57 public static void truncateProperties(Properties prop) { 58 Iterator keys = prop.keySet().iterator(); 59 while (keys.hasNext()) { 60 String key = (String ) keys.next(); 61 String value = prop.getProperty(key); 62 while (value.endsWith(" ") || value.endsWith("\t")) { 63 value = value.substring(0, value.length() - 1); 64 } 65 prop.setProperty(key, value); 66 } 67 } 68 69 79 public static String substring(String str, int start) { 80 if (str == null) { 81 return null; 82 } 83 84 if (start < 0) { 86 start = str.length() + start; } 88 89 if (start < 0) { 90 start = 0; 91 } 92 93 if (start > str.length()) { 94 return ""; 95 } 96 97 return str.substring(start); 98 } 99 100 public static String arrayToString(Object [] array) { 101 StringBuffer sb = new StringBuffer (); 102 sb.append("["); 103 for (short i = 0; i < array.length; i++) { 104 sb.append(array[i]); 105 if (i < array.length - 1) { 106 sb.append(","); 107 } 108 } 109 sb.append("]"); 110 return sb.toString(); 111 } 112 113 public static String arrayToString(Object [] array, Session session) { 114 StringBuffer sb = new StringBuffer (); 115 sb.append("["); 116 for (short i = 0; i < array.length; i++) { 117 Object o = array[i]; 118 if (o instanceof LogicalOid) { 119 sb.append(arrayToString(session.readObjectByOid(o), session)); 120 } else { 121 sb.append(o); 122 } 123 if (i < array.length - 1) { 124 sb.append(","); 125 } 126 } 127 sb.append("]"); 128 return sb.toString(); 129 } 130 131 public static boolean areEquals(byte[] b1, byte[] b2) { 132 if (b1.length != b2.length) { 133 return false; 134 } 135 for (int i = 0; i < b1.length; i++) { 136 if (b1[i] != b2[i]) { 137 return false; 138 } 139 } 140 return true; 141 } 142 143 public static boolean areEquals(Object [] b1, Object [] b2) { 144 if (b1.length != b2.length) { 145 return false; 146 } 147 for (int i = 0; i < b1.length; i++) { 148 if (!b1[i].equals(b2[i])) { 149 return false; 150 } 151 } 152 return true; 153 } 154 } 155 | Popular Tags |