1/*
2 [The "BSD license"]
3 Copyright (c) 2005-2009 Terence Parr
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in the
13     documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15     derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.antlr.runtime;
29
30import java.io.*;
31
32/** Vacuum all input from a Reader and then treat it like a StringStream.
33 *  Manage the buffer manually to avoid unnecessary data copying.
34 *
35 *  If you need encoding, use ANTLRInputStream.
36 */
37public class ANTLRReaderStream extends ANTLRStringStream {
38	public static final int READ_BUFFER_SIZE = 1024;
39	public static final int INITIAL_BUFFER_SIZE = 1024;
40
41	public ANTLRReaderStream() {
42	}
43
44	public ANTLRReaderStream(Reader r) throws IOException {
45		this(r, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE);
46	}
47
48	public ANTLRReaderStream(Reader r, int size) throws IOException {
49		this(r, size, READ_BUFFER_SIZE);
50	}
51
52	public ANTLRReaderStream(Reader r, int size, int readChunkSize) throws IOException {
53		load(r, size, readChunkSize);
54	}
55
56	public void load(Reader r, int size, int readChunkSize)
57		throws IOException
58	{
59		if ( r==null ) {
60			return;
61		}
62		if ( size<=0 ) {
63			size = INITIAL_BUFFER_SIZE;
64		}
65		if ( readChunkSize<=0 ) {
66			readChunkSize = READ_BUFFER_SIZE;
67		}
68		// System.out.println("load "+size+" in chunks of "+readChunkSize);
69		try {
70			// alloc initial buffer size.
71			data = new char[size];
72			// read all the data in chunks of readChunkSize
73			int numRead=0;
74			int p = 0;
75			do {
76				if ( p+readChunkSize > data.length ) { // overflow?
77					// System.out.println("### overflow p="+p+", data.length="+data.length);
78					char[] newdata = new char[data.length*2]; // resize
79					System.arraycopy(data, 0, newdata, 0, data.length);
80					data = newdata;
81				}
82				numRead = r.read(data, p, readChunkSize);
83				// System.out.println("read "+numRead+" chars; p was "+p+" is now "+(p+numRead));
84				p += numRead;
85			} while (numRead!=-1); // while not EOF
86			// set the actual size of the data available;
87			// EOF subtracted one above in p+=numRead; add one back
88			super.n = p+1;
89			//System.out.println("n="+n);
90		}
91		finally {
92			r.close();
93		}
94	}
95}
96