[antlr-interest] v3 stream input
Sohail Somani
sohail at taggedtype.net
Sat May 27 09:06:06 PDT 2006
On Sat, 2006-05-27 at 15:09 +0200, Martin Probst wrote:
> >>> CharArrayWriter wr = new CharArrayWriter();
> >>> BufferedWriter bw = new BufferedWriter(wr);
> >>
> >> I think you don't need to have a buffered writer around an im memory
> >> char array, but then again, I'm not that great in the complex
> >> scientific topic that is reading a simple file in Java ;-)
> >
> > The only other way I know of would require reallocating a char
> > array in
> > a loop or converting a Char (using Vector) to a char (as you don't
> > know
> > the length of the file). Of course, an understanding of the complex
> > scientific topic that is reading a simple file in Java still escapes
> > me :)
>
> Well, then I'd guess this is what you want:
> ...
I think this satisfies the charset issue and does the io the way I
wanted to do it in the first place, thanks!
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.BufferedWriter;
import java.lang.StringBuilder;
import org.antlr.runtime.ANTLRStringStream;
public class ANTLRInputStream extends ANTLRStringStream {
public ANTLRInputStream(InputStreamReader reader) throws IOException
{
load(reader);
}
public ANTLRInputStream(InputStream stream) throws IOException
{
load(new InputStreamReader(stream));
}
public void load(InputStreamReader reader) throws IOException
{
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
char [] buf = new char[4096];
int bytes=0;
while((bytes=br.read(buf))!=-1)
{
sb.append(buf,0,bytes);
}
br.close();
data = sb.toString().toCharArray();
}
}
More information about the antlr-interest
mailing list