Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
//==============================================================================//// ERG TRANSIT SYSTEMS Licensed software// (C) 2006 All rights reserved////==============================================================================//// Description: Provides a BufferedReader with a readLine method that// blocks for only a specified number of seconds. If no// input is read in that time, a specified default// string is returned. Otherwise, the input read is returned.////==============================================================================package com.erggroup.jats;import java.io.Reader;import java.io.BufferedReader;import java.io.IOException;public class TimedBufferedReader extends BufferedReader{private int timeout = 5 * 60; // 5 minuteprivate String defaultStr = "";/*** TimedBufferedReader constructor.* @param in Reader*/TimedBufferedReader(Reader in){super(in);}/*** TimedBufferedReader constructor.* @param in Reader* @param sz int Size of the input buffer.*/TimedBufferedReader(Reader in, int sz){super(in, sz);}/*** Sets number of seconds to block for input.* @param seconds int*/public void setTimeout(int timeout){this.timeout=timeout;}/*** Sets defaultStr to use if no input is read.* @param str String*/public void setDefaultStr(String str){defaultStr = str;}/*** We use ms internally* @return String*/public String readLine() throws IOException{int waitms = timeout*1000;int ms = 0;while (!this.ready()){try{Thread.currentThread().sleep(10);ms += 10;}catch (InterruptedException e){break;}if (ms >= waitms){return defaultStr;}}return super.readLine();}}