33362
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
import java.io.BufferedInputStream;
|
|
25 |
import java.io.BufferedOutputStream;
|
|
26 |
import java.io.DataInputStream;
|
|
27 |
import java.io.DataOutputStream;
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.StringWriter;
|
|
30 |
import java.net.Socket;
|
|
31 |
import java.nio.charset.StandardCharsets;
|
|
32 |
import java.nio.file.Files;
|
|
33 |
import java.nio.file.Path;
|
|
34 |
import java.nio.file.Paths;
|
|
35 |
import java.util.Collections;
|
|
36 |
|
|
37 |
public class CustomEditor implements AutoCloseable {
|
|
38 |
|
|
39 |
public static final int SOURCE_CODE = 0;
|
|
40 |
public static final int GET_SOURCE_CODE = 1;
|
|
41 |
public static final int REMOVE_CODE = 2;
|
|
42 |
|
|
43 |
public static final int EXIT_CODE = -1;
|
|
44 |
public static final int ACCEPT_CODE = -2;
|
|
45 |
public static final int CANCEL_CODE = -3;
|
|
46 |
|
|
47 |
private final Socket socket;
|
|
48 |
private final Path path;
|
|
49 |
private final StringWriter writer;
|
|
50 |
private final String source;
|
|
51 |
|
|
52 |
public CustomEditor(int port, String fileName) throws IOException {
|
|
53 |
this.socket = new Socket((String) null, port);
|
|
54 |
this.path = Paths.get(fileName);
|
|
55 |
this.writer = new StringWriter();
|
|
56 |
this.source = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
|
57 |
}
|
|
58 |
|
|
59 |
public void loop() throws IOException {
|
|
60 |
DataInputStream input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
|
61 |
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
|
62 |
|
|
63 |
while (true) {
|
|
64 |
int cmd = input.readInt();
|
|
65 |
switch (cmd) {
|
|
66 |
case EXIT_CODE: {
|
|
67 |
Files.write(path, Collections.singletonList(writer.toString()));
|
|
68 |
return;
|
|
69 |
}
|
|
70 |
case GET_SOURCE_CODE: {
|
|
71 |
byte[] bytes = source.getBytes(StandardCharsets.UTF_8);
|
|
72 |
output.writeInt(bytes.length);
|
|
73 |
output.write(bytes);
|
|
74 |
output.flush();
|
|
75 |
break;
|
|
76 |
}
|
|
77 |
case REMOVE_CODE: {
|
|
78 |
// only for external editor
|
|
79 |
Files.delete(path);
|
|
80 |
break;
|
|
81 |
}
|
|
82 |
case CANCEL_CODE: {
|
|
83 |
return;
|
|
84 |
}
|
|
85 |
case ACCEPT_CODE: {
|
|
86 |
Files.write(path, Collections.singletonList(writer.toString()));
|
|
87 |
break;
|
|
88 |
}
|
|
89 |
case SOURCE_CODE: {
|
|
90 |
int length = input.readInt();
|
|
91 |
byte[] bytes = new byte[length];
|
|
92 |
input.readFully(bytes);
|
|
93 |
writer.write(new String(bytes, StandardCharsets.UTF_8));
|
|
94 |
break;
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
public static void main(String[] args) throws IOException {
|
|
101 |
if (args.length != 2) {
|
|
102 |
System.err.println("Usage: port file");
|
|
103 |
System.exit(1);
|
|
104 |
}
|
|
105 |
try (CustomEditor editor = new CustomEditor(Integer.parseInt(args[0]), args[1])) {
|
|
106 |
editor.loop();
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
@Override
|
|
111 |
public void close() throws IOException {
|
|
112 |
socket.close();
|
|
113 |
}
|
|
114 |
}
|