<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    紀(jì)念SUN

    Powered By Andy

    2011年11月18日

    package cn.xx

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Arrays;

    /**
     * <pre>
     * @Types   role : 測(cè)試clone

     * @Create  time : 2011-12-26 : 上午10:49:39
     * @ver     curr : 1.0
     * </pre>
     */
    public class TestClone implements Serializable{

        private static final long serialVersionUID = -7340886443308126418L;

        /**
         * <pre>
         * @Methods role : 對(duì)象的Clone
         * 注: 要clone對(duì)象必須要實(shí)現(xiàn)Serializable接口, 不然拋NoSerializableException
         * </pre>
         * @param obj
         * @return
         * @throws IOException
         * @throws ClassNotFoundException
         * @Create  time : 2011-12-23 : 下午5:26:57
         * @ver     curr : 1.0
         */
        @SuppressWarnings("unchecked")
        public static <T> T invokeCopy(T obj) throws IOException, ClassNotFoundException{
            //T newObj = null;
            // write object to memory
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            
            // read object come from memory
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            //newObj = (T)ois.readObject();
            
            //bis.close();
            //oos.close();
            // return clone object
            return (T)ois.readObject();
            
            
        }
        
        /**
         * <pre>
         * @Methods role : 實(shí)現(xiàn)對(duì)象的clone
         * 兩種方案(二選一實(shí)現(xiàn)都可以):
         * 1: 實(shí)現(xiàn)Cloneable接口, 深度clone自己重寫clone()實(shí)現(xiàn),此方法只實(shí)現(xiàn)淺度clone
         * 2: 實(shí)現(xiàn)Serializable接口
         * @param obj
         * @return
         * @throws NoSuchMethodException
         * @throws SecurityException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         * @throws InvocationTargetException
         * @throws ClassNotFoundException
         * @throws IOException
         * @throws CloneNotSupportedException
         * @Create  time : 2011-12-26 : 上午10:31:37
         * @ver     curr : 1.0
         * </pre>
         */
        public static<T> T clone(T obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IOException, CloneNotSupportedException{
            // is null
            if(null == obj) return null;
            
            // is instanceof Cloneable
            if(obj instanceof Cloneable){
                return invokeClone(obj);
            }
            // is instanceof Serializable
            else if(obj instanceof Serializable){
                return invokeCopy(obj);
            }
            
            // is not supported clone
            throw new java.lang.CloneNotSupportedException();
        }

        @SuppressWarnings("unchecked")
        public static <T> T invokeClone(T obj) throws NoSuchMethodException,
                IllegalAccessException, InvocationTargetException {
            Class<? extends Object> classObject = obj.getClass();
            Method method = classObject.getDeclaredMethod("clone");
            return (T)method.invoke(obj);
        }
        
        public static void main(String[] args) throws ClassNotFoundException, IOException {
            String x = "123";
            String v = invokeCopy(x);
            
            String[][] array = {{"1", "2"},{ "3", "2","2","3"},{"V","3"}}, array2;
            
            
            System.out.println(x);
            System.out.println(v);
            array2 = invokeCopy(array);
            System.out.println(Arrays.deepToString(array2));
            
            int[] a = {1,48,2}, b = {1, 20, 19};
            System.out.println("a --- hashCode: " + a.hashCode() + "---b: hashCode:" + b.hashCode());
            b = invokeCopy(a);
            System.out.println(b.hashCode());
            System.out.println(Arrays.toString(b));
            short age = 25;
            Person p = new TestClone(). new Person(1l, "andy", age), ps, pe;
            
            ps = invokeCopy(p);
            
            System.out.println(ps);
            
            try {
                //pe = clone(p);
                pe = invokeClone(p);
                System.out.println(pe);
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        class Person implements Serializable{//, Cloneable{
            private static final long serialVersionUID = 7605971168826706980L;
            private Long id;
            private String name;
            private short age;
            
            
            public Person() {
            }
            
            
            public Person(Long id, String name, short age) {
                super();
                this.id = id;
                this.name = name;
                this.age = age;
            }


            public Long getId() {
                return id;
            }
            public void setId(Long id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public short getAge() {
                return age;
            }
            public void setAge(short age) {
                this.age = age;
            }


            public String toString() {
                return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
            }
            
            /*protected Object clone() throws CloneNotSupportedException {
                // TODO Auto-generated method stub
                return super.clone();
            }*/
            
        }
    }
    posted @ 2011-12-26 10:54 Powered By Andy 閱讀(261) | 評(píng)論 (0)編輯 收藏
    package cn.webmctv.test;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;

    public class TestInputStream {

        public static void main(String[] args) {
            /* 為什么要關(guān)閉和io流, 由于java底層是用c實(shí)現(xiàn)的, 所以當(dāng)我們不停的調(diào)用new InputStream -> impl
             * 時(shí)候, c打開(kāi)的文件會(huì)一直沒(méi)有關(guān)閉,而導(dǎo)致文件刪除不了,別的程序訪問(wèn)不了的問(wèn)題,和操作系統(tǒng)打開(kāi)文件
             * 超過(guò)最大數(shù)異常。而下面new FileInputStream(new File("c:/q.txt"));這種方式?jīng)]有關(guān)閉c打開(kāi)的
             * 文件一直new 就會(huì)出現(xiàn)打開(kāi)文件太多異常。
            short count = 0;
            InputStream inStream = null;
                try {
                    for (int i = 0; i < Short.MAX_VALUE; i++) {
                        //inStream.
                        inStream = new FileInputStream(new File("/root/install.log"));
                        //count ++;
                        System.out.println("count: " + count++);
                    }
                    //p.load(inStream);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally{
                    if(inStream != null) try{ inStream.close(); } catch(IOException e){};
                }
            
            System.out.println(Short.MAX_VALUE);
            */
            short count = 0;
            InputStream inStream = null;
            for (int i = 0; i < Short.MAX_VALUE; i++) {
                try {
                        //inStream.
                        inStream = new FileInputStream(new File("/root/install.log"));
                        //count ++;
                        System.out.println("count: " + count++);
                    
                    //p.load(inStream);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally{
                    if(inStream != null) try{ inStream.close(); } catch(IOException e){};
                }
            }
            
            System.out.println(Short.MAX_VALUE);
            
        }
    }
    posted @ 2011-11-18 10:09 Powered By Andy 閱讀(3224) | 評(píng)論 (0)編輯 收藏

    導(dǎo)航

    <2011年11月>
    303112345
    6789101112
    13141516171819
    20212223242526
    27282930123
    45678910

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 久久久久久亚洲精品无码| 亚洲一区二区三区香蕉| 亚洲日韩区在线电影| a成人毛片免费观看| 亚洲午夜福利717| 亚洲成年网站在线观看| 毛片免费在线观看网站| 免费午夜爽爽爽WWW视频十八禁| 国产AV无码专区亚洲AV漫画| rh男男车车的车车免费网站| 最新中文字幕免费视频| 亚洲熟女综合色一区二区三区| 99久久免费国产精品热| 亚洲大尺度无码无码专区| 极品色天使在线婷婷天堂亚洲| 久9热免费精品视频在线观看| 国产yw855.c免费视频| 香蕉视频免费在线| 综合亚洲伊人午夜网| 一级毛片免费毛片一级毛片免费| 国产精品色午夜视频免费看| 久久久久久a亚洲欧洲AV| 91高清免费国产自产| 亚洲色大成网站www永久男同| 美女内射无套日韩免费播放| 亚洲国产成人手机在线电影bd| 日本一区二区免费看| 亚洲免费观看在线视频| 免费国产成人高清视频网站| 成全视频高清免费观看电视剧| 久久精品国产精品亚洲| 日本不卡免费新一区二区三区| 亚洲中文字幕无码爆乳AV| 69视频免费观看l| 久久久久亚洲AV片无码下载蜜桃| 亚洲乱码av中文一区二区| 亚洲 自拍 另类小说综合图区 | 日韩精品久久久久久免费| 亚洲区视频在线观看| 亚洲国产专区一区| 99re免费在线视频|