Re-factor loading profile in the streams so that I can reuse that load elsewhere. handy function.

This commit is contained in:
Terence Parr 2014-06-28 10:46:25 -07:00 committed by Sam Harwell
parent a07d523c81
commit 31aa7bf5c9
2 changed files with 41 additions and 28 deletions

View File

@ -29,11 +29,9 @@
*/ */
package org.antlr.v4.runtime; package org.antlr.v4.runtime;
import java.io.File; import org.antlr.v4.runtime.misc.Utils;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/** /**
* This is an {@link ANTLRInputStream} that is loaded from a file all at once * This is an {@link ANTLRInputStream} that is loaded from a file all at once
@ -54,29 +52,8 @@ public class ANTLRFileStream extends ANTLRInputStream {
public void load(String fileName, String encoding) public void load(String fileName, String encoding)
throws IOException throws IOException
{ {
if ( fileName==null ) { data = Utils.readFile(fileName, encoding);
return; this.n = data.length;
}
File f = new File(fileName);
int size = (int)f.length();
InputStreamReader isr;
FileInputStream fis = new FileInputStream(fileName);
if ( encoding!=null ) {
isr = new InputStreamReader(fis, encoding);
}
else {
isr = new InputStreamReader(fis);
}
try {
data = new char[size];
n = isr.read(data);
if (n < data.length) {
data = Arrays.copyOf(data, n);
}
}
finally {
isr.close();
}
} }
@Override @Override

View File

@ -30,13 +30,17 @@
package org.antlr.v4.runtime.misc; package org.antlr.v4.runtime.misc;
import java.awt.*; import java.awt.Window;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer; import java.io.Writer;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -104,6 +108,38 @@ public class Utils {
} }
} }
public static char[] readFile(String fileName) throws IOException {
return readFile(fileName, null);
}
public static char[] readFile(String fileName, String encoding) throws IOException {
if ( fileName==null ) {
return null;
}
File f = new File(fileName);
int size = (int)f.length();
InputStreamReader isr;
FileInputStream fis = new FileInputStream(fileName);
if ( encoding!=null ) {
isr = new InputStreamReader(fis, encoding);
}
else {
isr = new InputStreamReader(fis);
}
char[] data = null;
try {
data = new char[size];
int n = isr.read(data);
if (n < data.length) {
data = Arrays.copyOf(data, n);
}
}
finally {
isr.close();
}
return data;
}
public static void waitForClose(final Window window) throws InterruptedException { public static void waitForClose(final Window window) throws InterruptedException {
final Object lock = new Object(); final Object lock = new Object();