@Test public void testEndsWithZeroesEqualToBufferSize() throws IOException { final byte[] data = new byte[4096]; for (int i = 0; i < 4090; i++) { data[i] = 'A'; } Files.write(targetFile.toPath(), data, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); final int zeroCount = RepairCorruptedFileEndings.countTrailingZeroes(targetFile); assertEquals(6, zeroCount); }
static int countTrailingZeroes(final File partitionFile) throws IOException { final RandomAccessFile raf = new RandomAccessFile(partitionFile, "r"); long startPos = partitionFile.length() - 4096; int count = 0; boolean reachedStartOfFile = false; while (!reachedStartOfFile) { int bufferLength = 4096; if (startPos < 0) { bufferLength = (int) (startPos + 4096); startPos = 0; reachedStartOfFile = true; } raf.seek(startPos); final byte[] buffer = new byte[bufferLength]; final int read = fillBuffer(raf, buffer); for (int i = read - 1; i >= 0; i--) { final byte b = buffer[i]; if (b == 0) { count++; } else { return count; } } startPos -= 4096; } return count; }
RepairCorruptedFileEndings { static int countTrailingZeroes(final File partitionFile) throws IOException { final RandomAccessFile raf = new RandomAccessFile(partitionFile, "r"); long startPos = partitionFile.length() - 4096; int count = 0; boolean reachedStartOfFile = false; while (!reachedStartOfFile) { int bufferLength = 4096; if (startPos < 0) { bufferLength = (int) (startPos + 4096); startPos = 0; reachedStartOfFile = true; } raf.seek(startPos); final byte[] buffer = new byte[bufferLength]; final int read = fillBuffer(raf, buffer); for (int i = read - 1; i >= 0; i--) { final byte b = buffer[i]; if (b == 0) { count++; } else { return count; } } startPos -= 4096; } return count; } }
RepairCorruptedFileEndings { static int countTrailingZeroes(final File partitionFile) throws IOException { final RandomAccessFile raf = new RandomAccessFile(partitionFile, "r"); long startPos = partitionFile.length() - 4096; int count = 0; boolean reachedStartOfFile = false; while (!reachedStartOfFile) { int bufferLength = 4096; if (startPos < 0) { bufferLength = (int) (startPos + 4096); startPos = 0; reachedStartOfFile = true; } raf.seek(startPos); final byte[] buffer = new byte[bufferLength]; final int read = fillBuffer(raf, buffer); for (int i = read - 1; i >= 0; i--) { final byte b = buffer[i]; if (b == 0) { count++; } else { return count; } } startPos -= 4096; } return count; }  }
RepairCorruptedFileEndings { static int countTrailingZeroes(final File partitionFile) throws IOException { final RandomAccessFile raf = new RandomAccessFile(partitionFile, "r"); long startPos = partitionFile.length() - 4096; int count = 0; boolean reachedStartOfFile = false; while (!reachedStartOfFile) { int bufferLength = 4096; if (startPos < 0) { bufferLength = (int) (startPos + 4096); startPos = 0; reachedStartOfFile = true; } raf.seek(startPos); final byte[] buffer = new byte[bufferLength]; final int read = fillBuffer(raf, buffer); for (int i = read - 1; i >= 0; i--) { final byte b = buffer[i]; if (b == 0) { count++; } else { return count; } } startPos -= 4096; } return count; }  static void main(final String[] args); }
RepairCorruptedFileEndings { static int countTrailingZeroes(final File partitionFile) throws IOException { final RandomAccessFile raf = new RandomAccessFile(partitionFile, "r"); long startPos = partitionFile.length() - 4096; int count = 0; boolean reachedStartOfFile = false; while (!reachedStartOfFile) { int bufferLength = 4096; if (startPos < 0) { bufferLength = (int) (startPos + 4096); startPos = 0; reachedStartOfFile = true; } raf.seek(startPos); final byte[] buffer = new byte[bufferLength]; final int read = fillBuffer(raf, buffer); for (int i = read - 1; i >= 0; i--) { final byte b = buffer[i]; if (b == 0) { count++; } else { return count; } } startPos -= 4096; } return count; }  static void main(final String[] args);  }