src/com/sun/org/apache/regexp/internal/ReaderCharacterIterator.java

changeset 2116
aaee9ae4799a
parent 2090
3b8ebb957957
parent 2115
ba503169016f
child 2117
a5f920b6d2b5
     1.1 --- a/src/com/sun/org/apache/regexp/internal/ReaderCharacterIterator.java	Sat Oct 24 16:18:47 2020 +0800
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,164 +0,0 @@
     1.4 -/*
     1.5 - * reserved comment block
     1.6 - * DO NOT REMOVE OR ALTER!
     1.7 - */
     1.8 -/*
     1.9 - * Copyright 1999-2004 The Apache Software Foundation.
    1.10 - *
    1.11 - * Licensed under the Apache License, Version 2.0 (the "License");
    1.12 - * you may not use this file except in compliance with the License.
    1.13 - * You may obtain a copy of the License at
    1.14 - *
    1.15 - *     http://www.apache.org/licenses/LICENSE-2.0
    1.16 - *
    1.17 - * Unless required by applicable law or agreed to in writing, software
    1.18 - * distributed under the License is distributed on an "AS IS" BASIS,
    1.19 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.20 - * See the License for the specific language governing permissions and
    1.21 - * limitations under the License.
    1.22 - */
    1.23 -
    1.24 -package com.sun.org.apache.regexp.internal;
    1.25 -
    1.26 -import java.io.Reader;
    1.27 -import java.io.IOException;
    1.28 -
    1.29 -/**
    1.30 - * Encapsulates java.io.Reader as CharacterIterator
    1.31 - *
    1.32 - * @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
    1.33 - */
    1.34 -public final class ReaderCharacterIterator implements CharacterIterator
    1.35 -{
    1.36 -    /** Underlying reader */
    1.37 -    private final Reader reader;
    1.38 -
    1.39 -    /** Buffer of read chars */
    1.40 -    private final StringBuffer buff;
    1.41 -
    1.42 -    /** read end? */
    1.43 -    private boolean closed;
    1.44 -
    1.45 -    /** @param reader a Reader, which is parsed */
    1.46 -    public ReaderCharacterIterator(Reader reader)
    1.47 -    {
    1.48 -        this.reader = reader;
    1.49 -        this.buff = new StringBuffer(512);
    1.50 -        this.closed = false;
    1.51 -    }
    1.52 -
    1.53 -    /** @return a substring */
    1.54 -    public String substring(int beginIndex, int endIndex)
    1.55 -    {
    1.56 -        try
    1.57 -        {
    1.58 -            ensure(endIndex);
    1.59 -            return buff.toString().substring(beginIndex, endIndex);
    1.60 -        }
    1.61 -        catch (IOException e)
    1.62 -        {
    1.63 -            throw new StringIndexOutOfBoundsException(e.getMessage());
    1.64 -        }
    1.65 -    }
    1.66 -
    1.67 -    /** @return a substring */
    1.68 -    public String substring(int beginIndex)
    1.69 -    {
    1.70 -        try
    1.71 -        {
    1.72 -            readAll();
    1.73 -            return buff.toString().substring(beginIndex);
    1.74 -        }
    1.75 -        catch (IOException e)
    1.76 -        {
    1.77 -            throw new StringIndexOutOfBoundsException(e.getMessage());
    1.78 -        }
    1.79 -    }
    1.80 -
    1.81 -    /** @return a character at the specified position. */
    1.82 -    public char charAt(int pos)
    1.83 -    {
    1.84 -        try
    1.85 -        {
    1.86 -            ensure(pos);
    1.87 -            return buff.charAt(pos);
    1.88 -        }
    1.89 -        catch (IOException e)
    1.90 -        {
    1.91 -            throw new StringIndexOutOfBoundsException(e.getMessage());
    1.92 -        }
    1.93 -    }
    1.94 -
    1.95 -    /** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
    1.96 -    public boolean isEnd(int pos)
    1.97 -    {
    1.98 -        if (buff.length() > pos)
    1.99 -        {
   1.100 -            return false;
   1.101 -        }
   1.102 -        else
   1.103 -        {
   1.104 -            try
   1.105 -            {
   1.106 -                ensure(pos);
   1.107 -                return (buff.length() <= pos);
   1.108 -            }
   1.109 -            catch (IOException e)
   1.110 -            {
   1.111 -                throw new StringIndexOutOfBoundsException(e.getMessage());
   1.112 -            }
   1.113 -        }
   1.114 -    }
   1.115 -
   1.116 -    /** Reads n characters from the stream and appends them to the buffer */
   1.117 -    private int read(int n) throws IOException
   1.118 -    {
   1.119 -        if (closed)
   1.120 -        {
   1.121 -            return 0;
   1.122 -        }
   1.123 -
   1.124 -        char[] c = new char[n];
   1.125 -        int count = 0;
   1.126 -        int read = 0;
   1.127 -
   1.128 -        do
   1.129 -        {
   1.130 -            read = reader.read(c);
   1.131 -            if (read < 0) // EOF
   1.132 -            {
   1.133 -                closed = true;
   1.134 -                break;
   1.135 -            }
   1.136 -            count += read;
   1.137 -            buff.append(c, 0, read);
   1.138 -        }
   1.139 -        while (count < n);
   1.140 -
   1.141 -        return count;
   1.142 -    }
   1.143 -
   1.144 -    /** Reads rest of the stream. */
   1.145 -    private void readAll() throws IOException
   1.146 -    {
   1.147 -        while(! closed)
   1.148 -        {
   1.149 -            read(1000);
   1.150 -        }
   1.151 -    }
   1.152 -
   1.153 -    /** Reads chars up to the idx */
   1.154 -    private void ensure(int idx) throws IOException
   1.155 -    {
   1.156 -        if (closed)
   1.157 -        {
   1.158 -            return;
   1.159 -        }
   1.160 -
   1.161 -        if (idx < buff.length())
   1.162 -        {
   1.163 -            return;
   1.164 -        }
   1.165 -        read(idx + 1 - buff.length());
   1.166 -    }
   1.167 -}

mercurial