1 20 21 package org.apache.directory.ldapstudio.browser.core.utils; 22 23 24 import java.beans.XMLDecoder ; 25 import java.beans.XMLEncoder ; 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 34 public class Utils 35 { 36 37 public static String [] stringToArray( String s ) 38 { 39 if ( s == null ) 40 { 41 return null; 42 } 43 else 44 { 45 List attributeList = new ArrayList (); 46 47 StringBuffer temp = new StringBuffer (); 48 for ( int i = 0; i < s.length(); i++ ) 49 { 50 char c = s.charAt( i ); 51 52 if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c >= '0' && c <= '9' ) || c == '-' 53 || c == '.' || c == ';' ) 54 { 55 temp.append( c ); 56 } 57 else 58 { 59 if ( temp.length() > 0 ) 60 { 61 attributeList.add( temp.toString() ); 62 temp = new StringBuffer (); 63 } 64 } 65 } 66 if ( temp.length() > 0 ) 67 { 68 attributeList.add( temp.toString() ); 69 } 70 71 return ( String [] ) attributeList.toArray( new String [attributeList.size()] ); 72 } 73 74 } 88 89 90 public static String arrayToString( String [] array ) 91 { 92 if ( array == null || array.length == 0 ) 93 { 94 return ""; 95 } 96 else 97 { 98 StringBuffer sb = new StringBuffer ( array[0] ); 99 for ( int i = 1; i < array.length; i++ ) 100 { 101 sb.append( ", " ); 102 sb.append( array[i] ); 103 } 104 return sb.toString(); 105 } 106 } 107 108 109 public static boolean equals( byte[] data1, byte[] data2 ) 110 { 111 if ( data1 == data2 ) 112 return true; 113 if ( data1 == null || data2 == null ) 114 return false; 115 if ( data1.length != data2.length ) 116 return false; 117 for ( int i = 0; i < data1.length; i++ ) 118 { 119 if ( data1[i] != data2[i] ) 120 return false; 121 } 122 return true; 123 } 124 125 126 public static String getShortenedString( String value, int length ) 127 { 128 129 if ( value == null ) 130 return ""; 131 132 if ( value.length() > length ) 133 { 134 value = value.substring( 0, length ) + "..."; 135 } 136 137 return value; 138 } 139 140 141 public static String serialize( Object o ) 142 { 143 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 144 XMLEncoder encoder = new XMLEncoder ( baos ); 145 encoder.writeObject( o ); 146 encoder.close(); 147 String s = LdifUtils.utf8decode( baos.toByteArray() ); 148 return s; 149 } 150 151 152 public static Object deserialize( String s ) 153 { 154 ByteArrayInputStream bais = new ByteArrayInputStream ( LdifUtils.utf8encode( s ) ); 155 XMLDecoder decoder = new XMLDecoder ( bais ); 156 Object o = decoder.readObject(); 157 decoder.close(); 158 return o; 159 } 160 161 162 public static String getNonNullString( Object o ) 163 { 164 return o == null ? "-" : o.toString(); 165 } 166 167 168 public static String formatBytes( long bytes ) 169 { 170 String size = ""; 171 if ( bytes > 1024 * 1024 ) 172 size += ( bytes / 1024 / 1024 ) + " MB (" + bytes + " Bytes)"; 173 else if ( bytes > 1024 ) 174 size += ( bytes / 1024 ) + " KB (" + bytes + " Bytes)"; 175 else if ( bytes > 1 ) 176 size += bytes + " Bytes"; 177 else 178 size += bytes + " Byte"; 179 return size; 180 } 181 182 183 public static boolean containsIgnoreCase( Collection c, String s ) 184 { 185 if ( c == null || s == null ) 186 { 187 return false; 188 } 189 190 Iterator it = c.iterator(); 191 while ( it.hasNext() ) 192 { 193 Object o = it.next(); 194 if ( o instanceof String && ( ( String ) o ).equalsIgnoreCase( s ) ) 195 { 196 return true; 197 } 198 } 199 200 return false; 201 } 202 203 204 public static String shorten( String label, int maxLength ) 205 { 206 if ( label == null ) 207 { 208 return null; 209 } 210 if ( maxLength < 3 ) 211 { 212 return "..."; 213 } 214 if ( label.length() > maxLength ) 215 { 216 label = label.substring( 0, maxLength / 2 ) + "..." 217 + label.substring( label.length() - maxLength / 2, label.length() ); 218 219 } 220 StringBuffer sb = new StringBuffer ( maxLength + 3 ); 221 for ( int i = 0; i < label.length(); i++ ) 222 { 223 char c = label.charAt( i ); 224 if ( c > 31 && c < 127 ) 225 sb.append( c ); 226 else 227 sb.append( '.' ); 228 } 229 return sb.toString(); 230 } 231 232 } 233 | Popular Tags |