OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [regexp/] [CharIndexedInputStream.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* gnu/regexp/CharIndexedInputStream.java
2
   Copyright (C) 1998-2001, 2004, 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.regexp;
39
import java.io.BufferedInputStream;
40
import java.io.IOException;
41
import java.io.InputStream;
42
 
43
// TODO: move(x) shouldn't rely on calling next() x times
44
 
45
class 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
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.