KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > visual > StreamRedirector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: StreamRedirector.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.visual;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26
27 /**
28  * Redirects the content coming in through an InputStream using a separate thread to a
29  * RedirectorLineHandler instance. The default text encoding is used.
30  */

31 public class StreamRedirector implements Runnable JavaDoc {
32
33     private InputStream JavaDoc in;
34     private RedirectorLineHandler handler;
35     private Exception JavaDoc exception;
36
37     /**
38      * @param in the InputStream to read the content from
39      * @param handler the handler that receives all the lines
40      */

41     public StreamRedirector(InputStream JavaDoc in, RedirectorLineHandler handler) {
42         this.in = in;
43         this.handler = handler;
44     }
45     
46     /**
47      * @return true if the run() method was terminated by an exception.
48      */

49     public boolean hasFailed() {
50         return (this.exception != null);
51     }
52     
53     /**
54      * @return the exception if the run() method was terminated by an exception, or null
55      */

56     public Exception JavaDoc getException() {
57         return this.exception;
58     }
59
60     /** @see java.lang.Runnable#run() */
61     public void run() {
62         this.exception = null;
63         try {
64             Reader JavaDoc inr = new java.io.InputStreamReader JavaDoc(in);
65             BufferedReader JavaDoc br = new BufferedReader JavaDoc(inr);
66             if (handler != null) {
67                 handler.notifyStart();
68             }
69             String JavaDoc line = null;
70             while ((line = br.readLine()) != null) {
71                 if (handler != null) {
72                     handler.handleLine(line);
73                 }
74             }
75             if (handler != null) {
76                 handler.notifyStart();
77             }
78         } catch (IOException JavaDoc ioe) {
79             this.exception = ioe;
80         }
81     }
82 }
Popular Tags