KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > LeadPipeInputStream


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
19 package org.apache.tools.ant.util;
20
21 import java.io.IOException JavaDoc;
22 import java.io.PipedInputStream JavaDoc;
23 import java.io.PipedOutputStream JavaDoc;
24
25 import org.apache.tools.ant.ProjectComponent;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.Project;
28
29 /**
30  * Special <code>PipedInputStream</code> that will not die
31  * when the writing <code>Thread</code> is no longer alive.
32  * @since Ant 1.6.2
33  */

34 public class LeadPipeInputStream extends PipedInputStream JavaDoc {
35     private ProjectComponent managingPc;
36
37     /**
38      * Construct a new <code>LeadPipeInputStream</code>.
39      */

40     public LeadPipeInputStream() {
41         super();
42     }
43
44     /**
45      * Construct a new <code>LeadPipeInputStream</code>
46      * with the specified buffer size.
47      * @param size the size of the circular buffer.
48      */

49     public LeadPipeInputStream(int size) {
50         super();
51         setBufferSize(size);
52     }
53
54     /**
55      * Construct a new <code>LeadPipeInputStream</code> to pull
56      * from the specified <code>PipedOutputStream</code>.
57      * @param src the <code>PipedOutputStream</code> source.
58      * @throws IOException if unable to construct the stream.
59      */

60     public LeadPipeInputStream(PipedOutputStream JavaDoc src) throws IOException JavaDoc {
61         super(src);
62     }
63
64     /**
65      * Construct a new <code>LeadPipeInputStream</code> to pull
66      * from the specified <code>PipedOutputStream</code>, using a
67      * circular buffer of the specified size.
68      * @param src the <code>PipedOutputStream</code> source.
69      * @param size the size of the circular buffer.
70      * @throws IOException if there is an error.
71      */

72     public LeadPipeInputStream(PipedOutputStream JavaDoc src, int size) throws IOException JavaDoc {
73         super(src);
74         setBufferSize(size);
75     }
76
77     //inherit doc
78
/**
79      * Read a byte from the stream.
80      * @return the byte (0 to 255) or -1 if there are no more.
81      * @throws IOException if there is an error.
82      */

83     public synchronized int read() throws IOException JavaDoc {
84         int result = -1;
85         try {
86             result = super.read();
87         } catch (IOException JavaDoc eyeOhEx) {
88             if ("write end dead".equalsIgnoreCase(eyeOhEx.getMessage())) {
89                 if (super.in > 0 && super.out < super.buffer.length
90                     && super.out > super.in) {
91                     result = super.buffer[super.out++] & 0xFF;
92                 }
93             } else {
94                 log("error at LeadPipeInputStream.read(): "
95                     + eyeOhEx.getMessage(), Project.MSG_INFO);
96             }
97         }
98         return result;
99     }
100
101     /**
102      * Set the size of the buffer.
103      * @param size the new buffer size. Ignored if <= current size.
104      */

105     public synchronized void setBufferSize(int size) {
106         if (size > buffer.length) {
107             byte[] newBuffer = new byte[size];
108             if (in >= 0) {
109                 if (in > out) {
110                     System.arraycopy(buffer, out, newBuffer, out, in - out);
111                 } else {
112                     int outlen = buffer.length - out;
113                     System.arraycopy(buffer, out, newBuffer, 0, outlen);
114                     System.arraycopy(buffer, 0, newBuffer, outlen, in);
115                     in += outlen;
116                     out = 0;
117                 }
118             }
119             buffer = newBuffer;
120         }
121     }
122
123     /**
124      * Set a managing <code>Task</code> for
125      * this <code>LeadPipeInputStream</code>.
126      * @param task the managing <code>Task</code>.
127      */

128     public void setManagingTask(Task task) {
129         setManagingComponent(task);
130     }
131
132     /**
133      * Set a managing <code>ProjectComponent</code> for
134      * this <code>LeadPipeInputStream</code>.
135      * @param pc the managing <code>ProjectComponent</code>.
136      */

137     public void setManagingComponent(ProjectComponent pc) {
138         this.managingPc = pc;
139     }
140
141     /**
142      * Log a message with the specified logging level.
143      * @param message the <code>String</code> message.
144      * @param loglevel the <code>int</code> logging level.
145      */

146     public void log(String JavaDoc message, int loglevel) {
147         if (managingPc != null) {
148             managingPc.log(message, loglevel);
149         } else {
150             if (loglevel > Project.MSG_WARN) {
151                 System.out.println(message);
152             } else {
153                 System.err.println(message);
154             }
155         }
156     }
157 }
158
159
Popular Tags