001 /*--------------------------------------------------------------------------+
002 $Id: StreamDrainerThread.java 26283 2010-02-18 11:18:57Z juergens $
003 | |
004 | Copyright 2005-2010 Technische Universitaet Muenchen |
005 | |
006 | Licensed under the Apache License, Version 2.0 (the "License"); |
007 | you may not use this file except in compliance with the License. |
008 | You may obtain a copy of the License at |
009 | |
010 | http://www.apache.org/licenses/LICENSE-2.0 |
011 | |
012 | Unless required by applicable law or agreed to in writing, software |
013 | distributed under the License is distributed on an "AS IS" BASIS, |
014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015 | See the License for the specific language governing permissions and |
016 | limitations under the License. |
017 +--------------------------------------------------------------------------*/
018 package edu.tum.cs.commons.io;
019
020 import java.io.BufferedReader;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.InputStreamReader;
024
025 import edu.tum.cs.commons.assertion.CCSMAssert;
026
027 /**
028 * A thread to drain an input stream. Read content is discarded.
029 *
030 * @author Elmar Juergens
031 * @author Florian Deissenboeck
032 * @author $Author: juergens $
033 * @version $Rev: 26283 $
034 * @levd.rating GREEN Hash: 540B98ECCE4EE60DC5A69D0C6A03DB2C
035 */
036 public class StreamDrainerThread extends Thread {
037
038 /** Stream the reader reads from. */
039 private final InputStream input;
040
041 /**
042 * Create a new reader that immediately starts to drain the content of this
043 * stream . This call is non- blocking
044 *
045 * @param input
046 * Stream to read from.
047 */
048 public StreamDrainerThread(InputStream input) {
049 super();
050 this.input = input;
051 start();
052 }
053
054 /** Reads content from the stream as long as the stream is not empty. */
055 @Override
056 public synchronized void run() {
057 BufferedReader reader = new BufferedReader(new InputStreamReader(input));
058 char[] buffer = new char[1024];
059
060 try {
061 @SuppressWarnings("unused")
062 int read = 0;
063 while ((read = reader.read(buffer)) != -1) {
064 // throw away read content
065 }
066 } catch (IOException e) {
067 CCSMAssert.fail("Error draining stream: " + e.getMessage());
068 }
069 }
070
071 }