Project

General

Profile

Statistics
| Revision:

root / src / FilesInfo.java

History | View | Annotate | Download (3.96 KB)

1 1 up20150487
2
3
import java.io.*;
4
import java.io.File;
5
import java.nio.charset.StandardCharsets;
6
import java.nio.file.Files;
7
import java.nio.file.Path;
8
import java.nio.file.Paths;
9
import java.security.MessageDigest;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.List;
13
14
import javax.management.RuntimeErrorException;
15
16
public class FilesInfo{
17
18
    private String filePath;
19
    private String fileID;
20
    private File file;
21
    private long ModifiedDate;
22
    private String owner;
23
    private int size;
24
    public static int CHUNK_MAXSIZE = 64000;
25
    private ArrayList<Chunk> chunks;
26
    private int NumChunks;
27
    private int initiatorPeer;
28
    private int rep;
29
30
    public FilesInfo(int initiatorPeer,String filePath, int rep) {
31
        this.initiatorPeer=initiatorPeer;
32
        this.rep=rep;
33
        this.file = new File(filePath);
34
        if (this.file.exists() && !this.file.isDirectory()) {
35
            this.filePath = filePath;
36
            Path path = Paths.get(this.filePath);
37
38
            this.ModifiedDate = this.file.lastModified();
39
            try {
40
                this.owner = Files.getOwner(path).toString();
41
            } catch (IOException e) {
42
43
            }
44
            this.size = (int) this.file.length();
45
46
            // setHeader
47
48
            this.NumChunks = (int) (this.size / CHUNK_MAXSIZE + 1);
49
            this.chunks = new ArrayList<Chunk>();
50
51
            makeId();
52
53
            fileToChunks();
54
55
        } else {
56
            System.err.println("File not found");
57
        }
58
        this.filePath = filePath;
59
    }
60
61
    public int getInitiatorPeer(){
62
        return initiatorPeer;
63
    }
64
    public void setInitiatorPeer(int initiatorPeer){
65
        this.initiatorPeer=initiatorPeer;
66
    }
67
    public byte[] getFileContent() {
68
        Path path = Paths.get(this.filePath);
69
        byte[] DD= new byte[1];
70
        try{
71
            return Files.readAllBytes(path);
72
        }catch(IOException e){
73
            return DD;
74
        }
75
76
    }
77
78
    private void fileToChunks() { // ainda não está completamente bem
79
        int currChunkNo = 0;
80
        byte[] chunkData = new byte[CHUNK_MAXSIZE];
81
82
        try {
83
            FileInputStream fileStream = new FileInputStream(this.file);
84
85
            BufferedInputStream bufferStream = new BufferedInputStream(fileStream);
86
87
            int bytes;
88
            while ((bytes = bufferStream.read(chunkData)) > 0) {
89
90
                byte[] data = Arrays.copyOfRange(chunkData,0, bytes);
91
                currChunkNo++;
92
                Chunk chunk = new Chunk(this.initiatorPeer,this.fileID, currChunkNo, bytes, data, this.rep);// o 2 está mal
93
                this.chunks.add(chunk);
94
                chunkData = new byte[CHUNK_MAXSIZE];
95
            }
96
97
        } catch (Exception e) {
98
            e.printStackTrace();
99
100
        }
101
102
    }
103
104
    public String getFileID() {
105
        return fileID;
106
    }
107
    public File getFile() {
108
        return file;
109
    }
110
111
    public int getSize() {
112
        return size;
113
    }
114
115
    public ArrayList<Chunk> getChunks() {
116
        return this.chunks;
117
    }
118
119
    private void makeId() {
120
        String fileName = this.file.getName();
121
        String owner = this.file.getParent();
122
        String modifiedDate = String.valueOf(this.file.lastModified());
123
        String fileId = fileName + '_' + owner + '_' + modifiedDate;
124
        this.fileID = sha256(fileId);
125
    }
126
127
    private String sha256(String toEncript) {
128
        try {
129
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
130
            byte[] encodedhash = digest.digest(toEncript.getBytes(StandardCharsets.UTF_8));
131
            StringBuffer hexString = new StringBuffer();
132
            for (int i = 0; i < encodedhash.length; i++) {
133
                String hex = Integer.toHexString(0xff & encodedhash[i]);
134
                if (hex.length() == 1)
135
                    hexString.append('0');
136
                hexString.append(hex);
137
            }
138
            return hexString.toString();
139
140
        } catch (Exception ex) {
141
            throw new RuntimeException(ex);
142
        }
143
144
    }
145
146
}