jaxp/src/com/sun/org/apache/regexp/internal/StreamCharacterIterator.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.InputStream;
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.InputStream 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 StreamCharacterIterator implements CharacterIterator
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
    /** Underlying is */
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
    private final InputStream is;
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 is an InputStream, which is parsed */
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
    public StreamCharacterIterator(InputStream is)
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
        this.is = is;
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
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    /** @return a character at the specified position. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    public char charAt(int pos)
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
            ensure(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
            return buff.charAt(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
            throw new StringIndexOutOfBoundsException(e.getMessage());
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    /** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    public boolean isEnd(int pos)
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        if (buff.length() > pos)
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                ensure(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                return (buff.length() <= pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
            catch (IOException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
                throw new StringIndexOutOfBoundsException(e.getMessage());
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
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    /** Reads n characters from the stream and appends them to the buffer */
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    private int read(int n) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        if (closed)
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        int c;
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        int i = n;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        while (--i >= 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
            c = is.read();
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
            if (c < 0) // EOF
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
                closed = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
            buff.append((char) c);
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        return n - i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    /** Reads rest of the stream. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    private void readAll() throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        while(! closed)
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
            read(1000);
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    /** Reads chars up to the idx */
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    private void ensure(int idx) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
        if (closed)
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        if (idx < buff.length())
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        read(idx + 1 - buff.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
}