001/** 
002 * Copyright 2011-2012
003 * Ubiquitous Knowledge Processing (UKP) Lab
004 * Technische Universität Darmstadt
005 * All rights reserved.
006 * 
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 * 
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 * 
017 * For a complete copy of the license please see the file LICENSE distributed 
018 * with the cleartk-syntax-berkeley project or visit 
019 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
020 * 
021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031 * POSSIBILITY OF SUCH DAMAGE. 
032 */
033
034
035package org.cleartk.util;
036
037import java.io.BufferedReader;
038import java.io.IOException;
039import java.io.InputStream;
040import java.io.InputStreamReader;
041import java.util.ArrayList;
042import java.util.List;
043
044import org.apache.uima.UIMAFramework;
045import org.apache.uima.util.Level;
046/**
047 * <br>
048 * Copyright (c) 2011-2012, Technische Universität Darmstadt <br>
049 * All rights reserved.
050 * 
051 * 
052 * @author Martin Riedl
053 */
054
055
056public abstract class InputStreamHandler<T> extends Thread {
057        /**
058         * Stream that is read
059         */
060        private BufferedReader reader;
061        private T buffer=null;
062
063        InputStreamHandler(T buffer ,InputStream stream) {
064                this.reader =new BufferedReader(new InputStreamReader(stream));
065                this.buffer = buffer;
066                start();
067        }
068
069        InputStreamHandler(T buffer, BufferedReader reader) {
070                this.reader = reader;
071                this.buffer = buffer;
072                start();
073        }
074        public T getBuffer(){
075                return buffer;
076        }
077
078        /**
079         * read data into buffer
080         */
081        public void run() {
082                try {
083                        String nextLine;
084                        while ((nextLine = reader.readLine()) != null) {
085                                addToBuffer(nextLine);
086                        }
087                } catch (IOException ioe) {
088                        UIMAFramework.getLogger(InputStreamHandler.class).log(Level.WARNING,ioe.getMessage());
089                }
090        }
091        
092        public abstract void addToBuffer(String line);
093        
094        public static InputStreamHandler<StringBuffer> getInputStreamAsBufferedString(InputStream stream){
095                
096                return new InputStreamHandler<StringBuffer>(new StringBuffer(), stream) {
097                        @Override
098                        public synchronized  void  addToBuffer(String line) {
099                                getBuffer().append(line.trim());
100                        }
101                };
102        }
103        public static InputStreamHandler<List<String>> getInputStreamAsList(InputStream stream){
104                
105        
106                return new InputStreamHandler<List<String>>(new ArrayList<String>(), stream) {
107                        @Override
108                        public synchronized void addToBuffer(String line) {
109                                getBuffer().add(line);
110                        }
111                };
112        }
113}