1. /*
  2. * Copyright 2000,2002-2004 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.taskdefs;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. /**
  22. * Copies all data from an input stream to an output stream.
  23. *
  24. * @since Ant 1.2
  25. */
  26. public class StreamPumper implements Runnable {
  27. // TODO: make SIZE an instance variable.
  28. // TODO: add a status flag to note if an error occurred in run.
  29. private static final int SIZE = 128;
  30. private InputStream is;
  31. private OutputStream os;
  32. private boolean finished;
  33. private boolean closeWhenExhausted;
  34. /**
  35. * Create a new stream pumper.
  36. *
  37. * @param is input stream to read data from
  38. * @param os output stream to write data to.
  39. * @param closeWhenExhausted if true, the output stream will be closed when
  40. * the input is exhausted.
  41. */
  42. public StreamPumper(InputStream is, OutputStream os,
  43. boolean closeWhenExhausted) {
  44. this.is = is;
  45. this.os = os;
  46. this.closeWhenExhausted = closeWhenExhausted;
  47. }
  48. /**
  49. * Create a new stream pumper.
  50. *
  51. * @param is input stream to read data from
  52. * @param os output stream to write data to.
  53. */
  54. public StreamPumper(InputStream is, OutputStream os) {
  55. this(is, os, false);
  56. }
  57. /**
  58. * Copies data from the input stream to the output stream.
  59. *
  60. * Terminates as soon as the input stream is closed or an error occurs.
  61. */
  62. public void run() {
  63. synchronized (this) {
  64. // Just in case this object is reused in the future
  65. finished = false;
  66. }
  67. final byte[] buf = new byte[SIZE];
  68. int length;
  69. try {
  70. while ((length = is.read(buf)) > 0) {
  71. os.write(buf, 0, length);
  72. }
  73. } catch (Exception e) {
  74. // ignore errors
  75. } finally {
  76. if (closeWhenExhausted) {
  77. try {
  78. os.close();
  79. } catch (IOException e) {
  80. // ignore
  81. }
  82. }
  83. synchronized (this) {
  84. finished = true;
  85. notifyAll();
  86. }
  87. }
  88. }
  89. /**
  90. * Tells whether the end of the stream has been reached.
  91. * @return true is the stream has been exhausted.
  92. **/
  93. public synchronized boolean isFinished() {
  94. return finished;
  95. }
  96. /**
  97. * This method blocks until the stream pumper finishes.
  98. * @see #isFinished()
  99. **/
  100. public synchronized void waitFor()
  101. throws InterruptedException {
  102. while (!isFinished()) {
  103. wait();
  104. }
  105. }
  106. }