1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.InputStreamReader ; 19 import java.io.UnsupportedEncodingException ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipFile ; 22 import org.eclipse.jdt.core.compiler.CharOperation; 23 24 public class Util implements SuffixConstants { 25 26 public interface Displayable { 27 String displayString(Object o); 28 } 29 30 private static final int DEFAULT_READING_SIZE = 8192; 31 public final static String UTF_8 = "UTF-8"; public static final String LINE_SEPARATOR = System.getProperty("line.separator"); 34 public static final String EMPTY_STRING = new String (CharOperation.NO_CHAR); 35 public static final int[] EMPTY_INT_ARRAY= new int[0]; 36 37 40 public static char[] bytesToChar(byte[] bytes, String encoding) throws IOException { 41 42 return getInputStreamAsCharArray(new ByteArrayInputStream (bytes), bytes.length, encoding); 43 44 } 45 49 public static byte[] getFileByteContent(File file) throws IOException { 50 InputStream stream = null; 51 try { 52 stream = new FileInputStream (file); 53 return getInputStreamAsByteArray(stream, (int) file.length()); 54 } finally { 55 if (stream != null) { 56 try { 57 stream.close(); 58 } catch (IOException e) { 59 } 61 } 62 } 63 } 64 69 public static char[] getFileCharContent(File file, String encoding) throws IOException { 70 InputStream stream = null; 71 try { 72 stream = new FileInputStream (file); 73 return getInputStreamAsCharArray(stream, (int) file.length(), encoding); 74 } finally { 75 if (stream != null) { 76 try { 77 stream.close(); 78 } catch (IOException e) { 79 } 81 } 82 } 83 } 84 102 109 public static byte[] getInputStreamAsByteArray(InputStream stream, int length) 110 throws IOException { 111 byte[] contents; 112 if (length == -1) { 113 contents = new byte[0]; 114 int contentsLength = 0; 115 int amountRead = -1; 116 do { 117 int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); 119 if (contentsLength + amountRequested > contents.length) { 121 System.arraycopy( 122 contents, 123 0, 124 contents = new byte[contentsLength + amountRequested], 125 0, 126 contentsLength); 127 } 128 129 amountRead = stream.read(contents, contentsLength, amountRequested); 131 132 if (amountRead > 0) { 133 contentsLength += amountRead; 135 } 136 } while (amountRead != -1); 137 138 if (contentsLength < contents.length) { 140 System.arraycopy( 141 contents, 142 0, 143 contents = new byte[contentsLength], 144 0, 145 contentsLength); 146 } 147 } else { 148 contents = new byte[length]; 149 int len = 0; 150 int readSize = 0; 151 while ((readSize != -1) && (len != length)) { 152 len += readSize; 155 readSize = stream.read(contents, len, length - len); 156 } 157 } 158 159 return contents; 160 } 161 184 190 public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding) 191 throws IOException { 192 InputStreamReader reader = null; 193 try { 194 reader = encoding == null 195 ? new InputStreamReader (stream) 196 : new InputStreamReader (stream, encoding); 197 } catch (UnsupportedEncodingException e) { 198 reader = new InputStreamReader (stream); 200 } 201 char[] contents; 202 int totalRead = 0; 203 if (length == -1) { 204 contents = CharOperation.NO_CHAR; 205 } else { 206 contents = new char[length]; } 209 210 while (true) { 211 int amountRequested; 212 if (totalRead < length) { 213 amountRequested = length - totalRead; 215 } else { 216 int current = reader.read(); 218 if (current < 0) break; 219 220 amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); 222 if (totalRead + 1 + amountRequested > contents.length) 224 System.arraycopy(contents, 0, contents = new char[totalRead + 1 + amountRequested], 0, totalRead); 225 226 contents[totalRead++] = (char) current; } 229 int amountRead = reader.read(contents, totalRead, amountRequested); 231 if (amountRead < 0) break; 232 totalRead += amountRead; 233 } 234 235 int start = 0; 237 if (totalRead > 0 && UTF_8.equals(encoding)) { 238 if (contents[0] == 0xFEFF) { totalRead--; 240 start = 1; 241 } 242 } 243 244 if (totalRead < contents.length) 246 System.arraycopy(contents, start, contents = new char[totalRead], 0, totalRead); 247 248 return contents; 249 } 250 251 public static int getLineNumber(int position, int[] lineEnds, int g, int d) { 252 if (lineEnds == null) 253 return 1; 254 if (d == -1) 255 return 1; 256 int m = g, start; 257 while (g <= d) { 258 m = g + (d - g) /2; 259 if (position < (start = lineEnds[m])) { 260 d = m-1; 261 } else if (position > start) { 262 g = m+1; 263 } else { 264 return m + 1; 265 } 266 } 267 if (position < lineEnds[m]) { 268 return m+1; 269 } 270 return m+2; 271 } 272 273 277 public static byte[] getZipEntryByteContent(ZipEntry ze, ZipFile zip) 278 throws IOException { 279 280 InputStream stream = null; 281 try { 282 stream = zip.getInputStream(ze); 283 if (stream == null) throw new IOException ("Invalid zip entry name : " + ze.getName()); return getInputStreamAsByteArray(stream, (int) ze.getSize()); 285 } finally { 286 if (stream != null) { 287 try { 288 stream.close(); 289 } catch (IOException e) { 290 } 292 } 293 } 294 } 295 296 300 public final static boolean isArchiveFileName(String name) { 301 int nameLength = name == null ? 0 : name.length(); 302 int suffixLength = SUFFIX_JAR.length; 303 if (nameLength < suffixLength) return false; 304 305 for (int i = 0; i < suffixLength; i++) { 307 char c = name.charAt(nameLength - i - 1); 308 int suffixIndex = suffixLength - i - 1; 309 if (c != SUFFIX_jar[suffixIndex] && c != SUFFIX_JAR[suffixIndex]) { 310 311 suffixLength = SUFFIX_ZIP.length; 313 if (nameLength < suffixLength) return false; 314 for (int j = 0; j < suffixLength; j++) { 315 c = name.charAt(nameLength - j - 1); 316 suffixIndex = suffixLength - j - 1; 317 if (c != SUFFIX_zip[suffixIndex] && c != SUFFIX_ZIP[suffixIndex]) return false; 318 } 319 return true; 320 } 321 } 322 return true; 323 } 324 328 public final static boolean isClassFileName(char[] name) { 329 int nameLength = name == null ? 0 : name.length; 330 int suffixLength = SUFFIX_CLASS.length; 331 if (nameLength < suffixLength) return false; 332 333 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) { 334 char c = name[offset + i]; 335 if (c != SUFFIX_class[i] && c != SUFFIX_CLASS[i]) return false; 336 } 337 return true; 338 } 339 343 public final static boolean isClassFileName(String name) { 344 int nameLength = name == null ? 0 : name.length(); 345 int suffixLength = SUFFIX_CLASS.length; 346 if (nameLength < suffixLength) return false; 347 348 for (int i = 0; i < suffixLength; i++) { 349 char c = name.charAt(nameLength - i - 1); 350 int suffixIndex = suffixLength - i - 1; 351 if (c != SUFFIX_class[suffixIndex] && c != SUFFIX_CLASS[suffixIndex]) return false; 352 } 353 return true; 354 } 355 362 public final static boolean isExcluded(char[] path, char[][] inclusionPatterns, char[][] exclusionPatterns, boolean isFolderPath) { 363 if (inclusionPatterns == null && exclusionPatterns == null) return false; 364 365 inclusionCheck: if (inclusionPatterns != null) { 366 for (int i = 0, length = inclusionPatterns.length; i < length; i++) { 367 char[] pattern = inclusionPatterns[i]; 368 char[] folderPattern = pattern; 369 if (isFolderPath) { 370 int lastSlash = CharOperation.lastIndexOf('/', pattern); 371 if (lastSlash != -1 && lastSlash != pattern.length-1){ int star = CharOperation.indexOf('*', pattern, lastSlash); 373 if ((star == -1 374 || star >= pattern.length-1 375 || pattern[star+1] != '*')) { 376 folderPattern = CharOperation.subarray(pattern, 0, lastSlash); 377 } 378 } 379 } 380 if (CharOperation.pathMatch(folderPattern, path, true, '/')) { 381 break inclusionCheck; 382 } 383 } 384 return true; } 386 if (isFolderPath) { 387 path = CharOperation.concat(path, new char[] {'*'}, '/'); 388 } 389 if (exclusionPatterns != null) { 390 for (int i = 0, length = exclusionPatterns.length; i < length; i++) { 391 if (CharOperation.pathMatch(exclusionPatterns[i], path, true, '/')) { 392 return true; 393 } 394 } 395 } 396 return false; 397 } 398 402 public final static boolean isJavaFileName(char[] name) { 403 int nameLength = name == null ? 0 : name.length; 404 int suffixLength = SUFFIX_JAVA.length; 405 if (nameLength < suffixLength) return false; 406 407 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) { 408 char c = name[offset + i]; 409 if (c != SUFFIX_java[i] && c != SUFFIX_JAVA[i]) return false; 410 } 411 return true; 412 } 413 417 public final static boolean isJavaFileName(String name) { 418 int nameLength = name == null ? 0 : name.length(); 419 int suffixLength = SUFFIX_JAVA.length; 420 if (nameLength < suffixLength) return false; 421 422 for (int i = 0; i < suffixLength; i++) { 423 char c = name.charAt(nameLength - i - 1); 424 int suffixIndex = suffixLength - i - 1; 425 if (c != SUFFIX_java[suffixIndex] && c != SUFFIX_JAVA[suffixIndex]) return false; 426 } 427 return true; 428 } 429 430 434 public static final int searchColumnNumber(int[] startLineIndexes, int lineNumber, int position) { 435 switch(lineNumber) { 436 case 1 : 437 return position + 1; 438 case 2: 439 return position - startLineIndexes[0]; 440 default: 441 int line = lineNumber - 2; 442 int length = startLineIndexes.length; 443 if (line >= length) { 444 return position - startLineIndexes[length - 1]; 445 } 446 return position - startLineIndexes[line]; 447 } 448 } 449 454 public static Boolean toBoolean(boolean bool) { 455 if (bool) { 456 return Boolean.TRUE; 457 } else { 458 return Boolean.FALSE; 459 } 460 } 461 464 public static String toString(Object [] objects) { 465 return toString(objects, 466 new Displayable(){ 467 public String displayString(Object o) { 468 if (o == null) return "null"; return o.toString(); 470 } 471 }); 472 } 473 474 477 public static String toString(Object [] objects, Displayable renderer) { 478 if (objects == null) return ""; StringBuffer buffer = new StringBuffer (10); 480 for (int i = 0; i < objects.length; i++){ 481 if (i > 0) buffer.append(", "); buffer.append(renderer.displayString(objects[i])); 483 } 484 return buffer.toString(); 485 } 486 } 487 | Popular Tags |