1. /*
  2. * Copyright 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.util;
  18. import java.io.File;
  19. import java.io.InputStream;
  20. import java.io.BufferedInputStream;
  21. import java.io.IOException;
  22. import java.io.FileInputStream;
  23. import org.apache.tools.ant.Task;
  24. import org.apache.tools.ant.Project;
  25. /**
  26. * Special <CODE>InputStream</CODE> that will
  27. * concatenate the contents of an array of files.
  28. */
  29. public class ConcatFileInputStream extends InputStream {
  30. private static final int EOF = -1;
  31. private int currentIndex = 0;
  32. private boolean eof = false;
  33. private File[] file;
  34. private InputStream currentStream;
  35. private Task managingTask;
  36. /**
  37. * Construct a new <CODE>ConcatFileInputStream</CODE>
  38. * with the specified <CODE>File[]</CODE>.
  39. * @param file <CODE>File[]</CODE>.
  40. * @throws <CODE>IOException</CODE> if I/O errors occur.
  41. */
  42. public ConcatFileInputStream(File[] file) throws IOException {
  43. this.file = file;
  44. openFile(currentIndex);
  45. }
  46. // inherit doc
  47. public void close() throws IOException {
  48. closeCurrent();
  49. eof = true;
  50. }
  51. // inherit doc
  52. public int read() throws IOException {
  53. int result = readCurrent();
  54. if (result == EOF && !eof) {
  55. openFile(++currentIndex);
  56. result = readCurrent();
  57. }
  58. return result;
  59. }
  60. /**
  61. * Set a managing <CODE>Task</CODE> for
  62. * this <CODE>ConcatFileInputStream</CODE>.
  63. * @param task the managing <CODE>Task</CODE>.
  64. */
  65. public void setManagingTask(Task task) {
  66. this.managingTask = task;
  67. }
  68. /**
  69. * Log a message with the specified logging level.
  70. * @param message the <CODE>String</CODE> message.
  71. * @param loglevel the <CODE>int</CODE> logging level.
  72. */
  73. public void log(String message, int loglevel) {
  74. if (managingTask != null) {
  75. managingTask.log(message, loglevel);
  76. } else {
  77. if (loglevel > Project.MSG_WARN) {
  78. System.out.println(message);
  79. } else {
  80. System.err.println(message);
  81. }
  82. }
  83. }
  84. private int readCurrent() throws IOException {
  85. return (eof || currentStream == null) ? EOF : currentStream.read();
  86. }
  87. private void openFile(int index) throws IOException {
  88. closeCurrent();
  89. if (file != null && index < file.length) {
  90. log("Opening " + file[index], Project.MSG_VERBOSE);
  91. currentStream = new BufferedInputStream(
  92. new FileInputStream(file[index]));
  93. } else {
  94. eof = true;
  95. }
  96. }
  97. private void closeCurrent() {
  98. if (currentStream != null) {
  99. try {
  100. currentStream.close();
  101. } catch (IOException eyeOhEx) {
  102. }
  103. currentStream = null;
  104. }
  105. }
  106. }