blob: 7aaca2c18c33e6a81d7f6ab297a4a6bdd0e7bf42 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/*******************************************************************************
* Copyright (c) 2012 Wind River Systems, Inc. and others. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Wind River Systems - initial API and implementation
*******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.core.internal.operations;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.CacheManager;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
/**
* The operation to calculate the message digest of a cache file.
*/
public class OpCacheFileDigest implements IOperation {
// The digest of which is going to be computed.
FSTreeNode node;
// The computing result
byte[] digest;
/**
* Create an operation to compute the digest of its local cache file.
*
* @param node The file system node.
*/
public OpCacheFileDigest(FSTreeNode node) {
this.node = node;
}
/*
* (non-Javadoc)
* @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#run(org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
File file = CacheManager.getCacheFile(node);
BufferedInputStream input = null;
try {
long totalSize = file.length();
int chunk_size = (int) totalSize / 100;
int percentRead = 0;
long bytesRead = 0;
MessageDigest digest = MessageDigest.getInstance(MD_ALG);
input = new BufferedInputStream(new DigestInputStream(new FileInputStream(file), digest));
byte[] data = new byte[OpStreamOp.DEFAULT_CHUNK_SIZE];
int length;
while ((length = input.read(data)) >= 0){
bytesRead += length;
if (chunk_size != 0) {
int percent = (int) bytesRead / chunk_size;
if (percent != percentRead) { // Update the progress.
monitor.worked(percent - percentRead);
percentRead = percent; // Remember the percentage.
}
}
}
this.digest = digest.digest();
}
catch (NoSuchAlgorithmException e) {
throw new InvocationTargetException(e);
}
catch (IOException e) {
throw new InvocationTargetException(e);
}
finally {
if (input != null) {
try {input.close();} catch (Exception e) {}
}
}
}
/**
* Get the computing result.
*
* @return The message digest of this cache file.
*/
public byte[] getDigest() {
return digest;
}
/*
* (non-Javadoc)
* @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getName()
*/
@Override
public String getName() {
return "Update cache digest"; //$NON-NLS-1$
}
/*
* (non-Javadoc)
* @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getTotalWork()
*/
@Override
public int getTotalWork() {
return 100;
}
}
|