Guest User

Untitled

a guest
Sep 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import java.io.FilterInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4.  
  5. /** A bit more efficient byte replacing filter stream */
  6. public class ByteReplacingInputStream extends FilterInputStream {
  7.  
  8. private final byte[] search, replacement;
  9. private byte[] buffer;
  10. private int bufferOffset, bufferEOL, replacementOffset;
  11.  
  12. public ByteReplacingInputStream(InputStream in, byte[] search, byte[] replacement) {
  13. super(in);
  14. this.search = search;
  15. this.replacement = replacement;
  16.  
  17. // Buffer fits the search string a single time
  18. buffer = new byte[this.search.length];
  19.  
  20. // Init rest of state
  21. bufferOffset = 0;
  22. bufferEOL = -1;
  23.  
  24. // Setting replacementOffset to trigger a fillBuffer at first read
  25. replacementOffset = this.replacement.length;
  26. }
  27.  
  28. @Override
  29. public int read() throws IOException {
  30. // If we just finished a replacement output run, refill the buffer
  31. if(replacementOffset >= replacement.length) {
  32. bufferOffset = 0;
  33. bufferEOL = -1;
  34. replacementOffset = -1;
  35. for (byte i = 0; i < buffer.length; i++) {
  36. int b = super.read();
  37. if (b == -1) {
  38. bufferEOL = i;
  39. break;
  40. } else {
  41. buffer[i] = (byte) b;
  42. }
  43. }
  44. }
  45.  
  46. // Once we reach the EOL, there is nothing left to be done
  47. if(bufferOffset == bufferEOL) {
  48. return -1;
  49. }
  50.  
  51. // If we're not inside a replacement, should we start one right now?
  52. if(replacementOffset < 0) {
  53. boolean match = true;
  54. for (byte i = 0; i < search.length; i++) {
  55. if (buffer[(bufferOffset + i) % buffer.length] != search[i]) {
  56. match = false;
  57. break;
  58. }
  59. }
  60. if (match) {
  61. replacementOffset = 0;
  62. }
  63. }
  64.  
  65. // Output a replacement byte
  66. if(replacementOffset >= 0) {
  67. if(replacementOffset < replacement.length) {
  68. return replacement[replacementOffset++];
  69. } else {
  70. // The replacement appears to be zero.
  71. // We're normally jump to the top, but since we don't have goto's in Java, we instead just recurse
  72. return read();
  73. }
  74. }
  75.  
  76. // We will now read a byte from the normal buffer
  77. int result = buffer[bufferOffset];
  78.  
  79. // If we haven't found the EOL yet, this is the time to read a new byte
  80. if(bufferEOL < 0) {
  81. int b = super.read();
  82. if (b == -1) {
  83. bufferEOL = bufferOffset;
  84. } else {
  85. buffer[bufferOffset] = (byte) b;
  86. }
  87. }
  88.  
  89. // We increase the offset
  90. bufferOffset = (bufferOffset + 1) % buffer.length;
  91.  
  92. // Return the normal byte
  93. return result;
  94. }
  95.  
  96. @Override
  97. public int read(byte b[]) throws IOException {
  98. return read(b, 0, b.length);
  99. }
  100.  
  101. @Override
  102. public int read(byte b[], int offset, int length) throws IOException {
  103. // Standard implementation from Java
  104. if (b == null) {
  105. throw new NullPointerException();
  106. } else if (offset < 0 || length < 0 || length > b.length - offset) {
  107. throw new IndexOutOfBoundsException();
  108. } else if (length == 0) {
  109. return 0;
  110. }
  111.  
  112. int c = read();
  113. if (c == -1) {
  114. return -1;
  115. }
  116. b[offset] = (byte) c;
  117.  
  118. int i = 1;
  119. try {
  120. for (; i < length ; i++) {
  121. c = read();
  122. if (c == -1) {
  123. break;
  124. }
  125. b[offset + i] = (byte) c;
  126. }
  127. } catch (IOException e) {}
  128.  
  129. return i;
  130. }
  131. }
Add Comment
Please, Sign In to add comment