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