-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowDocument.java
More file actions
101 lines (79 loc) · 3.2 KB
/
Copy pathShowDocument.java
File metadata and controls
101 lines (79 loc) · 3.2 KB
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
package turing;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import turing.Message.OPERATION;
import turing.Message.RESULT;
public class ShowDocument implements Runnable {
SocketChannel client;
Message received;
public ShowDocument (SocketChannel client, Message received){
if (client == null || received == null)
return;
this.client = client;
this.received = received;
}
@Override
public void run() {
String clientUsername = received.getUsername();
String documentName = received.getDocumentName();
FileChannel fileChannel = null;
FileChannel fileChannelDocument = null;
Message.RESULT result;
System.out.println("Documento: " + documentName);
try
{
result = Turing.databaseUsers.haveDocument(clientUsername,documentName);
if (result == RESULT.OP_OK){
fileChannelDocument = FileChannel.open(Paths.get(Turing.DATA_DIR, documentName),
StandardOpenOption.CREATE,StandardOpenOption.TRUNCATE_EXISTING,StandardOpenOption.WRITE);
int numeroSezioni = Turing.databaseDocuments.getDocument(documentName).getNumOfSections();
Section section = null;
int i = 0;
fileChannelDocument.close();
fileChannelDocument = FileChannel.open(Paths.get(Turing.DATA_DIR, documentName),
StandardOpenOption.WRITE, StandardOpenOption.APPEND);
while (i < numeroSezioni && result == RESULT.OP_OK) {
section = Turing.databaseDocuments.getDocument(documentName).getSection(i);
if (section!= null) {
String path = Turing.DATA_DIR;
path = path + section.getFileName();
File file = new File(path);
if (!file.exists()) file.createNewFile();
fileChannel = FileChannel.open(Paths.get(Turing.DATA_DIR,section.getFileName()),StandardOpenOption.READ);
//leggo contenuto della sezione
ByteBuffer bufferRead = Message.readFileChannel(fileChannel);
//bufferRead.flip(); la flip viene fatta dalla readFilechannel!
Message.writeFileChannel(bufferRead, fileChannelDocument); //scrivo il contenuto sul documento
bufferRead.clear();
} else result = RESULT.SEC_INEXISTENT;
i++;
}
}
Message toSend = new Message();
toSend.setMessageResult(OPERATION.SHOW_D,result);
Message.sendMessage(client, toSend);
System.out.println("Server-ShowDocument ["+ received.getUsername()+", document: " + received.getDocumentName()+" ]:" +result);
if (result == RESULT.OP_OK) {
fileChannelDocument.close();
fileChannelDocument = FileChannel.open(Paths.get(Turing.DATA_DIR,documentName),StandardOpenOption.READ);
ByteBuffer buffer = Message.readFileChannel(fileChannelDocument);
Message.send(client, buffer);
fileChannel.close();
}
client.close();
} catch (UnsupportedOperationException | SecurityException | IOException e){
Message toSend = new Message();
toSend.setMessageResult(OPERATION.SHOW_D,RESULT.OP_ERR);
Message.sendMessage(client, toSend);//invio l'esito
try {
client.close();
} catch (IOException e1) {
}
}
}
}