jaxp/src/com/sun/org/apache/regexp/internal/ReaderCharacterIterator.java
author ehelin
Mon, 31 Mar 2014 14:02:40 +0200
changeset 23544 e6362a5ba011
parent 12457 c348e06f0e82
permissions -rw-r--r--
8033251: Use DWARF debug symbols for Linux 32-bit as default Reviewed-by: dcubed, dholmes, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.regexp.internal;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import java.io.Reader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
 * Encapsulates java.io.Reader as CharacterIterator
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 * @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
public final class ReaderCharacterIterator implements CharacterIterator
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
    /** Underlying reader */
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
    private final Reader reader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
    /** Buffer of read chars */
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
    private final StringBuffer buff;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
    /** read end? */
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
    private boolean closed;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
    /** @param reader a Reader, which is parsed */
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
    public ReaderCharacterIterator(Reader reader)
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
        this.reader = reader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
        this.buff = new StringBuffer(512);
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
        this.closed = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    /** @return a substring */
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    public String substring(int beginIndex, int endIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
            ensure(endIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
            return buff.toString().substring(beginIndex, endIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
            throw new StringIndexOutOfBoundsException(e.getMessage());
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    /** @return a substring */
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    public String substring(int beginIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
            readAll();
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
            return buff.toString().substring(beginIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
            throw new StringIndexOutOfBoundsException(e.getMessage());
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    /** @return a character at the specified position. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    public char charAt(int pos)
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
            ensure(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
            return buff.charAt(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
            throw new StringIndexOutOfBoundsException(e.getMessage());
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    /** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    public boolean isEnd(int pos)
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        if (buff.length() > pos)
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
                ensure(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                return (buff.length() <= pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
            catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
                throw new StringIndexOutOfBoundsException(e.getMessage());
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    /** Reads n characters from the stream and appends them to the buffer */
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    private int read(int n) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        if (closed)
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
            return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        char[] c = new char[n];
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        int count = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        int read = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        do
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
            read = reader.read(c);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
            if (read < 0) // EOF
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
                closed = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
            count += read;
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
            buff.append(c, 0, read);
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        while (count < n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        return count;
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    /** Reads rest of the stream. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    private void readAll() throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        while(! closed)
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
            read(1000);
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    /** Reads chars up to the idx */
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    private void ensure(int idx) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        if (closed)
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        if (idx < buff.length())
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        read(idx + 1 - buff.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
}