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.

ConcatFileInputStream.java 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = -1;
  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. }
  45. // inherit doc
  46. public void close() throws IOException {
  47. closeCurrent();
  48. eof = true;
  49. }
  50. // inherit doc
  51. public int read() throws IOException {
  52. int result = readCurrent();
  53. if (result == EOF && !eof) {
  54. openFile(++currentIndex);
  55. result = readCurrent();
  56. }
  57. return result;
  58. }
  59. /**
  60. * Set a managing <CODE>Task</CODE> for
  61. * this <CODE>ConcatFileInputStream</CODE>.
  62. * @param task the managing <CODE>Task</CODE>.
  63. */
  64. public void setManagingTask(Task task) {
  65. this.managingTask = task;
  66. }
  67. /**
  68. * Log a message with the specified logging level.
  69. * @param message the <CODE>String</CODE> message.
  70. * @param loglevel the <CODE>int</CODE> logging level.
  71. */
  72. public void log(String message, int loglevel) {
  73. if (managingTask != null) {
  74. managingTask.log(message, loglevel);
  75. } else {
  76. if (loglevel > Project.MSG_WARN) {
  77. System.out.println(message);
  78. } else {
  79. System.err.println(message);
  80. }
  81. }
  82. }
  83. private int readCurrent() throws IOException {
  84. return (eof || currentStream == null) ? EOF : currentStream.read();
  85. }
  86. private void openFile(int index) throws IOException {
  87. closeCurrent();
  88. if (file != null && index < file.length) {
  89. log("Opening " + file[index], Project.MSG_VERBOSE);
  90. try {
  91. currentStream = new BufferedInputStream(
  92. new FileInputStream(file[index]));
  93. } catch (IOException eyeOhEx) {
  94. log("Failed to open " + file[index], Project.MSG_ERR);
  95. throw eyeOhEx;
  96. }
  97. } else {
  98. eof = true;
  99. }
  100. }
  101. private void closeCurrent() {
  102. if (currentStream != null) {
  103. try {
  104. currentStream.close();
  105. } catch (IOException eyeOhEx) {
  106. }
  107. currentStream = null;
  108. }
  109. }
  110. }