PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build270707/src/edu/vub/util/regexp/CharIndexedInputStream.java

http://ambienttalk.googlecode.com/
Java | 159 lines | 95 code | 20 blank | 44 comment | 32 complexity | 38b02fafe663767fbd4236314b540f9b MD5 | raw file
  1/* gnu/regexp/CharIndexedInputStream.java
  2   Copyright (C) 1998-2001, 2004, 2006 Free Software Foundation, Inc.
  3
  4This file is part of GNU Classpath.
  5
  6GNU Classpath is free software; you can redistribute it and/or modify
  7it under the terms of the GNU General Public License as published by
  8the Free Software Foundation; either version 2, or (at your option)
  9any later version.
 10
 11GNU Classpath is distributed in the hope that it will be useful, but
 12WITHOUT ANY WARRANTY; without even the implied warranty of
 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14General Public License for more details.
 15
 16You should have received a copy of the GNU General Public License
 17along with GNU Classpath; see the file COPYING.  If not, write to the
 18Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 1902110-1301 USA.
 20
 21Linking this library statically or dynamically with other modules is
 22making a combined work based on this library.  Thus, the terms and
 23conditions of the GNU General Public License cover the whole
 24combination.
 25
 26As a special exception, the copyright holders of this library give you
 27permission to link this library with independent modules to produce an
 28executable, regardless of the license terms of these independent
 29modules, and to copy and distribute the resulting executable under
 30terms of your choice, provided that you also meet, for each linked
 31independent module, the terms and conditions of the license of that
 32module.  An independent module is a module which is not derived from
 33or based on this library.  If you modify this library, you may extend
 34this exception to your version of the library, but you are not
 35obligated to do so.  If you do not wish to do so, delete this
 36exception statement from your version. */
 37
 38package edu.vub.util.regexp;
 39import java.io.BufferedInputStream;
 40import java.io.IOException;
 41import java.io.InputStream;
 42
 43// TODO: move(x) shouldn't rely on calling next() x times
 44
 45class CharIndexedInputStream implements CharIndexed {
 46    private static final int BUFFER_INCREMENT = 1024;
 47    private static final int UNKNOWN = Integer.MAX_VALUE; // value for end
 48    
 49    private BufferedInputStream br;
 50
 51    // so that we don't try to reset() right away
 52    private int index = -1;
 53
 54    private int bufsize = BUFFER_INCREMENT;
 55
 56    private int end = UNKNOWN;
 57
 58    private char cached = OUT_OF_BOUNDS;
 59
 60    // Big enough for a \r\n pair
 61    // lookBehind[0] = most recent
 62    // lookBehind[1] = second most recent
 63    private char[] lookBehind = new char[] { OUT_OF_BOUNDS, OUT_OF_BOUNDS }; 
 64    
 65    CharIndexedInputStream(InputStream str, int index) {
 66	if (str instanceof BufferedInputStream) br = (BufferedInputStream) str;
 67	else br = new BufferedInputStream(str,BUFFER_INCREMENT);
 68	next();
 69	if (index > 0) move(index);
 70    }
 71    
 72    private boolean next() {
 73	if (end == 1) return false;
 74	end--; // closer to end
 75
 76	try {
 77	    if (index != -1) {
 78		br.reset();
 79	    }
 80	    int i = br.read();
 81	    br.mark(bufsize);
 82	    if (i == -1) {
 83		end = 1;
 84		cached = OUT_OF_BOUNDS;
 85		return false;
 86	    }
 87	    cached = (char) i;
 88	    index = 1;
 89	} catch (IOException e) { 
 90	    e.printStackTrace();
 91	    cached = OUT_OF_BOUNDS;
 92	    return false; 
 93	}
 94	return true;
 95    }
 96    
 97    public char charAt(int index) {
 98	if (index == 0) {
 99	    return cached;
100	} else if (index >= end) {
101	    return OUT_OF_BOUNDS;
102	} else if (index == -1) {
103	    return lookBehind[0];
104	} else if (index == -2) {
105	    return lookBehind[1];
106	} else if (index < -2) {
107	    return OUT_OF_BOUNDS;
108	} else if (index >= bufsize) {
109	    // Allocate more space in the buffer.
110	    try {
111		while (bufsize <= index) bufsize += BUFFER_INCREMENT;
112		br.reset();
113		br.mark(bufsize);
114		br.skip(index-1);
115	    } catch (IOException e) { }
116	} else if (this.index != index) {
117	    try {
118		br.reset();
119		br.skip(index-1);
120	    } catch (IOException e) { }
121	}
122	char ch = OUT_OF_BOUNDS;
123	
124	try {
125	    int i = br.read();
126	    this.index = index+1; // this.index is index of next pos relative to charAt(0)
127	    if (i == -1) {
128		// set flag that next should fail next time?
129		end = index;
130		return ch;
131	    }
132	    ch = (char) i;
133	} catch (IOException ie) { }
134	
135	return ch;
136    }
137    
138    public boolean move(int index) {
139	// move read position [index] clicks from 'charAt(0)'
140	boolean retval = true;
141	while (retval && (index-- > 0)) retval = next();
142	return retval;
143    }
144    
145    public boolean isValid() {
146	return (cached != OUT_OF_BOUNDS);
147    }
148
149    public CharIndexed lookBehind(int index, int length) {
150	throw new UnsupportedOperationException(
151	    "difficult to look behind for an input stream");
152    }
153
154    public int length() {
155	throw new UnsupportedOperationException(
156	    "difficult to tell the length for an input stream");
157    }
158}
159