/hudson-core/src/main/java/hudson/util/io/ReopenableFileOutputStream.java

http://github.com/hudson/hudson · Java · 99 lines · 52 code · 12 blank · 35 comment · 2 complexity · a1e67d028282bae79f5effd129f28400 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010, CloudBees, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util.io;
  25. import hudson.util.IOException2;
  26. import java.io.File;
  27. import java.io.FileNotFoundException;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import java.io.OutputStream;
  31. /**
  32. * {@link OutputStream} that writes to a file.
  33. *
  34. * <p>
  35. * Unlike regular {@link FileOutputStream}, this implementation allows the caller to close,
  36. * and then keep writing.
  37. *
  38. * @author Kohsuke Kawaguchi
  39. */
  40. public class ReopenableFileOutputStream extends OutputStream {
  41. private final File out;
  42. private OutputStream current;
  43. private boolean appendOnNextOpen = false;
  44. public ReopenableFileOutputStream(File out) {
  45. this.out = out;
  46. }
  47. private synchronized OutputStream current() throws IOException {
  48. if (current==null)
  49. try {
  50. current = new FileOutputStream(out,appendOnNextOpen);
  51. } catch (FileNotFoundException e) {
  52. throw new IOException2("Failed to open "+out,e);
  53. }
  54. return current;
  55. }
  56. @Override
  57. public void write(int b) throws IOException {
  58. current().write(b);
  59. }
  60. @Override
  61. public void write(byte[] b) throws IOException {
  62. current().write(b);
  63. }
  64. @Override
  65. public void write(byte[] b, int off, int len) throws IOException {
  66. current().write(b, off, len);
  67. }
  68. @Override
  69. public void flush() throws IOException {
  70. current().flush();
  71. }
  72. @Override
  73. public synchronized void close() throws IOException {
  74. if (current!=null) {
  75. current.close();
  76. appendOnNextOpen = true;
  77. current = null;
  78. }
  79. }
  80. /**
  81. * In addition to close, ensure that the next "open" would truncate the file.
  82. */
  83. public synchronized void rewind() throws IOException {
  84. close();
  85. appendOnNextOpen = false;
  86. }
  87. }