| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.io.input; |
| 18 | |
|
| 19 | |
import java.io.Closeable; |
| 20 | |
import java.io.File; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.RandomAccessFile; |
| 23 | |
import java.io.UnsupportedEncodingException; |
| 24 | |
import java.nio.charset.Charset; |
| 25 | |
import java.nio.charset.CharsetEncoder; |
| 26 | |
import java.nio.charset.UnsupportedCharsetException; |
| 27 | |
|
| 28 | |
import org.apache.commons.io.Charsets; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 1782769 | public class ReversedLinesFileReader implements Closeable { |
| 37 | |
|
| 38 | |
private final int blockSize; |
| 39 | |
private final Charset encoding; |
| 40 | |
|
| 41 | |
private final RandomAccessFile randomAccessFile; |
| 42 | |
|
| 43 | |
private final long totalByteLength; |
| 44 | |
private final long totalBlockCount; |
| 45 | |
|
| 46 | |
private final byte[][] newLineSequences; |
| 47 | |
private final int avoidNewlineSplitBufferSize; |
| 48 | |
private final int byteDecrement; |
| 49 | |
|
| 50 | |
private FilePart currentFilePart; |
| 51 | |
|
| 52 | 81 | private boolean trailingNewlineOfFileSkipped = false; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
@Deprecated |
| 64 | |
public ReversedLinesFileReader(final File file) throws IOException { |
| 65 | 0 | this(file, 4096, Charset.defaultCharset()); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public ReversedLinesFileReader(final File file, final Charset charset) throws IOException { |
| 79 | 0 | this(file, 4096, charset); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | 81 | public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding) throws IOException { |
| 96 | 81 | this.blockSize = blockSize; |
| 97 | 81 | this.encoding = encoding; |
| 98 | |
|
| 99 | 81 | randomAccessFile = new RandomAccessFile(file, "r"); |
| 100 | 81 | totalByteLength = randomAccessFile.length(); |
| 101 | 81 | int lastBlockLength = (int) (totalByteLength % blockSize); |
| 102 | 81 | if (lastBlockLength > 0) { |
| 103 | 47 | totalBlockCount = totalByteLength / blockSize + 1; |
| 104 | |
} else { |
| 105 | 34 | totalBlockCount = totalByteLength / blockSize; |
| 106 | 34 | if (totalByteLength > 0) { |
| 107 | 17 | lastBlockLength = blockSize; |
| 108 | |
} |
| 109 | |
} |
| 110 | 81 | currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null); |
| 111 | |
|
| 112 | |
|
| 113 | 81 | final Charset charset = Charsets.toCharset(encoding); |
| 114 | 81 | final CharsetEncoder charsetEncoder = charset.newEncoder(); |
| 115 | 81 | final float maxBytesPerChar = charsetEncoder.maxBytesPerChar(); |
| 116 | 81 | if(maxBytesPerChar==1f) { |
| 117 | |
|
| 118 | 19 | byteDecrement = 1; |
| 119 | 62 | } else if(charset == Charset.forName("UTF-8")) { |
| 120 | |
|
| 121 | |
|
| 122 | 32 | byteDecrement = 1; |
| 123 | 30 | } else if(charset == Charset.forName("Shift_JIS")) { |
| 124 | |
|
| 125 | |
|
| 126 | 6 | byteDecrement = 1; |
| 127 | 24 | } else if(charset == Charset.forName("UTF-16BE") || charset == Charset.forName("UTF-16LE")) { |
| 128 | |
|
| 129 | |
|
| 130 | 12 | byteDecrement = 2; |
| 131 | 12 | } else if(charset == Charset.forName("UTF-16")) { |
| 132 | 6 | throw new UnsupportedEncodingException( |
| 133 | |
"For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)"); |
| 134 | |
} else { |
| 135 | 6 | throw new UnsupportedEncodingException( |
| 136 | |
"Encoding "+encoding+" is not supported yet (feel free to submit a patch)"); |
| 137 | |
} |
| 138 | |
|
| 139 | 69 | newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) }; |
| 140 | |
|
| 141 | 69 | avoidNewlineSplitBufferSize = newLineSequences[0].length; |
| 142 | 69 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException { |
| 160 | 81 | this(file, blockSize, Charsets.toCharset(encoding)); |
| 161 | 69 | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
public String readLine() throws IOException { |
| 170 | |
|
| 171 | 2555 | String line = currentFilePart.readLine(); |
| 172 | 26893 | while (line == null) { |
| 173 | 24391 | currentFilePart = currentFilePart.rollOver(); |
| 174 | 24391 | if (currentFilePart != null) { |
| 175 | 24338 | line = currentFilePart.readLine(); |
| 176 | |
} else { |
| 177 | |
|
| 178 | |
break; |
| 179 | |
} |
| 180 | |
} |
| 181 | |
|
| 182 | |
|
| 183 | 2555 | if("".equals(line) && !trailingNewlineOfFileSkipped) { |
| 184 | 64 | trailingNewlineOfFileSkipped = true; |
| 185 | 64 | line = readLine(); |
| 186 | |
} |
| 187 | |
|
| 188 | 2555 | return line; |
| 189 | |
} |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public void close() throws IOException { |
| 197 | 69 | randomAccessFile.close(); |
| 198 | 69 | } |
| 199 | |
|
| 200 | 51365 | private class FilePart { |
| 201 | |
private final long no; |
| 202 | |
|
| 203 | |
private final byte[] data; |
| 204 | |
|
| 205 | |
private byte[] leftOver; |
| 206 | |
|
| 207 | |
private int currentLastBytePos; |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | 24419 | private FilePart(final long no, final int length, final byte[] leftOverOfLastFilePart) throws IOException { |
| 217 | 24419 | this.no = no; |
| 218 | 24419 | final int dataLength = length + (leftOverOfLastFilePart != null ? leftOverOfLastFilePart.length : 0); |
| 219 | 24419 | this.data = new byte[dataLength]; |
| 220 | 24419 | final long off = (no - 1) * blockSize; |
| 221 | |
|
| 222 | |
|
| 223 | 24419 | if (no > 0 ) { |
| 224 | 24402 | randomAccessFile.seek(off); |
| 225 | 24402 | final int countRead = randomAccessFile.read(data, 0, length); |
| 226 | 24402 | if (countRead != length) { |
| 227 | 0 | throw new IllegalStateException("Count of requested bytes and actually read bytes don't match"); |
| 228 | |
} |
| 229 | |
} |
| 230 | |
|
| 231 | 24419 | if (leftOverOfLastFilePart != null) { |
| 232 | 24338 | System.arraycopy(leftOverOfLastFilePart, 0, data, length, leftOverOfLastFilePart.length); |
| 233 | |
} |
| 234 | 24419 | this.currentLastBytePos = data.length - 1; |
| 235 | 24419 | this.leftOver = null; |
| 236 | 24419 | } |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
private FilePart rollOver() throws IOException { |
| 245 | |
|
| 246 | 24391 | if (currentLastBytePos > -1) { |
| 247 | 0 | throw new IllegalStateException("Current currentLastCharPos unexpectedly positive... " |
| 248 | |
+ "last readLine() should have returned something! currentLastCharPos=" + currentLastBytePos); |
| 249 | |
} |
| 250 | |
|
| 251 | 24391 | if (no > 1) { |
| 252 | 24338 | return new FilePart(no - 1, blockSize, leftOver); |
| 253 | |
} else { |
| 254 | |
|
| 255 | 53 | if (leftOver != null) { |
| 256 | 0 | throw new IllegalStateException("Unexpected leftover of the last block: leftOverOfThisFilePart=" |
| 257 | |
+ new String(leftOver, encoding)); |
| 258 | |
} |
| 259 | 53 | return null; |
| 260 | |
} |
| 261 | |
} |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
private String readLine() throws IOException { |
| 270 | |
|
| 271 | 26893 | String line = null; |
| 272 | |
int newLineMatchByteCount; |
| 273 | |
|
| 274 | 26893 | final boolean isLastFilePart = no == 1; |
| 275 | |
|
| 276 | 26893 | int i = currentLastBytePos; |
| 277 | 586113 | while (i > -1) { |
| 278 | |
|
| 279 | 586060 | if (!isLastFilePart && i < avoidNewlineSplitBufferSize) { |
| 280 | |
|
| 281 | |
|
| 282 | 24338 | createLeftOver(); |
| 283 | 24338 | break; |
| 284 | |
} |
| 285 | |
|
| 286 | |
|
| 287 | 561722 | if ((newLineMatchByteCount = getNewLineMatchByteCount(data, i)) > 0 ) { |
| 288 | 2438 | final int lineStart = i + 1; |
| 289 | 2438 | final int lineLengthBytes = currentLastBytePos - lineStart + 1; |
| 290 | |
|
| 291 | 2438 | if (lineLengthBytes < 0) { |
| 292 | 0 | throw new IllegalStateException("Unexpected negative line length="+lineLengthBytes); |
| 293 | |
} |
| 294 | 2438 | final byte[] lineData = new byte[lineLengthBytes]; |
| 295 | 2438 | System.arraycopy(data, lineStart, lineData, 0, lineLengthBytes); |
| 296 | |
|
| 297 | 2438 | line = new String(lineData, encoding); |
| 298 | |
|
| 299 | 2438 | currentLastBytePos = i - newLineMatchByteCount; |
| 300 | 2438 | break; |
| 301 | |
} |
| 302 | |
|
| 303 | |
|
| 304 | 559284 | i -= byteDecrement; |
| 305 | |
|
| 306 | |
|
| 307 | 559284 | if (i < 0) { |
| 308 | 64 | createLeftOver(); |
| 309 | 64 | break; |
| 310 | |
} |
| 311 | |
} |
| 312 | |
|
| 313 | |
|
| 314 | 26893 | if (isLastFilePart && leftOver != null) { |
| 315 | |
|
| 316 | 64 | line = new String(leftOver, encoding); |
| 317 | 64 | leftOver = null; |
| 318 | |
} |
| 319 | |
|
| 320 | 26893 | return line; |
| 321 | |
} |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
private void createLeftOver() { |
| 327 | 24402 | final int lineLengthBytes = currentLastBytePos + 1; |
| 328 | 24402 | if (lineLengthBytes > 0) { |
| 329 | |
|
| 330 | 24402 | leftOver = new byte[lineLengthBytes]; |
| 331 | 24402 | System.arraycopy(data, 0, leftOver, 0, lineLengthBytes); |
| 332 | |
} else { |
| 333 | 0 | leftOver = null; |
| 334 | |
} |
| 335 | 24402 | currentLastBytePos = -1; |
| 336 | 24402 | } |
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
private int getNewLineMatchByteCount(final byte[] data, final int i) { |
| 346 | 2241472 | for (final byte[] newLineSequence : newLineSequences) { |
| 347 | 1682188 | boolean match = true; |
| 348 | 4572290 | for (int j = newLineSequence.length - 1; j >= 0; j--) { |
| 349 | 2890102 | final int k = i + j - (newLineSequence.length - 1); |
| 350 | 2890102 | match &= k >= 0 && data[k] == newLineSequence[j]; |
| 351 | |
} |
| 352 | 1682188 | if (match) { |
| 353 | 2438 | return newLineSequence.length; |
| 354 | |
} |
| 355 | |
} |
| 356 | 559284 | return 0; |
| 357 | |
} |
| 358 | |
} |
| 359 | |
|
| 360 | |
} |