📄 classserver.java
字号:
/* * @(#)ClassServer.java 1.2 00/06/21 * * Copyright 1995-1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */import java.io.*;import java.net.*;import javax.net.*;/* * ClassServer.java -- a simple file server that can serve * Http get request in both clear and secure channel *//** * Based on ClassServer.java in tutorial/rmi */public abstract class ClassServer implements Runnable { private ServerSocket server = null; /** * Constructs a ClassServer based on <b>ss</b> and * obtains a file's bytecodes using the method <b>getBytes</b>. * */ protected ClassServer(ServerSocket ss) { server = ss; newListener(); } /** * Returns an array of bytes containing the bytes for * the file represented by the argument <b>path</b>. * * @return the bytes for the file * @exception FileNotFoundException if the file corresponding * to <b>path</b> could not be loaded. * @exception IOException if error occurs reading the class */ public abstract byte[] getBytes(String path) throws IOException, FileNotFoundException; /** * The "listen" thread that accepts a connection to the * server, parses the header to obtain the file name * and sends back the bytes for the file (or error * if the file is not found or the response was malformed). */ public void run() { Socket socket; // accept a connection try { socket = server.accept(); } catch (IOException e) { System.out.println("Class Server died: " + e.getMessage()); e.printStackTrace(); return; } // create a new thread to accept the next connection newListener(); try { DataOutputStream out = new DataOutputStream(socket.getOutputStream()); try { // get path to class file from header DataInputStream in = new DataInputStream(socket.getInputStream()); String path = getPath(in); // retrieve bytecodes byte[] bytecodes = getBytes(path); // send bytecodes in response (assumes HTTP/1.0 or later) try { out.writeBytes("HTTP/1.0 200 OK\r\n"); out.writeBytes("Content-Length: " + bytecodes.length + "\r\n"); out.writeBytes("Content-Type: text/html\r\n\r\n"); out.write(bytecodes); out.flush(); } catch (IOException ie) { ie.printStackTrace(); return; } } catch (Exception e) { e.printStackTrace(); // write out error response out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n"); out.writeBytes("Content-Type: text/html\r\n\r\n"); out.flush(); } } catch (IOException ex) { // eat exception (could log error to log file, but // write out to stdout for now). System.out.println("error writing response: " + ex.getMessage()); ex.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { } } } /** * Create a new thread to listen. */ private void newListener() { (new Thread(this)).start(); } /** * Returns the path to the file obtained from * parsing the HTML header. */ private static String getPath(DataInputStream in) throws IOException { String line = in.readLine(); String path = ""; // extract class from GET line if (line.startsWith("GET /")) { line = line.substring(5, line.length()-1).trim(); int index = line.indexOf(' '); if (index != -1) { path = line.substring(0, index); } } // eat the rest of header do { line = in.readLine(); } while ((line.length() != 0) && (line.charAt(0) != '\r') && (line.charAt(0) != '\n')); if (path.length() != 0) { return path; } else { throw new IOException("Malformed Header"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -