001: package org.drools.eclipse.builder;
002:
003: import java.io.BufferedInputStream;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.InputStreamReader;
007:
008: import org.drools.eclipse.DroolsEclipsePlugin;
009: import org.eclipse.core.resources.IFile;
010: import org.eclipse.core.runtime.CoreException;
011: import org.eclipse.core.runtime.IStatus;
012: import org.eclipse.core.runtime.Status;
013:
014: /**
015: * Utility class.
016: *
017: * @author <a href="mailto:kris_verlaenen@hotmail.com">kris verlaenen </a>
018: */
019: public class Util {
020:
021: public static final char[] NO_CHAR = new char[0];
022: private static final int DEFAULT_READING_SIZE = 8192;
023:
024: public static char[] getResourceContentsAsCharArray(IFile file)
025: throws CoreException {
026: String encoding = null;
027: try {
028: encoding = file.getCharset();
029: } catch (CoreException ce) {
030: // do not use any encoding
031: }
032:
033: InputStream stream = null;
034: stream = new BufferedInputStream(file.getContents(true));
035: try {
036: return getInputStreamAsCharArray(stream, -1, encoding);
037: } catch (IOException e) {
038: throw new CoreException(new Status(IStatus.ERROR,
039: DroolsEclipsePlugin.PLUGIN_ID, IStatus.ERROR,
040: "IOException", e));
041: } finally {
042: try {
043: stream.close();
044: } catch (IOException e) {
045: // ignore
046: }
047: }
048: }
049:
050: private static char[] getInputStreamAsCharArray(InputStream stream,
051: int length, String encoding) throws IOException {
052: InputStreamReader reader = null;
053: reader = encoding == null ? new InputStreamReader(stream)
054: : new InputStreamReader(stream, encoding);
055: char[] contents;
056: if (length == -1) {
057: contents = NO_CHAR;
058: int contentsLength = 0;
059: int amountRead = -1;
060: do {
061: int amountRequested = Math.max(stream.available(),
062: DEFAULT_READING_SIZE);
063: if (contentsLength + amountRequested > contents.length) {
064: System.arraycopy(contents, 0,
065: contents = new char[contentsLength
066: + amountRequested], 0,
067: contentsLength);
068: }
069: amountRead = reader.read(contents, contentsLength,
070: amountRequested);
071:
072: if (amountRead > 0) {
073: contentsLength += amountRead;
074: }
075: } while (amountRead != -1);
076:
077: int start = 0;
078: if (contentsLength > 0 && "UTF-8".equals(encoding)) {
079: if (contents[0] == 0xFEFF) {
080: contentsLength--;
081: start = 1;
082: }
083: }
084: if (contentsLength < contents.length) {
085: System.arraycopy(contents, start,
086: contents = new char[contentsLength], 0,
087: contentsLength);
088: }
089: } else {
090: contents = new char[length];
091: int len = 0;
092: int readSize = 0;
093: while ((readSize != -1) && (len != length)) {
094: len += readSize;
095: readSize = reader.read(contents, len, length - len);
096: }
097: int start = 0;
098: if (length > 0 && "UTF-8".equals(encoding)) {
099: if (contents[0] == 0xFEFF) {
100: len--;
101: start = 1;
102: }
103: }
104: if (len != length)
105: System.arraycopy(contents, start,
106: (contents = new char[len]), 0, len);
107: }
108:
109: return contents;
110: }
111: }
|