You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

OutputStreamFunneler.java 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. package org.apache.tools.ant.util;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. /**
  21. * Manages a set of <code>OutputStream</code>s to
  22. * write to a single underlying stream, which is
  23. * closed only when the last &quot;funnel&quot;
  24. * has been closed.
  25. */
  26. public class OutputStreamFunneler {
  27. /**
  28. * Default timeout.
  29. * @see #setTimeout(long)
  30. */
  31. public static final long DEFAULT_TIMEOUT_MILLIS = 1000;
  32. private final class Funnel extends OutputStream {
  33. private boolean closed = false;
  34. private Funnel() {
  35. synchronized (OutputStreamFunneler.this) {
  36. ++count;
  37. }
  38. }
  39. public void flush() throws IOException {
  40. synchronized (OutputStreamFunneler.this) {
  41. dieIfClosed();
  42. out.flush();
  43. }
  44. }
  45. public void write(int b) throws IOException {
  46. synchronized (OutputStreamFunneler.this) {
  47. dieIfClosed();
  48. out.write(b);
  49. }
  50. }
  51. public void write(byte[] b) throws IOException {
  52. synchronized (OutputStreamFunneler.this) {
  53. dieIfClosed();
  54. out.write(b);
  55. }
  56. }
  57. public void write(byte[] b, int off, int len) throws IOException {
  58. synchronized (OutputStreamFunneler.this) {
  59. dieIfClosed();
  60. out.write(b, off, len);
  61. }
  62. }
  63. public void close() throws IOException {
  64. release(this);
  65. }
  66. }
  67. private OutputStream out;
  68. private int count = 0;
  69. private boolean closed;
  70. private long timeoutMillis;
  71. /**
  72. * Create a new <code>OutputStreamFunneler</code> for
  73. * the specified <code>OutputStream</code>.
  74. * @param out <code>OutputStream</code>.
  75. */
  76. public OutputStreamFunneler(OutputStream out) {
  77. this(out, DEFAULT_TIMEOUT_MILLIS);
  78. }
  79. /**
  80. * Create a new <code>OutputStreamFunneler</code> for
  81. * the specified <code>OutputStream</code>, with the
  82. * specified timeout value.
  83. * @param out <code>OutputStream</code>.
  84. * @param timeoutMillis <code>long</code>.
  85. * @see #setTimeout(long)
  86. */
  87. public OutputStreamFunneler(OutputStream out, long timeoutMillis) {
  88. if (out == null) {
  89. throw new IllegalArgumentException(
  90. "OutputStreamFunneler.<init>: out == null");
  91. }
  92. this.out = out;
  93. this.closed = false; //as far as we know
  94. setTimeout(timeoutMillis);
  95. }
  96. /**
  97. * Set the timeout for this <code>OutputStreamFunneler</code>.
  98. * This is the maximum time that may elapse between the closure
  99. * of the last &quot;funnel&quot; and the next call to
  100. * <code>getOutputStream()</code> without closing the
  101. * underlying stream.
  102. * @param timeoutMillis <code>long</code> timeout value.
  103. */
  104. public synchronized void setTimeout(long timeoutMillis) {
  105. this.timeoutMillis = timeoutMillis;
  106. }
  107. /**
  108. * Get a &quot;funnel&quot; <code>OutputStream</code> instance to
  109. * write to this <code>OutputStreamFunneler</code>'s underlying
  110. * <code>OutputStream</code>.
  111. * @return <code>OutputStream</code>.
  112. * @throws IOException if unable to create the funnel.
  113. */
  114. public synchronized OutputStream getFunnelInstance()
  115. throws IOException {
  116. dieIfClosed();
  117. try {
  118. return new Funnel();
  119. } finally {
  120. notifyAll();
  121. }
  122. }
  123. private synchronized void release(Funnel funnel) throws IOException {
  124. //ignore release of an already-closed funnel
  125. if (!funnel.closed) {
  126. try {
  127. if (timeoutMillis > 0) {
  128. try {
  129. wait(timeoutMillis);
  130. } catch (InterruptedException eyeEx) {
  131. //ignore
  132. }
  133. }
  134. if (--count == 0) {
  135. close();
  136. }
  137. } finally {
  138. funnel.closed = true;
  139. }
  140. }
  141. }
  142. private synchronized void close() throws IOException {
  143. try {
  144. dieIfClosed();
  145. out.close();
  146. } finally {
  147. closed = true;
  148. }
  149. }
  150. private synchronized void dieIfClosed() throws IOException {
  151. if (closed) {
  152. throw new IOException("The funneled OutputStream has been closed.");
  153. }
  154. }
  155. }