子非魚(yú)

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            21 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks
           1/**
           2 * 部分類的copy方法實(shí)現(xiàn)
           3 *
           4 */

           5public class CopyFactoryImpl implements CopyFactory{
           6
           7    public Object copy(Object from){
           8        if(from != null){
           9//            if(from instanceof Params)
          10//                return copyParams((Params)from);
          11//            if(from instanceof Value)
          12//                return copyValue((Value)from);
          13//            if(from instanceof OvertimePolicies)
          14//                return copyPolicies((OvertimePolicies)from);
          15//            if(from instanceof Event)
          16//                return copyEvent((Event)from);
          17            
          18            return copyObject(from);//from應(yīng)Serialization
          19            
          20        }

          21        
          22        return null;
          23    }

          24    
          25
          26    /**
          27     * 緩存復(fù)制方式拷貝
          28     * @param from
          29     * @return
          30     */

          31    public Object copyObject(Object from){
          32       try{
          33          // 在內(nèi)存中開(kāi)辟一塊緩沖區(qū),用于將源對(duì)象寫(xiě)入
          34          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          35          ObjectOutputStream out = new ObjectOutputStream(bout);
          36          //通過(guò)Serialization機(jī)制將自身寫(xiě)入該緩沖區(qū)
          37          out.writeObject(from);
          38          out.close();
          39
          40          // 找到剛才開(kāi)辟的緩沖區(qū)準(zhǔn)備讀取
          41          ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
          42          ObjectInputStream in = new ObjectInputStream(bin);
          43          //將剛才寫(xiě)入的內(nèi)容讀入目標(biāo)對(duì)象
          44          Object target = in.readObject();
          45          in.close();
          46
          47          //返回目標(biāo)對(duì)象,拷貝完畢
          48          return target;
          49       }
          catch (Exception e){
          50          return null;
          51       }

          52    }

          53}
          posted on 2007-07-25 16:51 子非魚(yú) 閱讀(255) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 瑞安市| 玉龙| 宁陵县| 新巴尔虎左旗| 仁化县| 永登县| 阿拉善盟| 三亚市| 平山县| 卢氏县| 保康县| 民丰县| 陵水| 隆德县| 九龙县| 同心县| 鸡西市| 呼和浩特市| 玉环县| 白山市| 环江| 武邑县| 梧州市| 涟源市| 上杭县| 清远市| 丹东市| 苍梧县| 三明市| 白河县| 海伦市| 达州市| 苏尼特右旗| 鹿泉市| 永安市| 临清市| 沧州市| 徐州市| 台州市| 荆州市| 宜阳县|