NewlineStream.java 839 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package de.jplag;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. /**
  5. * This stream adds a newline to the end of a file. This is a proxy.
  6. */
  7. public class NewlineStream extends InputStream {
  8. private int endOfFile = 0;
  9. private InputStream stream;
  10. public NewlineStream(InputStream stream) {
  11. super();
  12. this.stream = stream;
  13. }
  14. @Override
  15. public int read() throws IOException {
  16. int result;
  17. switch (endOfFile) {
  18. case 0:
  19. result = stream.read();
  20. if (result == -1) {
  21. result = 13;
  22. endOfFile = 1;
  23. }
  24. break;
  25. case 1:
  26. result = 10;
  27. endOfFile = 2;
  28. break;
  29. default:
  30. result = -1;
  31. break;
  32. }
  33. return result;
  34. }
  35. }