1 package org.apache.oro.text.awk; 2 3 59 60 import java.io.*; 61 import org.apache.oro.text.regex.*; 62 63 92 public final class AwkStreamInput { 93 static final int _DEFAULT_BUFFER_INCREMENT = 2048; 94 private Reader __searchStream; 95 private int __bufferIncrementUnit; 96 boolean _endOfStreamReached; 97 int _bufferSize, _bufferOffset, _currentOffset; 99 char[] _buffer; 100 101 105 AwkStreamInput() { 106 _currentOffset = 0; 107 } 108 109 110 120 public AwkStreamInput(Reader input, int bufferIncrement) { 121 __searchStream = input; 122 __bufferIncrementUnit = bufferIncrement; 123 _buffer = new char[bufferIncrement]; 124 _bufferOffset = _bufferSize = _currentOffset = 0; 125 _endOfStreamReached = false; 126 } 127 128 129 136 public AwkStreamInput(Reader input) { 137 this(input, _DEFAULT_BUFFER_INCREMENT); 138 } 139 140 int _reallocate(int initialOffset) throws IOException { 142 int offset, bytesRead; 143 char[] tmpBuffer; 144 145 if(_endOfStreamReached) 146 return _bufferSize; 147 148 offset = _bufferSize - initialOffset; 149 tmpBuffer = new char[offset + __bufferIncrementUnit]; 150 151 bytesRead = 152 __searchStream.read(tmpBuffer, offset, __bufferIncrementUnit); 153 154 if(bytesRead <= 0){ 155 _endOfStreamReached = true; 156 160 if(bytesRead == 0) 161 throw new IOException("read from input stream returned 0 bytes."); 162 return _bufferSize; 163 } else { 164 _bufferOffset += initialOffset; 165 _bufferSize = offset + bytesRead; 166 167 System.arraycopy(_buffer, initialOffset, tmpBuffer, 0, offset); 168 _buffer = tmpBuffer; 169 } 170 171 return offset; 172 } 173 174 boolean read() throws IOException { 175 _bufferOffset+=_bufferSize; 176 _bufferSize = __searchStream.read(_buffer); 177 _endOfStreamReached = (_bufferSize == -1); 178 return (!_endOfStreamReached); 179 } 180 181 public boolean endOfStream() { return _endOfStreamReached; } 182 183 } 184 | Popular Tags |