InsertStream.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package de.jplag.scheme;
  2. import java.io.File;
  3. import java.io.FileDescriptor;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. public class InsertStream extends FileInputStream {
  8. public InsertStream(File file) throws FileNotFoundException {
  9. super(file);
  10. }
  11. public InsertStream(FileDescriptor fdObj) {
  12. super(fdObj);
  13. }
  14. public InsertStream(String name) throws FileNotFoundException {
  15. super(name);
  16. }
  17. boolean end_of_file = false;
  18. @Override
  19. public int read() throws IOException {
  20. int result = super.read();
  21. if (result != -1 || end_of_file) return result;
  22. end_of_file = true;
  23. return Character.LINE_SEPARATOR;
  24. }
  25. @Override
  26. public int read(byte[] b) throws IOException {
  27. int result = super.read(b);
  28. if (result != -1 || end_of_file) return result;
  29. end_of_file = true;
  30. if (b.length > 1) {
  31. b[0] = Character.LINE_SEPARATOR;
  32. return 1;
  33. }
  34. return -1;
  35. }
  36. @Override
  37. public int read(byte[] b, int off, int len) throws IOException {
  38. int result = super.read(b,off,len);
  39. if (result != -1 || end_of_file) return result;
  40. end_of_file = true;
  41. if (len > 0) {
  42. b[off] = Character.LINE_SEPARATOR;
  43. return 1;
  44. } else return 0;
  45. }
  46. }