下面列出了java.util.zip.CheckedInputStream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private long calculateChecksum(URL layer) {
if (layer == null) {
return -1;
}
try {
InputStream is = layer.openStream();
try {
CheckedInputStream cis = new CheckedInputStream(is, new CRC32());
// Compute the CRC32 checksum
byte[] buf = new byte[1024];
while (cis.read(buf) >= 0) {
}
cis.close();
return cis.getChecksum().getValue();
} finally {
is.close();
}
} catch (IOException e) {
return -1;
}
}
public void test_read() throws Exception {
// testing that the return by skip is valid
InputStream checkInput = Support_Resources
.getStream("hyts_checkInput.txt");
CheckedInputStream checkIn = new CheckedInputStream(checkInput,
new CRC32());
checkIn.read();
checkIn.close();
try {
checkIn.read();
fail("IOException expected.");
} catch (IOException ee) {
// expected
}
checkInput.close();
}
public void test_read$byteII() throws Exception {
// testing that the return by skip is valid
InputStream checkInput = Support_Resources
.getStream("hyts_checkInput.txt");
CheckedInputStream checkIn = new CheckedInputStream(checkInput,
new CRC32());
byte buff[] = new byte[50];
checkIn.read(buff, 10, 5);
checkIn.close();
try {
checkIn.read(buff, 10, 5);
fail("IOException expected.");
} catch (IOException ee) {
// expected
}
checkInput.close();
}
private Long getCRC(URL zipUrl) {
Long result = -1l;
try {
CRC32 crc = new CRC32();
CheckedInputStream cis = new CheckedInputStream(zipUrl.openStream(), crc);
byte[] buffer = new byte[1024];
int length;
//read the entry from zip file and extract it to disk
while( (length = cis.read(buffer)) > 0);
cis.close();
result = crc.getValue();
} catch (IOException e) {
LOG.warn("Unable to calculate CRC, resource doesn't exist?", e);
}
return result;
}
@Override
public Long getFileChecksum(File file) {
long checksum = -1;
byte[] buffer = new byte[512];
try {
FileInputStream fis = new FileInputStream(file);
CheckedInputStream cis = new CheckedInputStream(fis, check);
int read = cis.read(buffer);
while(read != -1){
read = cis.read(buffer);
}
fis.close();
cis.close();
checksum = check.getValue();
} catch (IOException ex) {
logger.error("Error getting file checksum: "+ex);
}
return checksum;
}
/**
* Return the CRC checksum of a given part
*
* @param partIndex
* the index of the part
* @param isAttachment
* true if the part is an attachment
* @return the part checksum
* @throws PackageException
*/
@PublicAtsApi
public long getPartChecksum(
int partIndex,
boolean isAttachment ) throws PackageException {
InputStream partDataStream = getPartData(partIndex, isAttachment);
if (partDataStream != null) {
try {
SeekInputStream seekDataStream = new SeekInputStream(partDataStream);
seekDataStream.seek(0);
// create a new crc and reset it
CRC32 crc = new CRC32();
// use checked stream to get the checksum
CheckedInputStream stream = new CheckedInputStream(seekDataStream, crc);
int bufLen = 4096;
byte[] buffer = new byte[bufLen];
int numBytesRead = bufLen;
while (numBytesRead == bufLen) {
numBytesRead = stream.read(buffer, 0, bufLen);
}
long checksum = stream.getChecksum().getValue();
stream.close();
return checksum;
} catch (IOException ioe) {
throw new PackageException(ioe);
}
} else {
throw new MimePartWithoutContentException("MIME part does not have any content");
}
}
static void createTestFile() throws Throwable {
/**
* Generate a temporary file for uploading/downloading
*/
sourceFile = new File(System.getProperty("user.home"), "sftp-file");
java.util.Random rnd = new java.util.Random();
FileOutputStream out = new FileOutputStream(sourceFile);
byte[] buf = new byte[1024000];
for (int i = 0; i < 100; i++) {
rnd.nextBytes(buf);
out.write(buf);
}
out.close();
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
sourceFile), new Adler32());
try {
byte[] tempBuf = new byte[16384];
while (cis.read(tempBuf) >= 0) {
}
sourceFileChecksum = cis.getChecksum().getValue();
} catch (IOException e) {
} finally {
cis.close();
}
}
public static long getAlder32(File f) {
try {
FileInputStream inputStream = new FileInputStream(f);
Adler32 adlerChecksum = new Adler32();
CheckedInputStream cinStream = new CheckedInputStream(inputStream, adlerChecksum);
byte[] b = new byte[128];
while (cinStream.read(b) >= 0) {
}
long checksum = cinStream.getChecksum().getValue();
cinStream.close();
return checksum;
} catch (IOException e) {
return 0;
}
}
static void performTest(SshClient ssh) throws Throwable {
/**
* Authenticate the user using password authentication
*/
PasswordAuthentication pwd = new PasswordAuthentication();
pwd.setPassword(password);
ssh.authenticate(pwd);
/**
* Start a session and do basic IO
*/
if (ssh.isAuthenticated()) {
System.out.println("Client info: " + ssh.toString());
SftpClient sftp = new SftpClient(ssh);
sftp.setBlockSize(32768);
sftp.setTransferMode(SftpClient.MODE_BINARY);
/**
* Create a directory for the test files
*/
sftp.mkdirs("sftp/test-files");
/**
* Change directory
*/
sftp.cd("sftp/test-files");
/**
* Put a file into our new directory
*/
long length = sourceFile.length();
long t1 = System.currentTimeMillis();
sftp.put(sourceFile.getAbsolutePath());
long t2 = System.currentTimeMillis();
long e = t2 - t1;
float kbs;
if (e >= 1000) {
kbs = (((float) length / 1024) / ((float) e / 1000) / 1000);
System.out.println("Upload Transfered at " + df.format(kbs)
+ " MB/s");
} else {
System.out.println("Download transfered in under one second");
}
/**
* Download the file inot a new location
*/
File f2 = new File(System.getProperty("user.home"), "downloaded");
f2.mkdir();
sftp.lcd(f2.getAbsolutePath());
File retrievedFile = new File(f2, sourceFile.getName());
t1 = System.currentTimeMillis();
sftp.get(sourceFile.getName());
t2 = System.currentTimeMillis();
e = t2 - t1;
if (e >= 1000) {
kbs = (((float) length / 1024) / ((float) e / 1000) / 1000);
System.out.println("Download Transfered at " + df.format(kbs)
+ " MB/s");
} else {
System.out.println("Download transfered in under one second");
}
long checksum2 = 0;
CheckedInputStream cis = new CheckedInputStream(
new FileInputStream(retrievedFile), new Adler32());
try {
byte[] tempBuf = new byte[16384];
while (cis.read(tempBuf) >= 0) {
}
checksum2 = cis.getChecksum().getValue();
} catch (IOException ex) {
} finally {
cis.close();
}
if (checksum2 != sourceFileChecksum) {
System.out.println("FILES DO NOT MATCH");
}
}
}