00001 /* 00002 [The "BSD licence"] 00003 Copyright (c) 2005-2008 Terence Parr 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. The name of the author may not be used to endorse or promote products 00015 derived from this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 package org.antlr.runtime; 00029 00030 import java.io.*; 00031 00037 public class ANTLRReaderStream extends ANTLRStringStream { 00038 public static final int READ_BUFFER_SIZE = 1024; 00039 public static final int INITIAL_BUFFER_SIZE = 1024; 00040 00041 public ANTLRReaderStream() { 00042 } 00043 00044 public ANTLRReaderStream(Reader r) throws IOException { 00045 this(r, INITIAL_BUFFER_SIZE, READ_BUFFER_SIZE); 00046 } 00047 00048 public ANTLRReaderStream(Reader r, int size) throws IOException { 00049 this(r, size, READ_BUFFER_SIZE); 00050 } 00051 00052 public ANTLRReaderStream(Reader r, int size, int readChunkSize) throws IOException { 00053 load(r, size, readChunkSize); 00054 } 00055 00056 public void load(Reader r, int size, int readChunkSize) 00057 throws IOException 00058 { 00059 if ( r==null ) { 00060 return; 00061 } 00062 if ( size<=0 ) { 00063 size = INITIAL_BUFFER_SIZE; 00064 } 00065 if ( readChunkSize<=0 ) { 00066 readChunkSize = READ_BUFFER_SIZE; 00067 } 00068 // System.out.println("load "+size+" in chunks of "+readChunkSize); 00069 try { 00070 // alloc initial buffer size. 00071 data = new char[size]; 00072 // read all the data in chunks of readChunkSize 00073 int numRead=0; 00074 int p = 0; 00075 do { 00076 if ( p+readChunkSize > data.length ) { // overflow? 00077 // System.out.println("### overflow p="+p+", data.length="+data.length); 00078 char[] newdata = new char[data.length*2]; // resize 00079 System.arraycopy(data, 0, newdata, 0, data.length); 00080 data = newdata; 00081 } 00082 numRead = r.read(data, p, readChunkSize); 00083 // System.out.println("read "+numRead+" chars; p was "+p+" is now "+(p+numRead)); 00084 p += numRead; 00085 } while (numRead!=-1); // while not EOF 00086 // set the actual size of the data available; 00087 // EOF subtracted one above in p+=numRead; add one back 00088 super.n = p+1; 00089 //System.out.println("n="+n); 00090 } 00091 finally { 00092 r.close(); 00093 } 00094 } 00095 }
1.5.5