Repository navigation
Expand file tree
/
Copy pathProtocol.java
More file actions
139 lines (121 loc) · 2.84 KB
/
Copy pathProtocol.java
File metadata and controls
139 lines (121 loc) · 2.84 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.nio.file.Files;
public class Protocol{
public Socket sock;
public BufferedReader in;
public DataOutputStream out;
public Protocol (Socket s) throws IOException{
sock = s;
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new DataOutputStream(s.getOutputStream());
}
public void writeResquestHeader(String fonctionName){
try {
out.writeBytes(fonctionName+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeResponseHeader(int code){
try {
if (code == 0){
out.writeBytes("OK:\n");
}else{
out.writeBytes("ERR:\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public String readRequestHeader(){
try {
return in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public int readResponseHeader(){
try {
String responseHeader = in.readLine();
System.out.println(responseHeader);
if (responseHeader.equals("OK:")){
return 0;
}else{
return -1;
}
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
public void writeParamString(String param){
try {
out.writeBytes(param+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
public String readParamString(){
try {
return in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String readResponseBody(){
String line;
StringBuilder sb = new StringBuilder();
try {
while (!(line = in.readLine()).equals("")){
sb.append(line+"\n");
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void writeResponseBody(String body){
try {
out.writeBytes(body);
} catch (IOException e) {
e.printStackTrace();
}
}
public void endMessage(){
try {
out.writeBytes("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
public void downloadFile(File f){
try {
ObjectInputStream inF = new ObjectInputStream(sock.getInputStream());
byte[] content = (byte[]) inF.readObject();
Files.write(f.toPath(), content);
} catch (IOException e) {
System.out.println("ServeurFichier : Erreur de réception du fichier ----> " + e);
} catch (ClassNotFoundException e) {
System.out.println("Une erreur est survenue avec le fichier ----> " + e);
}
}
public void uploadFile(File f){
try{
ObjectOutputStream outF = new ObjectOutputStream(sock.getOutputStream());
byte content[] = Files.readAllBytes(f.toPath());
outF.writeObject(content);
}catch(Exception e){
e.printStackTrace();
}
}
}