1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
13 import java.io.BufferedInputStream;
14 import java.io.ByteArrayInputStream;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.util.Locale;
21 import java.util.MissingResourceException;
22 import java.util.ResourceBundle;
23 import java.util.zip.ZipEntry;
24 import java.util.zip.ZipFile;
26 import net.sourceforge.phpdt.core.compiler.CharOperation;
30 public static String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
31 public static char[] LINE_SEPARATOR_CHARS = LINE_SEPARATOR.toCharArray();
32 public final static char[] SUFFIX_class = ".class".toCharArray(); //$NON-NLS-1$
33 public final static char[] SUFFIX_CLASS = ".CLASS".toCharArray(); //$NON-NLS-1$
34 public final static char[] SUFFIX_java = ".java".toCharArray(); //$NON-NLS-1$
35 public final static char[] SUFFIX_JAVA = ".JAVA".toCharArray(); //$NON-NLS-1$
36 public final static char[] SUFFIX_jar = ".jar".toCharArray(); //$NON-NLS-1$
37 public final static char[] SUFFIX_JAR = ".JAR".toCharArray(); //$NON-NLS-1$
38 public final static char[] SUFFIX_zip = ".zip".toCharArray(); //$NON-NLS-1$
39 public final static char[] SUFFIX_ZIP = ".ZIP".toCharArray(); //$NON-NLS-1$
41 private final static char[] DOUBLE_QUOTES = "''".toCharArray(); //$NON-NLS-1$
42 private final static char[] SINGLE_QUOTE = "'".toCharArray(); //$NON-NLS-1$
43 private static final int DEFAULT_READING_SIZE = 8192;
45 /* Bundle containing messages */
46 protected static ResourceBundle bundle;
47 private final static String bundleName =
48 "net.sourceforge.phpdt.internal.compiler.util.messages"; //$NON-NLS-1$
53 * Lookup the message with the given ID in this catalog and bind its
54 * substitution locations with the given strings.
56 public static String bind(String id, String binding1, String binding2) {
57 return bind(id, new String[] { binding1, binding2 });
60 * Lookup the message with the given ID in this catalog and bind its
61 * substitution locations with the given string.
63 public static String bind(String id, String binding) {
64 return bind(id, new String[] { binding });
67 * Lookup the message with the given ID in this catalog and bind its
68 * substitution locations with the given string values.
70 public static String bind(String id, String[] bindings) {
72 return "No message available"; //$NON-NLS-1$
73 String message = null;
75 message = bundle.getString(id);
76 } catch (MissingResourceException e) {
77 // If we got an exception looking for the message, fail gracefully by just returning
78 // the id we were looking for. In most cases this is semi-informative so is not too bad.
79 return "Missing message: " + id + " in: " + bundleName; //$NON-NLS-2$ //$NON-NLS-1$
81 // for compatibility with MessageFormat which eliminates double quotes in original message
82 char[] messageWithNoDoubleQuotes =
83 CharOperation.replace(message.toCharArray(), DOUBLE_QUOTES, SINGLE_QUOTE);
84 message = new String(messageWithNoDoubleQuotes);
89 int length = message.length();
92 StringBuffer output = new StringBuffer(80);
94 if ((end = message.indexOf('{', start)) > -1) {
95 output.append(message.substring(start + 1, end));
96 if ((start = message.indexOf('}', end)) > -1) {
99 index = Integer.parseInt(message.substring(end + 1, start));
100 output.append(bindings[index]);
101 } catch (NumberFormatException nfe) {
102 output.append(message.substring(end + 1, start + 1));
103 } catch (ArrayIndexOutOfBoundsException e) {
104 output.append("{missing " + Integer.toString(index) + "}"); //$NON-NLS-2$ //$NON-NLS-1$
107 output.append(message.substring(end, length));
111 output.append(message.substring(start + 1, length));
115 return output.toString();
118 * Lookup the message with the given ID in this catalog
120 public static String bind(String id) {
121 return bind(id, (String[]) null);
124 * Creates a NLS catalog for the given locale.
126 public static void relocalize() {
128 bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());
129 } catch(MissingResourceException e) {
130 System.out.println("Missing resource : " + bundleName.replace('.', '/') + ".properties for locale " + Locale.getDefault()); //$NON-NLS-1$//$NON-NLS-2$
135 * Returns the given bytes as a char array using a given encoding (null means platform default).
137 public static char[] bytesToChar(byte[] bytes, String encoding) throws IOException {
139 return getInputStreamAsCharArray(new ByteArrayInputStream(bytes), bytes.length, encoding);
143 * Returns the contents of the given file as a byte array.
144 * @throws IOException if a problem occured reading the file.
146 public static byte[] getFileByteContent(File file) throws IOException {
147 InputStream stream = null;
149 stream = new BufferedInputStream(new FileInputStream(file));
150 return getInputStreamAsByteArray(stream, (int) file.length());
152 if (stream != null) {
155 } catch (IOException e) {
161 * Returns the contents of the given file as a char array.
162 * When encoding is null, then the platform default one is used
163 * @throws IOException if a problem occured reading the file.
165 public static char[] getFileCharContent(File file, String encoding) throws IOException {
166 InputStream stream = null;
168 stream = new BufferedInputStream(new FileInputStream(file));
169 return Util.getInputStreamAsCharArray(stream, (int) file.length(), encoding);
171 if (stream != null) {
174 } catch (IOException e) {
180 * Returns the given input stream's contents as a byte array.
181 * If a length is specified (ie. if length != -1), only length bytes
182 * are returned. Otherwise all bytes in the stream are returned.
183 * Note this doesn't close the stream.
184 * @throws IOException if a problem occured reading the stream.
186 public static byte[] getInputStreamAsByteArray(InputStream stream, int length)
190 contents = new byte[0];
191 int contentsLength = 0;
194 int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K
196 // resize contents if needed
197 if (contentsLength + amountRequested > contents.length) {
201 contents = new byte[contentsLength + amountRequested],
206 // read as many bytes as possible
207 amountRead = stream.read(contents, contentsLength, amountRequested);
209 if (amountRead > 0) {
210 // remember length of contents
211 contentsLength += amountRead;
213 } while (amountRead != -1);
215 // resize contents if necessary
216 if (contentsLength < contents.length) {
220 contents = new byte[contentsLength],
225 contents = new byte[length];
228 while ((readSize != -1) && (len != length)) {
230 // We record first the read size. In this case len is the actual read size.
232 readSize = stream.read(contents, len, length - len);
239 * Returns the given input stream's contents as a character array.
240 * If a length is specified (ie. if length != -1), only length chars
241 * are returned. Otherwise all chars in the stream are returned.
242 * Note this doesn't close the stream.
243 * @throws IOException if a problem occured reading the stream.
245 public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding)
247 InputStreamReader reader = null;
248 reader = encoding == null
249 ? new InputStreamReader(stream)
250 : new InputStreamReader(stream, encoding);
253 contents = CharOperation.NO_CHAR;
254 int contentsLength = 0;
257 int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE); // read at least 8K
259 // resize contents if needed
260 if (contentsLength + amountRequested > contents.length) {
264 contents = new char[contentsLength + amountRequested],
269 // read as many chars as possible
270 amountRead = reader.read(contents, contentsLength, amountRequested);
272 if (amountRead > 0) {
273 // remember length of contents
274 contentsLength += amountRead;
276 } while (amountRead != -1);
278 // resize contents if necessary
279 if (contentsLength < contents.length) {
283 contents = new char[contentsLength],
288 contents = new char[length];
291 while ((readSize != -1) && (len != length)) {
293 // We record first the read size. In this case len is the actual read size.
295 readSize = reader.read(contents, len, length - len);
298 // Now we need to resize in case the default encoding used more than one byte for each
301 System.arraycopy(contents, 0, (contents = new char[len]), 0, len);
308 * Returns the contents of the given zip entry as a byte array.
309 * @throws IOException if a problem occured reading the zip entry.
311 public static byte[] getZipEntryByteContent(ZipEntry ze, ZipFile zip)
314 InputStream stream = null;
316 stream = new BufferedInputStream(zip.getInputStream(ze));
317 return getInputStreamAsByteArray(stream, (int) ze.getSize());
319 if (stream != null) {
322 } catch (IOException e) {
328 * Returns true iff str.toLowerCase().endsWith(".jar") || str.toLowerCase().endsWith(".zip")
329 * implementation is not creating extra strings.
331 public final static boolean isArchiveFileName(String name) {
332 int nameLength = name == null ? 0 : name.length();
333 int suffixLength = SUFFIX_JAR.length;
334 if (nameLength < suffixLength) return false;
336 // try to match as JAR file
337 for (int i = 0; i < suffixLength; i++) {
338 char c = name.charAt(nameLength - i - 1);
339 int suffixIndex = suffixLength - i - 1;
340 if (c != SUFFIX_jar[suffixIndex] && c != SUFFIX_JAR[suffixIndex]) {
342 // try to match as ZIP file
343 suffixLength = SUFFIX_ZIP.length;
344 if (nameLength < suffixLength) return false;
345 for (int j = 0; j < suffixLength; j++) {
346 c = name.charAt(nameLength - j - 1);
347 suffixIndex = suffixLength - j - 1;
348 if (c != SUFFIX_zip[suffixIndex] && c != SUFFIX_ZIP[suffixIndex]) return false;
356 * Returns true iff str.toLowerCase().endsWith(".class")
357 * implementation is not creating extra strings.
359 public final static boolean isClassFileName(String name) {
360 int nameLength = name == null ? 0 : name.length();
361 int suffixLength = SUFFIX_CLASS.length;
362 if (nameLength < suffixLength) return false;
364 for (int i = 0; i < suffixLength; i++) {
365 char c = name.charAt(nameLength - i - 1);
366 int suffixIndex = suffixLength - i - 1;
367 if (c != SUFFIX_class[suffixIndex] && c != SUFFIX_CLASS[suffixIndex]) return false;
372 * Returns true iff str.toLowerCase().endsWith(".java")
373 * implementation is not creating extra strings.
375 public final static boolean isJavaFileName(String name) {
376 int nameLength = name == null ? 0 : name.length();
377 int suffixLength = SUFFIX_JAVA.length;
378 if (nameLength < suffixLength) return false;
380 for (int i = 0; i < suffixLength; i++) {
381 char c = name.charAt(nameLength - i - 1);
382 int suffixIndex = suffixLength - i - 1;
383 if (c != SUFFIX_java[suffixIndex] && c != SUFFIX_JAVA[suffixIndex]) return false;