/*
* Copyright 2009
* DedaSys LLC - http://www.dedasys.com
*
* Author: David N. Welton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hecl.files;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.DataInputStream;
import org.hecl.HeclException;
import org.hecl.Interp;
import org.hecl.Thing;
/**
* The HeclFileUtils class provides several utility
* functions that are used both by FileCmds, and elsewhere in Hecl.
*
* @author David N. Welton
* @version 1.0
*/
public class HeclFileUtils {
/**
* readFile takes a filename and returns the file's
* contents as a Thing.
*
* @param filename a String value
* @return a Thing value
* @exception HeclException if an error occurs
* @exception IOException if an error occurs
*/
public static Thing readFile(String filename) throws HeclException, IOException {
//System.out.println("readFile " + filename); // debug
File realfn = new File(filename).getAbsoluteFile();
// Change by av. The readFileFromDis method seems not read UTF-8 encoded files properly on Windows platform.
//return readFileFromDis(new DataInputStream(new FileInputStream(realfn)));
InputStreamReader eingabestrom;
eingabestrom = new InputStreamReader(new FileInputStream(realfn), "UTF-8");
BufferedReader eingabe = new BufferedReader(eingabestrom);
StringBuffer sb = new StringBuffer(1024);
String zeile;
boolean isEOF = false;
do {
zeile = eingabe.readLine();
if (zeile != null) {
sb.append(zeile);
sb.append("\n");
}
else isEOF = true;
} while (isEOF == false);
eingabe.close();
eingabestrom.close();
return new Thing(sb.toString());
}
/**
* readFileFromDis, given a DataInputStream, reads
* from the stream until no more can be read, and returns the
* results as a Thing.
*
* @param dis a DataInputStream value
* @return a Thing value
* @exception IOException if an error occurs
*/
public static Thing readFileFromDis(DataInputStream dis) throws IOException {
//System.out.println("readFileFromDis " + dis.toString()); // debug
int bsize = 1024;
byte[] buf = new byte[bsize];
byte[] acc = null;
byte[] oldacc = null;
int len = 0;
int pos = 0;
int i = 1;
while ((len = dis.read(buf)) > -1) {
oldacc = acc;
acc = new byte[i * bsize];
if (oldacc != null) {
System.arraycopy(oldacc, 0, acc, 0, (i-1) * bsize);
}
System.arraycopy(buf, 0, acc, pos, len);
pos += len;
i ++;
}
return new Thing(new String(acc, 0, pos));
}
/**
* The sourceFile method takes a file name. It opens
* that file, reads the contents, and evaluates them with the
* provided interpreter.
*
* @param interp an Interp value
* @param filename a String value
* @exception HeclException if an error occurs
*/
public static void sourceFile(Interp interp, String filename)
throws HeclException {
interp.currentFile = new Thing(filename);
try {
interp.eval(readFile(filename));
} catch (HeclException he) {
throw he;
} catch (Exception e) {
throw new HeclException("Error while running '" + filename + "': " + e.toString());
}
}
}
Ads help maintain this website.