PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/java/main/org/wikimedia/wikihadoop/ByteMatcher.java

https://github.com/giangbinhtran/Hedera
Java | 90 lines | 61 code | 7 blank | 22 comment | 14 complexity | be481dab536118582a682800f692df6b MD5 | raw file
  1. /**
  2. * Copyright 2011 Yusuke Matsubara
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.wikimedia.wikihadoop;
  17. import java.io.*;
  18. import org.apache.hadoop.io.DataOutputBuffer;
  19. import org.apache.hadoop.fs.Seekable;
  20. public class ByteMatcher {
  21. private final InputStream in;
  22. private final Seekable pos;
  23. private long lastPos;
  24. private long currentPos;
  25. private long bytes;
  26. public ByteMatcher(InputStream in, Seekable pos) throws IOException {
  27. this.in = in;
  28. this.pos = pos;
  29. this.bytes = 0;
  30. this.lastPos = -1;
  31. this.currentPos = -1;
  32. }
  33. public ByteMatcher(SeekableInputStream is) throws IOException {
  34. this(is, is);
  35. }
  36. public long getReadBytes() {
  37. return this.bytes;
  38. }
  39. public long getPos() throws IOException {
  40. return this.pos.getPos();
  41. }
  42. public long getLastUnmatchPos() { return this.lastPos; }
  43. public void skip(long len) throws IOException {
  44. this.in.skip(len);
  45. this.bytes += len;
  46. }
  47. /**
  48. * Tuan (22.05.2014) - change the visibility of this method to public for being able to read from other packages
  49. */
  50. public boolean readUntilMatch(String textPat, DataOutputBuffer outBufOrNull, long end) throws IOException {
  51. byte[] match = textPat.getBytes("UTF-8");
  52. int i = 0;
  53. while (true) {
  54. int b = this.in.read();
  55. // end of file:
  56. if (b == -1) {
  57. System.err.println("eof 1");
  58. return false;
  59. }
  60. ++this.bytes; //! TODO: count up later in batch
  61. // save to buffer:
  62. if (outBufOrNull != null)
  63. outBufOrNull.write(b);
  64. // check if we're matching:
  65. if (b == match[i]) {
  66. i++;
  67. if (i >= match.length)
  68. return true;
  69. } else {
  70. i = 0;
  71. if ( this.currentPos != this.getPos() ) {
  72. this.lastPos = this.currentPos;
  73. this.currentPos = this.getPos();
  74. }
  75. }
  76. // see if we've passed the stop point:
  77. if (i == 0 && this.pos.getPos() >= end) {
  78. System.err.println("eof 2: end=" + end);
  79. return false;
  80. }
  81. }
  82. }
  83. }