-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
120 lines (106 loc) · 4.04 KB
/
Server.java
File metadata and controls
120 lines (106 loc) · 4.04 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
package ru.spbau.mit.alyokhina;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* A server that processes two list requests and receives
*/
public class Server {
/**
* Socket for connection with this server
*/
private ServerSocket serverSocket;
/**
* Constructor
*
* @param port port of connection
* @throws IOException if Socket can't be created
*/
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
}
/**
* Start of the server
*/
public void start() {
while (true) {
try {
final Socket socket = serverSocket.accept();
Thread thread = new Thread(() -> {
try (DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) {
while (!Thread.interrupted()) {
int requestType = dataInputStream.readInt();
String path = dataInputStream.readUTF();
if (requestType == Request.LIST_REQUEST.ordinal()) {
list(path, dataOutputStream);
} else if (requestType == Request.GET_REQUEST.ordinal()) {
get(path, dataOutputStream);
} else if (requestType == Request.IS_EXISTS_REQUEST.ordinal()) {
isExist(path, dataOutputStream);
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
});
thread.start();
} catch (IOException ignored) {
break;
}
}
}
/**
* Write count files, names of files and their types from input directory to dataOutputStream
*
* @param path directory path
* @param dataOutputStream stream for write result
* @throws IOException if it is impossible to write in dataOutputStream
*/
private void list(String path, DataOutputStream dataOutputStream) throws IOException {
File directory = new File(path);
File[] files = directory.listFiles();
dataOutputStream.writeInt(files == null ? 0 : files.length);
if (files != null) {
for (File file : files) {
dataOutputStream.writeUTF(file.getName());
dataOutputStream.writeBoolean(file.isDirectory());
}
}
dataOutputStream.flush();
}
/**
* Write file contents in dataOutputStream
*
* @param path name of file
* @param dataOutputStream OutputStream for write result
* @throws IOException if it is impossible to write in dataOutputStream
*/
private void get(String path, DataOutputStream dataOutputStream) throws IOException {
File file = new File(path);
int length = (int) file.length();
if (length != 0) {
DataInputStream dataInputStreamForRequest = new DataInputStream(new FileInputStream(file));
byte[] bytes = new byte[length];
if (dataInputStreamForRequest.read(bytes) == length) {
dataOutputStream.writeInt(length);
dataOutputStream.write(bytes);
} else {
throw new IOException("Impossible to read all data");
}
dataInputStreamForRequest.close();
} else {
dataOutputStream.writeInt(length);
}
}
/**
* check file for existence
*
* @param path path of the file, that we want to check
* @throws IOException if we can't write in dataOutputStream or create
*/
private void isExist(String path, DataOutputStream dataOutputStream) throws IOException {
File file = new File(path);
dataOutputStream.writeBoolean(file.exists());
}
}