You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReverseFileReader.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.io;
  18. import java.io.EOFException;
  19. import java.io.IOException;
  20. import java.nio.ByteBuffer;
  21. import java.nio.channels.SeekableByteChannel;
  22. import java.nio.charset.Charset;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.StandardOpenOption;
  26. import java.util.ArrayList;
  27. import java.util.Stack;
  28. /**
  29. * Reads a file in reverse.
  30. */
  31. public class ReverseFileReader implements AutoCloseable {
  32. /** Path to the file we're reading. */
  33. private final Path file;
  34. /** File to manipulate. */
  35. private SeekableByteChannel byteChannel;
  36. /** Number of bytes to skip backwards at a time. */
  37. private byte seekLength = 50;
  38. /**
  39. * Create a new ReverseFileReader.
  40. *
  41. * @param file File to read
  42. *
  43. * @throws SecurityException If a security manager exists and its checkRead method denies
  44. * read access to the file.
  45. * @throws IOException If there is an error seeking to the end of the file.
  46. */
  47. public ReverseFileReader(final Path file) throws SecurityException, IOException {
  48. this.file = file;
  49. byteChannel = Files.newByteChannel(file, StandardOpenOption.READ);
  50. reset();
  51. }
  52. /**
  53. * Reset the file pointer to the end of the file.
  54. *
  55. * @throws IOException If there is an error seeking, or the file is closed.
  56. */
  57. public final void reset() throws IOException {
  58. if (!byteChannel.isOpen()) {
  59. throw new IOException("Channel has been closed.");
  60. }
  61. byteChannel = Files.newByteChannel(file, StandardOpenOption.READ);
  62. byteChannel.position(byteChannel.size());
  63. }
  64. /**
  65. * Get the current seekLength.
  66. *
  67. * @return current seekLength
  68. */
  69. public byte getSeekLength() {
  70. return seekLength;
  71. }
  72. /**
  73. * Set the seekLength.
  74. *
  75. * @param newValue New value for seekLength
  76. */
  77. public void setSeekLength(final byte newValue) {
  78. seekLength = newValue;
  79. }
  80. @Override
  81. public void close() throws IOException {
  82. if (byteChannel.isOpen()) {
  83. byteChannel.close();
  84. }
  85. }
  86. /**
  87. * Get the next full line.
  88. *
  89. * @return The next full line.
  90. * @throws IOException If an error reading or seeking occured, or if the fiel is closed.
  91. */
  92. public String getNextLine() throws IOException {
  93. if (!byteChannel.isOpen()) {
  94. throw new IOException("Channel has been closed.");
  95. }
  96. // Used to store result to output.
  97. final ArrayList<Byte> line = new ArrayList<>(seekLength);
  98. // Check current position, if 0 we are at the start of the file
  99. // and should throw an exception.
  100. final long startfp = byteChannel.position();
  101. if (startfp == 0) {
  102. throw new EOFException("Reached Start of file");
  103. }
  104. // Keep looping until we get a full line, or the end of the file
  105. boolean keepLooping = true;
  106. while (keepLooping) {
  107. // Get Current Position
  108. long fp = byteChannel.position();
  109. // Check how far to seek backwards (seekLength or to the start of the file)
  110. final int seekDistance;
  111. if (fp < seekLength) {
  112. // Seek to the start of the file;
  113. seekDistance = (int) fp;
  114. fp = 0;
  115. } else {
  116. // Seek to position current-seekLength
  117. seekDistance = seekLength;
  118. fp -= seekDistance;
  119. }
  120. // Seek!
  121. byteChannel.position(fp);
  122. final ByteBuffer bytes = ByteBuffer.allocate(seekDistance);
  123. // Read into the bytes array
  124. byteChannel.read(bytes);
  125. // And loop looking for data
  126. // This uses seekDistance so that only wanted data is checked.
  127. boolean gotNewLine = false;
  128. for (int i = seekDistance - 1; i >= 0; --i) {
  129. // Check for New line Character, or a non carriage-return char
  130. if (bytes.get(i) == '\n') {
  131. // Seek to the location of this character and exit this loop.
  132. byteChannel.position(fp + i);
  133. gotNewLine = true;
  134. break;
  135. } else if (bytes.get(i) != '\r') {
  136. // Add to the result, the loop will continue going.
  137. line.add(0, bytes.get(i));
  138. }
  139. }
  140. // We have now processed the data we read (Either added it all to the
  141. // buffer, or found a newline character.)
  142. if (fp == 0 && !gotNewLine) {
  143. // This part of the loop started from the start of the file, but didn't
  144. // find a new line anywhere. no more loops are possible, so Treat
  145. // this as "got new line"
  146. gotNewLine = true;
  147. byteChannel.position(0);
  148. }
  149. // Do we need to continue?
  150. if (gotNewLine) {
  151. // We have found a new line somewhere, thus we don't need
  152. // to read any more bytes, so exit the while loop!
  153. keepLooping = false;
  154. } else {
  155. // We have not found a new line anywhere,
  156. // Seek to the pre-read position, and repeat.
  157. byteChannel.position(fp);
  158. }
  159. }
  160. // Return the data obtained.
  161. final byte[] result = new byte[line.size()];
  162. for (int i = 0; i < line.size(); ++i) {
  163. result[i] = line.get(i);
  164. }
  165. return new String(result, Charset.forName("UTF-8"));
  166. }
  167. /**
  168. * Try and get x number of lines.
  169. * If the file is closed, an empty stack will be returned.
  170. *
  171. * @param numLines Number of lines to try and get.
  172. * @return The requested lines
  173. */
  174. public Stack<String> getLines(final int numLines) {
  175. final Stack<String> result = new Stack<>();
  176. for (int i = 0; i < numLines; ++i) {
  177. try {
  178. result.push(getNextLine());
  179. } catch (IOException e) {
  180. break;
  181. }
  182. }
  183. return result;
  184. }
  185. /**
  186. * Try and get x number of lines and return a \n delimited String.
  187. * If the file is closed, an empty string will be returned.
  188. *
  189. * @param numLines Number of lines to try and get.
  190. * @return The requested lines
  191. */
  192. public String getLinesAsString(final int numLines) {
  193. final StringBuilder result = new StringBuilder();
  194. for (int i = 0; i < numLines; ++i) {
  195. try {
  196. result.insert(0, '\n');
  197. result.insert(0, getNextLine());
  198. } catch (IOException e) {
  199. break;
  200. }
  201. }
  202. if (result.charAt(0) == '\n') {
  203. result.deleteCharAt(0);
  204. }
  205. return result.toString();
  206. }
  207. }