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

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