13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/* 23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"] 33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr 43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved. 53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without 73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions 83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met: 93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright 103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer. 113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright 123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein notice, this list of conditions and the following disclaimer in the 133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein documentation and/or other materials provided with the distribution. 143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products 153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein derived from this software without specific prior written permission. 163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime; 293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.io.*; 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** Vacuum all input from a Reader and then treat it like a StringStream. 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * Manage the buffer manually to avoid unnecessary data copying. 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * If you need encoding, use ANTLRInputStream. 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class ANTLRReaderStream extends ANTLRStringStream { 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public static final int READ_BUFFER_SIZE = 1024; 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public static final int INITIAL_BUFFER_SIZE = 1024; 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ANTLRReaderStream() { 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ANTLRReaderStream(Reader r) throws IOException { 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this(r, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE); 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ANTLRReaderStream(Reader r, int size) throws IOException { 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this(r, size, READ_BUFFER_SIZE); 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ANTLRReaderStream(Reader r, int size, int readChunkSize) throws IOException { 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein load(r, size, readChunkSize); 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void load(Reader r, int size, int readChunkSize) 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein throws IOException 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein { 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( r==null ) { 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return; 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( size<=0 ) { 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein size = INITIAL_BUFFER_SIZE; 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( readChunkSize<=0 ) { 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein readChunkSize = READ_BUFFER_SIZE; 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // System.out.println("load "+size+" in chunks of "+readChunkSize); 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein try { 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // alloc initial buffer size. 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein data = new char[size]; 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // read all the data in chunks of readChunkSize 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int numRead=0; 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int p = 0; 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein do { 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( p+readChunkSize > data.length ) { // overflow? 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // System.out.println("### overflow p="+p+", data.length="+data.length); 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein char[] newdata = new char[data.length*2]; // resize 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein System.arraycopy(data, 0, newdata, 0, data.length); 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein data = newdata; 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein numRead = r.read(data, p, readChunkSize); 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // System.out.println("read "+numRead+" chars; p was "+p+" is now "+(p+numRead)); 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein p += numRead; 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } while (numRead!=-1); // while not EOF 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // set the actual size of the data available; 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // EOF subtracted one above in p+=numRead; add one back 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein super.n = p+1; 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein //System.out.println("n="+n); 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein finally { 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein r.close(); 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 96