001: /*******************************************************************************
002: * Copyright (c) 2003, 2004 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jsp;
011:
012: import java.io.*;
013: import java.io.File;
014:
015: public class Util {
016:
017: static char[] getChars(String s) {
018: int l = s.length();
019: char[] cc = new char[l];
020: if (l > 0)
021: s.getChars(0, l, cc, 0);
022: return cc;
023: }
024:
025: static char[] getFileCharContent(File file, String encoding)
026: throws IOException {
027: System.out
028: .println("****jdt.internal.compiler.util.Util.getFileCharContent: " + file + " " + encoding); //$NON-NLS-1$ //$NON-NLS-2$
029: InputStream stream = null;
030: try {
031: stream = new BufferedInputStream(new FileInputStream(file));
032: return Util.getInputStreamAsCharArray(stream, (int) file
033: .length(), encoding);
034: } finally {
035: if (stream != null) {
036: try {
037: stream.close();
038: } catch (IOException e) {
039: // ignore
040: }
041: }
042: }
043: }
044:
045: public static char[] getInputStreamAsCharArray(InputStream stream,
046: int length, String encoding) throws IOException {
047: InputStreamReader reader = (encoding == null) ? new InputStreamReader(
048: stream)
049: : new InputStreamReader(stream, encoding);
050: char[] contents;
051: if (length == -1) {
052: contents = new char[0];
053: int contentsLength = 0;
054: int amountRead = -1;
055: do {
056: int amountRequested = Math
057: .max(stream.available(), 8192); // read at least 8K
058:
059: // resize contents if needed
060: if (contentsLength + amountRequested > contents.length) {
061: System.arraycopy(contents, 0,
062: contents = new char[contentsLength
063: + amountRequested], 0,
064: contentsLength);
065: }
066:
067: // read as many chars as possible
068: amountRead = reader.read(contents, contentsLength,
069: amountRequested);
070:
071: if (amountRead > 0) {
072: // remember length of contents
073: contentsLength += amountRead;
074: }
075: } while (amountRead != -1);
076:
077: // resize contents if necessary
078: if (contentsLength < contents.length) {
079: System.arraycopy(contents, 0,
080: contents = new char[contentsLength], 0,
081: contentsLength);
082: }
083: } else {
084: contents = new char[length];
085: int len = 0;
086: int readSize = 0;
087: while ((readSize != -1) && (len != length)) {
088: // See PR 1FMS89U
089: // We record first the read size. In this case len is the actual read size.
090: len += readSize;
091: readSize = reader.read(contents, len, length - len);
092: }
093: // See PR 1FMS89U
094: // Now we need to resize in case the default encoding used more than one byte for each
095: // character
096: if (len != length)
097: System.arraycopy(contents, 0,
098: (contents = new char[len]), 0, len);
099: }
100:
101: return contents;
102: }
103: }
|