/*******************************************************************************
* Copyright 2013
* Ubiquitous Knowledge Processing (UKP) Lab
* Technische Universität Darmstadt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package de.tudarmstadt.ukp.csniper.webapp.search.cqp;
import java.io.IOException;
import java.io.Reader;
public class TimeoutReader extends Reader
{
private Reader delegate;
private int timeout = 15 * 1000;
public TimeoutReader(final Reader aDelegate)
{
delegate = aDelegate;
}
public int getTimeout()
{
return timeout;
}
public void setTimeout(int aTimeout)
{
timeout = aTimeout;
}
public String readLine() throws IOException
{
long lastRead = System.currentTimeMillis();
long now = System.currentTimeMillis();
StringBuilder buffer = new StringBuilder();
while (now - lastRead < timeout) {
now = System.currentTimeMillis();
if (delegate.ready()) {
int ch = delegate.read();
if (ch == '\n') {
if(buffer.charAt(buffer.length()-1) == '\r') {
buffer.deleteCharAt(buffer.length()-1);
}
return buffer.toString();
}
else {
buffer.appendCodePoint(ch);
}
lastRead = now;
}
else {
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
// Nothing to do.
}
}
}
throw new IOException("Read timeout after " + timeout + "ms.");
}
@Override
public int read(char[] aCbuf, int aOff, int aLen)
throws IOException
{
throw new UnsupportedOperationException();
}
@Override
public void close()
throws IOException
{
delegate.close();
}
}