Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/memoire/re/RECharIndexedInputStream.java


1   
2   
3   package com.memoire.re;
4   import com.memoire.re.*;
5   
6   /*
7    *  gnu/regexp/RECharIndexedReader.java
8    *  Copyright (C) 1998 Wes Biggs
9    *
10   *  This library is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU Library General Public License as published
12   *  by the Free Software Foundation; either version 2 of the License, or
13   *  (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   *  GNU Library General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Library General Public License
21   *  along with this program; if not, write to the Free Software
22   *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23   */
24  
25  
26  import java.io.InputStream;
27  import java.io.BufferedInputStream;
28  import java.io.IOException;
29  
30  // FIXME: Integer.MAX_VALUE is a hack
31  // TODO: move(x) shouldn't rely on calling next() x times
32  
33  class RECharIndexedInputStream implements RECharIndexed {
34    private static final int BUFFER_INCREMENT = 1024;
35  
36    private BufferedInputStream br;
37    private int m_index, m_end, m_bufsize;
38    private char cached;
39    
40    RECharIndexedInputStream(InputStream str, int index) {
41      if (str instanceof BufferedInputStream) br = (BufferedInputStream) str;
42      else br = new BufferedInputStream(str,BUFFER_INCREMENT);
43      m_bufsize = BUFFER_INCREMENT;
44      m_index = -1;
45      m_end = Integer.MAX_VALUE; // end is unknown
46      next();
47      if (index > 0) move(index);
48    }
49  
50    private boolean next() {
51      if (m_end == 1) return false;
52      m_end--; // closer to end
53      try {
54        if (m_index != -1) {
55    br.reset();
56        }
57        int i = br.read();
58        br.mark(m_bufsize);
59        if (i == -1) {
60    m_end = 1;
61    cached = RECharIndexed.OUT_OF_BOUNDS;
62    return false;
63        }
64        cached = (char) i;
65        m_index = 1;
66      } catch (IOException e) { 
67        e.printStackTrace();
68        cached = RECharIndexed.OUT_OF_BOUNDS;
69        return false; 
70      }
71      return true;
72    }
73  
74    public char charAt(int index) {
75      if (index == 0) return cached;
76      if (index >= m_end) return RECharIndexed.OUT_OF_BOUNDS;
77      if (index >= m_bufsize) {
78        // Allocate more space in the buffer.
79        try {
80    while (m_bufsize <= index) m_bufsize += BUFFER_INCREMENT;
81    br.reset();
82    br.mark(m_bufsize);
83    br.skip(index-1);
84        } catch (IOException e) { }
85      } else if (m_index != index) {
86        try {
87    br.reset();
88    br.skip(index-1);
89        } catch (IOException e) { }
90      }
91      char ch = RECharIndexed.OUT_OF_BOUNDS;
92  
93      try {
94        int i = br.read();
95        m_index = index+1; // m_index is index of next pos relative to charAt(0)
96        if (i == -1) {
97    // set flag that next should fail next time?
98    m_end = index;
99    return ch;
100       }
101       ch = (char) i;
102     } catch (IOException ie) { }
103 
104     return ch;
105   }
106 
107   public boolean move(int index) {
108     // move read position [index] clicks from 'charAt(0)'
109     boolean retval = true;
110     while (retval && (index-- > 0)) retval = next();
111     return retval;
112   }
113 
114   public boolean isValid() {
115     return (cached != RECharIndexed.OUT_OF_BOUNDS);
116   }
117 }
118