am 6ff5ba00: Merge "Add FileUtils.checksumCrc32" into honeycomb-LTE

* commit '6ff5ba00b07b32f3c82bce2c412e5e8f30c202bc':
  Add FileUtils.checksumCrc32
This commit is contained in:
Wink Saville
2011-06-04 06:24:40 -07:00
committed by Android Git Automerger

View File

@@ -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) {
}
}
}
}
}