Java learning notes-implement a class of MyInputStream (28), java reads inputstream
1. Implement a class MyInputStream to read files, and do not throw an exception
public class TestDemo { public static void main(String[] args) throws Exception { InputStream in = new MyInputStream("d:/a/a.txt"); byte[] b = new byte[1024]; int len = 0; while((len=in.read(b))!=-1){ String s = new String(b,0,len); System.err.print(s); } in.close(); }}
Class MyInputStream extends InputStream {// becomes a subclass of inputstream, that is, is. private InputStream in; public MyInputStream (String fileName) {try {in = new FileInputStream (fileName);} catch (FileNotFoundException e) {e. printStackTrace () ;}} public int read (byte [] B) {int len =-1; try {len = in. read (B);} catch (IOException e) {e. printStackTrace ();} return len;} public void close () {try {in. close ();} catch (IOException e) {e. printStackTrace () ;}@ Override public int read () throws IOException {return 0 ;}}
2 or less packaged[W1]Modifies the close method to recycle connections.
1: implements the Connection interface and has a Connection Member.
2: Modify the close method.
3: All other methods call the connection of the member variable.
Public class MyDataSource implements DataSource {private DataSource list <Connection> pool = new DataSource list <Connection> (); public MyDataSource () {try {Class. forName ("com. mysql. jdbc. driver "); String url =" jdbc: mysql: // db909? CharacterEncoding = UTf8 "; for (int I = 0; I <3; I ++) {// create a native connection, // com. mysql. jdbc. JDBC4Connection @ 8888 Connection con = DriverManager. getConnection (url, "root", "1234"); // declare the packaging class MyConn conn = new MyConn (con); pool. add (conn); // add the packaging class to the pool} catch (Exception e) {e. printStackTrace () ;}// this method is from datasource and is used to return a Connection public Connection getConnection () throws SQLException {synchronized (pool) {if (pool. size () = 0) {try {pool. wait ();} catch (InterruptedException e) {e. printStackTrace ();} return getConnection ();} Connection con = pool. removeFirst (); System. err. println ("siize:" + pool. size (); return con ;}}
The following package connection
Class MyConn implements Connection {// declare the private Connection conn of the encapsulated class; // com. mysql. jdbc. jdbc4Connection @ 1111 // construct the JDBC4Connection object Jdbc4Connection @ 8888 public MyConn (connection con) {this. conn = con;} // close the connection public void close () throws SQLException {synchronized (pool) {// someone has called the close method and cannot close the System. err. println ("someone connected .... "+ This); pool. add (this); pool. Sort y ();}}}
3. Use packaging to handle garbled characters in get Mode
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 class BaseServlet extends HttpServlet {@ Override public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req. setCharacterEncoding ("UTF-8"); String methodName = req. getParameter ("cmd"); try {Method mm = this. getClass (). getMethod (methodName, HttpServletRequest. class, HttpServletResponse. class); // declare the packaging class MyRequest mr = new MyRequest (req); mm. invoke (this, mr, resp);} catch (Exception e) {e. printStackTrace () ;}}// package requestclass MyRequest extends HttpServletRequestWrapper {private HttpServletRequest req; public MyRequest (HttpServletRequest request) {super (request); this. req = request;} // modify the getparameter method @ Override public String getParameter (String name) {String value = req. getParameter (name); if (req. getMethod (). equals ("GET") {System. err. println ("transcoding"); try {value = new String (value. getBytes ("ISO-8859-1"), "UTF-8");} catch (Exception e) {}} return value ;}}
Summary:
1: proxy or packaging is to enhance the method of a class.
Proxy: You must create a subclass of this interface in the memory based on the given interface. $ Proxy0.
Packaging: APIs are not required, but a class is declared and becomes a subclass of the encapsulated class. It also has a member of the encapsulated class.
2: Basic proxy code:
Object proxyedObj =
Proxy. newProxyInstance (ClassLoader,
New class [] {interface array of the class to be proxy. class },
New InvocationHandler () {// execution handle
Public Object invode (Object proxy, Method reflection, object [] args ){
Reutrn method. invode (proxy class, args );
}
}
3: packaging:
If a class is a packaging class of a class, then:
A extends B {
Privet B;
}
4: When, when, and when
If the official SUN provides a packaging adapter, packaging should be preferred. For example, HttpServletRequest, its packaging class is HtpServletRequestWraper.
If the packaging class adapter is not officially provided, you can use a dynamic proxy. Such as Connection.