handler.java
来自「exploring Java 2ed edition 附带的例子程序」· Java 代码 · 共 61 行
JAVA
61 行
package exploringjava.protocolhandlers.crypt;import java.io.*;import java.net.*;public class Handler extends URLStreamHandler { protected void parseURL(URL url, String spec, int start, int limit) { int slash = spec.indexOf('/'); String crypType = spec.substring(start, slash-1); super.parseURL(url, spec, slash, limit); setURL( url, "crypt:"+crypType, url.getHost(), url.getPort(), url.getFile(), url.getRef() ); } protected URLConnection openConnection(URL url) throws IOException { String crypType = url.getProtocol().substring(6); return new CryptURLConnection( url, crypType ); }}class CryptURLConnection extends URLConnection { static int defaultPort = 80; CryptInputStream cis; public String getContentType() { return guessContentTypeFromName( url.getFile() ); } CryptURLConnection ( URL url, String crypType ) throws IOException { super( url ); try { String classname = "exploringjava.protocolhandlers.crypt." + crypType + "CryptInputStream"; cis = (CryptInputStream)Class.forName(classname).newInstance(); } catch ( Exception e ) { throw new IOException("Crypt Class Not Found: "+e); } } public void connect() throws IOException { int port = ( url.getPort() == -1 ) ? defaultPort : url.getPort(); Socket s = new Socket( url.getHost(), port ); // Send the filename in plaintext OutputStream server = s.getOutputStream(); new PrintWriter( new OutputStreamWriter( server, "8859_1" ), true ).println( "GET " + url.getFile() ); // Initialize the CryptInputStream cis.set( s.getInputStream(), server ); connected = true; } public InputStream getInputStream() throws IOException { if (!connected) connect(); return ( cis ); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?