import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.nio.charset.Charset;
import java.security.MessageDigest;
Metadata read(File file, Charset encoding) {
Reader reader = null;
int lines = 0;
- char c = (char)-1;
+ char c = (char) -1;
try {
MessageDigest md5Digest = DigestUtils.getMd5Digest();
md5Digest.reset();
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
int i = reader.read();
- boolean afterCR = true;
+ boolean afterCR = false;
while (i != -1) {
c = (char) i;
if (afterCR) {
md5Digest.update(charToBytesUTF(c));
i = reader.read();
}
- if (c != (char)-1) {
+ if (c != (char) -1) {
lines++;
}
String hash = Hex.encodeHexString(md5Digest.digest());
}
private byte[] charToBytesUTF(char c) {
- char[] buffer = new char[]{c};
+ char[] buffer = new char[] {c};
byte[] b = new byte[buffer.length << 1];
for (int i = 0; i < buffer.length; i++) {
int bpos = i << 1;
public class FileMetadataTest {
private static final String EXPECTED_HASH_WITHOUT_LATEST_EOL = "c80cc50d65ace6c4eb63f189d274dbeb";
+ private static final String EXPECTED_HASH_NEW_LINE_FIRST = "cf2d41454b5b451eeb5122f0848c1d2a";
private static final String EXPECTED_HASH_WITH_LATEST_EOL = "bf77e51d219e7d7d643faac86f1b5d15";
@Rule
assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITHOUT_LATEST_EOL);
}
+ @Test
+ public void start_with_newline() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "\nfoo\nbar\r\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(4);
+ assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_NEW_LINE_FIRST);
+ }
+
@Test
public void should_throw_if_file_does_not_exist() throws Exception {
File tempFolder = temp.newFolder();