KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > util > JalistoUtils


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.io.*;
32 import java.net.URL JavaDoc;
33
34 public class JalistoUtils {
35
36     public static File loadPropertiesFileIn(String JavaDoc propertiesFilePath, Properties JavaDoc 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 JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
48                 InputStream testIS = classLoader.getResourceAsStream(propertiesFilePath);
49                 URL JavaDoc 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 JavaDoc prop) {
58         Iterator JavaDoc keys = prop.keySet().iterator();
59         while (keys.hasNext()) {
60             String JavaDoc key = (String JavaDoc) keys.next();
61             String JavaDoc 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     /**
70      * Gets a substring of the specified string avoiding exceptions.
71      * A negative start position can be used to start n characters from
72      * the end of the string.
73      *
74      * @param str the string to get the substring from
75      * @param start the position to start from, negative means
76      * count back from the end of the string by this many characters
77      * @return substring from start position
78      */

79     public static String JavaDoc substring(String JavaDoc str, int start) {
80         if (str == null) {
81             return null;
82         }
83
84         // handle negatives, which means last n characters
85
if (start < 0) {
86             start = str.length() + start; // remember start is negative
87
}
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 JavaDoc arrayToString(Object JavaDoc[] array) {
101         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc arrayToString(Object JavaDoc[] array, Session session) {
114         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
115         sb.append("[");
116         for (short i = 0; i < array.length; i++) {
117             Object JavaDoc 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 JavaDoc[] b1, Object JavaDoc[] 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