国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java學習筆記—實現一個類MyInputStream(28)

2019-11-14 23:40:43
字體:
來源:轉載
供稿:網友
java學習筆記—實現一個類MyInputStream(28)

1實現一個類MyInputStream讀取文件,且不能拋出異常

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 {  //成為inputstream的子類,即is a.    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以下通過包裝實現對close方法的修改,以回收連接

1:實現Connection接口,擁有一個Connection的成員。

 2:修改close方法。

 3:其他的方法都調用成員變量的connection。

public class MyDataSource implements DataSource  {    private LinkedList<Connection> pool = new LinkedList<Connection>();    public MyDataSource() {        try {            Class.forName("com.MySQL.jdbc.Driver");            String url = "jdbc:mysql:///db909?characterEncoding=UTf8";            for (int i = 0; i < 3; i++) {                 //創建原生的連接,// com.mysql.jdbc.JDBC4Connection@8888                Connection con = DriverManager.getConnection(url, "root",                        "1234");                //聲明包裝類                MyConn conn = new MyConn(con);                 pool.add(conn);//將包裝類添加到池中去            }        } catch (Exception e) {            e.printStackTrace();        }    }    //此方法來自于datasource,用于返回一個連接    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;        }    }

以下包裝connection

class MyConn implements Connection  {        // 聲明被包裝類的成員        private Connection conn; //com.mysql.jdbc.Jdbc4Connection@1111        // 通過構造接收MySql的connection的對象JDBC4Connection@8888        public MyConn(Connection con) {            this.conn = con;        }         //關閉連接        public void close() throws SQLException {            synchronized (pool) {                //有人調用了關閉方法,不能關                System.err.println("有人還連接了。。。。"+this);                pool.add(this);                pool.notify();            }        }}

3、用包裝處理get方式的亂碼

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);            //聲明包裝類            MyRequest mr = new MyRequest(req);            mm.invoke(this,mr,resp);        }catch(Exception e){            e.printStackTrace();        }    }}//包裝requestclass MyRequest extends HttpServletRequestWrapper{    private HttpServletRequest req;    public MyRequest(HttpServletRequest request) {        super(request);        this.req=request;    }    //修改getparameter方法    @Override    public String getParameter(String name) {        String value = req.getParameter(name);        if(req.getMethod().equals("GET")){            System.err.println("轉碼");            try{                value = new String(value.getBytes("ISO-8859-1"),"UTF-8");            }catch(Exception e){            }        }        return value;    }}

總結:

1:代理或是包裝都是對某個類的方法進行增強。

代理:必須要根據給定的接口,在內存中創建這個接口的子類。$Proxy0。

包裝:不需要接口,但聲明聲明一個類,變成被包裝類的子類,同時擁有一個被包裝類的成員。

2:代理基本代碼:

Object proxyedObj =

Proxy.newProxyInstance(ClassLoader,

New class[]{被代理的類的接口數組.class},

New InvocationHandler(){//執行句柄

Public Object invode(Object 代理,Method 方法反射,object[] args){

Reutrn method.invode(被代理類,args);

}

}

3:包裝:

如果一個類是某個類的包裝類,則:

A extends B{

Privet B b;

}

4:什么情況下,使用包裝,什么情況下使用代理

如果官方(SUN)提供了包裝類適配器,則應該優先使用包裝。如HttpServletRequest,它的包裝類就是HtpServletRequestWraper.

如果官方沒有提供包裝類的適配器,則可以使用動態代理。如Connection。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南投县| 斗六市| 兖州市| 隆安县| 曲阳县| 辉县市| 玉溪市| 东丰县| 凤凰县| 阿瓦提县| 顺昌县| 金山区| 墨江| 楚雄市| 正阳县| 涟水县| 保山市| 伊金霍洛旗| 中江县| 长丰县| 九寨沟县| 广西| 江陵县| 烟台市| 英山县| 边坝县| 庆阳市| 平罗县| 武强县| 灵寿县| 山丹县| 宣汉县| 四会市| 永康市| 乳源| 新绛县| 新竹市| 张北县| 新竹市| 大英县| 西林县|