1.1 --- a/src/com/sun/org/apache/regexp/internal/StreamCharacterIterator.java Sat Oct 24 16:18:47 2020 +0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,161 +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.InputStream; 1.27 -import java.io.IOException; 1.28 - 1.29 -/** 1.30 - * Encapsulates java.io.InputStream as CharacterIterator. 1.31 - * 1.32 - * @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a> 1.33 - */ 1.34 -public final class StreamCharacterIterator implements CharacterIterator 1.35 -{ 1.36 - /** Underlying is */ 1.37 - private final InputStream is; 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 is an InputStream, which is parsed */ 1.46 - public StreamCharacterIterator(InputStream is) 1.47 - { 1.48 - this.is = is; 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 - 1.82 - /** @return a character at the specified position. */ 1.83 - public char charAt(int pos) 1.84 - { 1.85 - try 1.86 - { 1.87 - ensure(pos); 1.88 - return buff.charAt(pos); 1.89 - } 1.90 - catch (IOException e) 1.91 - { 1.92 - throw new StringIndexOutOfBoundsException(e.getMessage()); 1.93 - } 1.94 - } 1.95 - 1.96 - /** @return <tt>true</tt> iff if the specified index is after the end of the character stream */ 1.97 - public boolean isEnd(int pos) 1.98 - { 1.99 - if (buff.length() > pos) 1.100 - { 1.101 - return false; 1.102 - } 1.103 - else 1.104 - { 1.105 - try 1.106 - { 1.107 - ensure(pos); 1.108 - return (buff.length() <= pos); 1.109 - } 1.110 - catch (IOException e) 1.111 - { 1.112 - throw new StringIndexOutOfBoundsException(e.getMessage()); 1.113 - } 1.114 - } 1.115 - } 1.116 - 1.117 - /** Reads n characters from the stream and appends them to the buffer */ 1.118 - private int read(int n) throws IOException 1.119 - { 1.120 - if (closed) 1.121 - { 1.122 - return 0; 1.123 - } 1.124 - 1.125 - int c; 1.126 - int i = n; 1.127 - while (--i >= 0) 1.128 - { 1.129 - c = is.read(); 1.130 - if (c < 0) // EOF 1.131 - { 1.132 - closed = true; 1.133 - break; 1.134 - } 1.135 - buff.append((char) c); 1.136 - } 1.137 - return n - i; 1.138 - } 1.139 - 1.140 - /** Reads rest of the stream. */ 1.141 - private void readAll() throws IOException 1.142 - { 1.143 - while(! closed) 1.144 - { 1.145 - read(1000); 1.146 - } 1.147 - } 1.148 - 1.149 - /** Reads chars up to the idx */ 1.150 - private void ensure(int idx) throws IOException 1.151 - { 1.152 - if (closed) 1.153 - { 1.154 - return; 1.155 - } 1.156 - 1.157 - if (idx < buff.length()) 1.158 - { 1.159 - return; 1.160 - } 1.161 - 1.162 - read(idx + 1 - buff.length()); 1.163 - } 1.164 -}