1 Implement a class Myinputstream read a file, and cannot throw an exception
Public classTestdemo { Public Static voidMain (string[] args) throws Exception {InputStreaminch=NewMyinputstream ("D:/a/a.txt"); byte[] B =New byte[1024x768]; intLen =0; while((len=inch. Read (b))!=-1) {String s=NewString (b,0, Len); System.err.print (s); } inch. Close (); }}
classMyinputstream extends InputStream {//becomes a subclass of InputStream, which is a. PrivateInputStreaminch; PublicMyinputstream (String fileName) {Try { inch=NewFileInputStream (fileName); } Catch(FileNotFoundException e) {e.printstacktrace (); } } Public intReadbyte[] b) { intlen=-1; Try{len=inch. Read (b); } Catch(IOException e) {e.printstacktrace (); } returnLen; } Public voidClose () {Try { inch. Close (); } Catch(IOException e) {e.printstacktrace (); }} @Override Public intread () throws IOException {return 0; }}
2 The following modifications to the Close method are implemented by wrapping [W1] to reclaim the connection
1: Implement connection interface, have a member of connection.
2: Modify the Close method.
3: All other methods call the connection of the member variable.
Public classmyDataSource implements DataSource {PrivateLinkedlist<connection> pool =NewLinkedlist<connection>(); PublicmyDataSource () {Try{class.forname ("Com.mysql.jdbc.Driver"); String URL="Jdbc:mysql:///db909?characterencoding=utf8"; for(inti =0; I <3; i++) { //Create a native connection,//[email protected]Connection con = drivermanager.getconnection (URL,"Root", "1234"); //declaring wrapper classesMyConn conn =Newmyconn (con); POOL.ADD (conn);//Add the wrapper class to the pool } } Catch(Exception e) {e.printstacktrace (); } } //This method comes from DataSource, which is used to return a connection PublicConnection getconnection () throws SQLException {synchronized (pool) {if(pool.size () = =0) { Try{pool.wait (); } Catch(interruptedexception e) {e.printstacktrace (); } returngetconnection (); } Connection con=Pool.removefirst (); System.err.println ("siize:"+pool.size ()); returncon; } }
The following packaging connection
classMyConn implements Connection {//declaring a member of a wrapped class PrivateConnection Conn;//[email protected]//by constructing an object that receives MySQL connection [email protected] Publicmyconn (Connection con) { This. conn =con; } //Close Connection Public voidClose () throws SQLException {synchronized (pool) {//someone called the Close method, cannot close theSystem.err.println ("someone also connected to the .... "+ This); Pool.add ( This); Pool.notify (); } }}
3, with packaging processing get way of garbled
Package Cn.itcast.utils;import Java.io.ioexception;import java.lang.reflect.method;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletrequestwrapper;import Javax.servlet.http.HttpServletResponse; Public classBaseservlet extends HttpServlet {@Override Public voidService (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {RE Q.setcharacterencoding ("UTF-8"); String MethodName= Req.getparameter ("cmd"); Try{Method mm= This. GetClass (). GetMethod (methodname,httpservletrequest.class, HttpServletResponse.class); //declaring wrapper classesMyrequest Mr =Newmyrequest (req); Mm.invoke ( This, MR,RESP); }Catch(Exception e) {e.printstacktrace (); } }}//Package RequestclassMyrequest extends httpservletrequestwrapper{PrivateHttpServletRequest req; Publicmyrequest (HttpServletRequest request) {super (request); This. req=request; } //Modify the GetParameter method@Override Publicstring GetParameter (string name) {String Value=Req.getparameter (name); if(Req.getmethod (). Equals ("GET") {System.err.println ("transcoding"); Try{Value=NewString (Value.getbytes ("iso-8859-1"),"UTF-8"); }Catch(Exception e) {}}returnvalue; }}
Summarize:
1: An agent or wrapper is an enhancement to the method of a class.
Proxy: The subclass of this interface must be created in memory based on the given interface. $Proxy 0.
Wrapper: The interface is not required, but the declaration declares a class that becomes a subclass of the wrapped class and has a member of the wrapped class.
2: Agent Base code:
Object proxyedobj =
Proxy.newproxyinstance (ClassLoader,
New class[]{The interface array of the class being proxied. Class},
New Invocationhandler () {//execute handle
public object Invode (object Proxy, Method methods reflection, object[] args) {
Reutrn Method.invode (by proxy class, args);
}
}
3: Packing:
If a class is a wrapper class for a class, then:
A extends b{
Privet b b;
}
4: Under what circumstances, use the packaging, and under what circumstances use the agent
If the official (SUN) provides a wrapper class adapter, the wrapper should be used preferentially. such as HttpServletRequest, its packaging class is htpservletrequestwraper.
If the official does not provide an adapter for the wrapper class, you can use dynamic proxies. such as connection.
Java Learning Notes-Implementing a Class Myinputstream (28)