InsertStream.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package 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. public int read() throws IOException {
  19. int result = super.read();
  20. if (result != -1 || end_of_file) return result;
  21. end_of_file = true;
  22. return Character.LINE_SEPARATOR;
  23. }
  24. public int read(byte[] b) throws IOException {
  25. int result = super.read(b);
  26. if (result != -1 || end_of_file) return result;
  27. end_of_file = true;
  28. if (b.length > 1) {
  29. b[0] = Character.LINE_SEPARATOR;
  30. return 1;
  31. }
  32. return -1;
  33. }
  34. public int read(byte[] b, int off, int len) throws IOException {
  35. int result = super.read(b,off,len);
  36. if (result != -1 || end_of_file) return result;
  37. end_of_file = true;
  38. if (len > 0) {
  39. b[off] = Character.LINE_SEPARATOR;
  40. return 1;
  41. } else return 0;
  42. }
  43. }