1 | /*
|
---|
2 | * Copyright 2004-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.util;
|
---|
19 |
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.PipedInputStream;
|
---|
22 | import java.io.PipedOutputStream;
|
---|
23 |
|
---|
24 | import org.apache.tools.ant.ProjectComponent;
|
---|
25 | import org.apache.tools.ant.Task;
|
---|
26 | import org.apache.tools.ant.Project;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Special <CODE>PipedInputStream</CODE> that will not die
|
---|
30 | * when the writing <CODE>Thread</CODE> is no longer alive.
|
---|
31 | * @since Ant 1.6.2
|
---|
32 | */
|
---|
33 | public class LeadPipeInputStream extends PipedInputStream {
|
---|
34 | private ProjectComponent managingPc;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Construct a new <CODE>LeadPipeInputStream</CODE>.
|
---|
38 | */
|
---|
39 | public LeadPipeInputStream() {
|
---|
40 | super();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Construct a new <CODE>LeadPipeInputStream</CODE>
|
---|
45 | * with the specified buffer size.
|
---|
46 | * @param size the size of the circular buffer.
|
---|
47 | */
|
---|
48 | public LeadPipeInputStream(int size) {
|
---|
49 | super();
|
---|
50 | setBufferSize(size);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Construct a new <CODE>LeadPipeInputStream</CODE> to pull
|
---|
55 | * from the specified <CODE>PipedOutputStream</CODE>.
|
---|
56 | * @param src the <CODE>PipedOutputStream</CODE> source.
|
---|
57 | * @throws IOException if unable to construct the stream.
|
---|
58 | */
|
---|
59 | public LeadPipeInputStream(PipedOutputStream src) throws IOException {
|
---|
60 | super(src);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Construct a new <CODE>LeadPipeInputStream</CODE> to pull
|
---|
65 | * from the specified <CODE>PipedOutputStream</CODE>, using a
|
---|
66 | * circular buffer of the specified size.
|
---|
67 | * @param src the <CODE>PipedOutputStream</CODE> source.
|
---|
68 | * @param size the size of the circular buffer.
|
---|
69 | */
|
---|
70 | public LeadPipeInputStream(PipedOutputStream src, int size) throws IOException {
|
---|
71 | super(src);
|
---|
72 | setBufferSize(size);
|
---|
73 | }
|
---|
74 |
|
---|
75 | //inherit doc
|
---|
76 | public synchronized int read() throws IOException {
|
---|
77 | int result = -1;
|
---|
78 | try {
|
---|
79 | result = super.read();
|
---|
80 | } catch (IOException eyeOhEx) {
|
---|
81 | if ("write end dead".equalsIgnoreCase(eyeOhEx.getMessage())) {
|
---|
82 | if (super.in > 0 && super.out < super.buffer.length
|
---|
83 | && super.out > super.in) {
|
---|
84 | result = super.buffer[super.out++] & 0xFF;
|
---|
85 | }
|
---|
86 | } else {
|
---|
87 | log("error at LeadPipeInputStream.read(): "
|
---|
88 | + eyeOhEx.getMessage(), Project.MSG_INFO);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return result;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Set the size of the buffer.
|
---|
96 | * @param size the new buffer size. Ignored if <= current size.
|
---|
97 | */
|
---|
98 | public synchronized void setBufferSize(int size) {
|
---|
99 | if (size > buffer.length) {
|
---|
100 | byte[] newBuffer = new byte[size];
|
---|
101 | if (in >= 0) {
|
---|
102 | if (in > out) {
|
---|
103 | System.arraycopy(buffer, out, newBuffer, out, in - out);
|
---|
104 | } else {
|
---|
105 | int outlen = buffer.length - out;
|
---|
106 | System.arraycopy(buffer, out, newBuffer, 0, outlen);
|
---|
107 | System.arraycopy(buffer, 0, newBuffer, outlen, in);
|
---|
108 | in+= outlen;
|
---|
109 | out = 0;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | buffer = newBuffer;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Set a managing <CODE>Task</CODE> for
|
---|
118 | * this <CODE>LeadPipeInputStream</CODE>.
|
---|
119 | * @param task the managing <CODE>Task</CODE>.
|
---|
120 | */
|
---|
121 | public void setManagingTask(Task task) {
|
---|
122 | setManagingComponent(task);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Set a managing <CODE>ProjectComponent</CODE> for
|
---|
127 | * this <CODE>LeadPipeInputStream</CODE>.
|
---|
128 | * @param pc the managing <CODE>ProjectComponent</CODE>.
|
---|
129 | */
|
---|
130 | public void setManagingComponent(ProjectComponent pc) {
|
---|
131 | this.managingPc = pc;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Log a message with the specified logging level.
|
---|
136 | * @param message the <CODE>String</CODE> message.
|
---|
137 | * @param loglevel the <CODE>int</CODE> logging level.
|
---|
138 | */
|
---|
139 | public void log(String message, int loglevel) {
|
---|
140 | if (managingPc != null) {
|
---|
141 | managingPc.log(message, loglevel);
|
---|
142 | } else {
|
---|
143 | if (loglevel > Project.MSG_WARN) {
|
---|
144 | System.out.println(message);
|
---|
145 | } else {
|
---|
146 | System.err.println(message);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|