/** * Returns the next non-comment, non-whitespace line of the input buffer. * * @param input the input buffer * @return the next non-comment, non-whitespace line of the input buffer or null if the end of the * buffer is reached before such a line can be found */ static /*@Nullable*/ String getNextRealLine(BufferedReader input) { String currentLine = ""; try { while (currentLine != null) { currentLine = input.readLine(); if (currentLine != null && !isComment(currentLine) && !isWhitespace(currentLine)) return currentLine; } } catch (IOException e) { throw new RuntimeException(e.toString()); } return null; }
/** Submits POST command to the server, and reads the reply. */ public boolean post( String url, String fileName, String cryptToken, String type, String path, String content, String comment) throws IOException { String sep = "89692781418184"; while (content.indexOf(sep) != -1) sep += "x"; String message = makeMimeForm(fileName, cryptToken, type, path, content, comment, sep); // for test // URL server = new URL("http", "localhost", 80, savePath); URL server = new URL(getCodeBase().getProtocol(), getCodeBase().getHost(), getCodeBase().getPort(), url); URLConnection connection = server.openConnection(); connection.setAllowUserInteraction(false); connection.setDoOutput(true); // connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestProperty("Content-type", "multipart/form-data; boundary=" + sep); connection.setRequestProperty("Content-length", Integer.toString(message.length())); // System.out.println(url); String replyString = null; try { DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(message); out.close(); // System.out.println("Wrote " + message.length() + // " bytes to\n" + connection); try { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String reply = null; while ((reply = in.readLine()) != null) { if (reply.startsWith("ERROR ")) { replyString = reply.substring("ERROR ".length()); } } in.close(); } catch (IOException ioe) { replyString = ioe.toString(); } } catch (UnknownServiceException use) { replyString = use.getMessage(); System.out.println(message); } if (replyString != null) { // System.out.println("---- Reply " + replyString); if (replyString.startsWith("URL ")) { URL eurl = getURL(replyString.substring("URL ".length())); getAppletContext().showDocument(eurl); } else if (replyString.startsWith("java.io.FileNotFoundException")) { // debug; when run from appletviewer, the http connection // is not available so write the file content if (path.endsWith(".draw") || path.endsWith(".map")) System.out.println(content); } else showStatus(replyString); return false; } else { showStatus(url + " saved"); return true; } }