diff options
| author | 2011-06-03 17:03:51 -0700 | |
|---|---|---|
| committer | 2011-06-03 17:03:51 -0700 | |
| commit | 6d25a990afffd5eb385aba3043d5dfad36f1539a (patch) | |
| tree | d591113b4104b04d98f69bbe56d21a605b9198a7 | |
| parent | 3b71239aca01fb029d9acd2c606b7be9b8f32d66 (diff) | |
Add FileUtils.checksumCrc32
Change-Id: Ic6bffcb66554d7af38cea68648d9431fda8ef10d
| -rw-r--r-- | core/java/android/os/FileUtils.java | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java index f56f6a9937ac..632daa1321e3 100644 --- a/core/java/android/os/FileUtils.java +++ b/core/java/android/os/FileUtils.java @@ -19,19 +19,20 @@ package android.os; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.regex.Pattern; +import java.util.zip.CRC32; +import java.util.zip.CheckedInputStream; /** * Tools for managing files. Not for public consumption. * @hide */ -public class FileUtils -{ +public class FileUtils { public static final int S_IRWXU = 00700; public static final int S_IRUSR = 00400; public static final int S_IWUSR = 00200; @@ -94,7 +95,7 @@ public class FileUtils /** returns the FAT file system volume ID for the volume mounted * at the given mount point, or -1 for failure - * @param mount point for FAT volume + * @param mountPoint point for FAT volume * @return volume ID or -1 */ public static native int getFatVolumeId(String mountPoint); @@ -226,4 +227,32 @@ public class FileUtils input.close(); } } + + /** + * Computes the checksum of a file using the CRC32 checksum routine. + * The value of the checksum is returned. + * + * @param file the file to checksum, must not be null + * @return the checksum value or an exception is thrown. + */ + public static long checksumCrc32(File file) throws FileNotFoundException, IOException { + CRC32 checkSummer = new CRC32(); + CheckedInputStream cis = null; + + try { + cis = new CheckedInputStream( new FileInputStream(file), checkSummer); + byte[] buf = new byte[128]; + while(cis.read(buf) >= 0) { + // Just read for checksum to get calculated. + } + return checkSummer.getValue(); + } finally { + if (cis != null) { + try { + cis.close(); + } catch (IOException e) { + } + } + } + } } |