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

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

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

    posts - 51, comments - 17, trackbacks - 0, articles - 9
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    java 3322

    Posted on 2007-04-11 21:41 chenweicai 閱讀(167) 評論(0)  編輯  收藏

    1.
    import java.net.InetAddress;
    import java.net.UnknownHostException;

    public class NetWork {

     //static fields and static block
     
        //InetAddress of the local host
     private static InetAddress local = null;
     
     //integer version of the local host
     private static int packedLocal = 0;
     
     static {
      try{
       local = InetAddress.getLocalHost();
      }catch(UnknownHostException e){
       e.printStackTrace();
      }

      System.out.println("localhost : " + local);
      packedLocal = translate(local);
     }
     
     public static InetAddress getLocalHost(){
      return local;
     }
     
     public static String getLocalHostName(){
      return local.getHostName();
     }
     
     public static String getLocalMachineName(){
      String hostname = local.getHostName();
      int machEndIndex = hostname.indexOf('.');
      if(machEndIndex == -1)
       return hostname;
      else
       return hostname.substring(0, machEndIndex);
     }
     
     public static boolean isLocal(InetAddress address){
      return local.equals(address);
     }
     
     public static boolean isLocal(int packedAddress){
      return packedAddress == packedLocal;
     }
     
     /**
      * return an integer representation of the specified
      * InetAddress. This is used for efficiency
      */
     public static int translate(InetAddress address){
      byte[] ad = address.getAddress();//原始IP地址
      if(ad.length != 4)
       throw new IllegalArgumentException("only accept 32-byte IP address");
      int packedAddress = ((ad[0] << 24) & 0xFF000000) |
           ((ad[1] << 16) & 0xFF0000) |
           ((ad[2] <<  8) & 0xFF00) |
           (ad[3] & 0xFF);
      return packedAddress;
     }
     
     /**
      * return an InetAddress representation of the specified integer.
      * This is used for performance improvements in serivalization.
      */
     public static InetAddress translate (int packed)
      throws UnknownHostException{
      int[] ad = new int[4];
         ad[0] = (packed  >>> 24);
         ad[1] = ((packed >>> 16) & 0x000000FF);
         ad[2] = ((packed >>>  8) & 0x000000FF);
         ad[3] = (packed          & 0x000000FF);
         StringBuffer buf = new StringBuffer();
         buf.append(ad[0]);
         buf.append(".");
         buf.append(ad[1]);
         buf.append(".");
         buf.append(ad[2]);
         buf.append(".");
         buf.append(ad[3]);
        
         return InetAddress.getByName(buf.toString());
     }
     
     public static String translateToString(int packed){
      int[] ad = new int[4];
      ad[0] = (packed >>> 24);
      ad[1] = ((packed >>> 16) & 0x000000FF);
      ad[2] = ((packed >>> 8) & 0x000000FF);
      ad[3] = (packed & 0x000000FF);
      
      StringBuffer buf = new StringBuffer();
      buf.append(ad[0]);
      buf.append(".");
      buf.append(ad[1]);
      buf.append(".");
      buf.append(ad[2]);
      buf.append(".");
      buf.append(ad[3]);
      
      return buf.toString();
     }
     
     public static void main(String[] args){
      InetAddress localhost = getLocalHost();
      System.out.println("Local Host Name is : " + getLocalHostName());
      System.out.println("Local Host Machine Name is : " + getLocalMachineName());
      if(isLocal(localhost))
       System.out.println(localhost.getHostName() + " is Local Host.");
      int intforaddress = translate(localhost);
      System.out.println("localhost integer representation is " + intforaddress);
      System.out.println("localhost string representation is " + translateToString(intforaddress));
      System.out.println(translateToString(intforaddress).toString());
      
     }
    }



    2. public interfece Externalizable extendx Serializable
    Externalizable 實例類的惟一特性是可以被寫入序列化流中,該類負責保存和恢復實例內容。 若某個要完全控制某一對象及其超類型的流格式和內容,則它要實現 Externalizable 接口的 writeExternal 和 readExternal 方法。這些方法必須顯式與超類型進行協調以保存其狀態。這些方法將代替自定義的 writeObject 和 readObject 方法實現。
    Serialization 對象將使用 Serializable 和 Externalizable 接口。對象持久性機制也可以使用它們。要存儲的每個對象都需要檢測是否支持 Externalizable 接口。如果對象支持 Externalizable,則調用 writeExternal 方法。如果對象不支持 Externalizable 但實現了 Serializable,則使用 ObjectOutputStream 保存該對象。
    在重構 Externalizable 對象時,先使用無參數的公共構造方法創建一個實例,然后調用 readExternal 方法。通過從 ObjectInputStream 中讀取 Serializable 對象可以恢復這些對象。
    Externalizable 實例可以通過 Serializable 接口中記錄的 writeReplace 和 readResolve 方法來指派一個替代對象。

    writeExternal

    void writeExternal(ObjectOutput out)
    throws IOException
    該對象可實現 writeExternal 方法來保存其內容,它可以通過調用 DataOutput 的方法來保存其基本值,或調用 ObjectOutput 的 writeObject 方法來保存對象、字符串和數組。

     

    參數:
    out - 要寫入對象的流
    拋出:
    IOException - 包含可能發生的所有 I/O 異常

    readExternal

    void readExternal(ObjectInput in)
    throws IOException,
    ClassNotFoundException
    對象實現 readExternal 方法來恢復其內容,它通過調用 DataInput 的方法來恢復其基礎類型,調用 readObject 來恢復對象、字符串和數組。readExternal 方法必須按照與 writeExternal 方法寫入值時使用的相同順序和類型來讀取這些值。

     

    參數:
    in - 為了恢復對象而從中讀取數據的流
    拋出:
    IOException - 如果發生 I/O 錯誤
    ClassNotFoundException - 如果無法找到需要恢復的某個對象的類。

    import java.io.Externalizable;
    import java.net.InetAddress;

    public interface EndPoint extends Externalizable {

     /**
      *
      * @return Returns the IP address contained in this endpoint.
      */
     public InetAddress getAddress();
     
     /**
      * returns the IP address contained in this endpoint.
      * enclosed in an integer value.
      */
     public int getIntAddress();
     
     /**
      * Returns the port number contained in this endpoint.
      */
     public int getPort();
     
     
     /**
      * Return true if this endpoint is the local endpoint.
      */
     public boolean isLocal();
     
     /**
      * Returns true if this endpoint is a multicast endpoint.
      */
     public boolean isMulticastEndPoint();
    }



    import internet.network.NetWork;

    import java.io.Externalizable;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutput;
    import java.io.ObjectOutputStream;
    import java.net.InetAddress;
    import java.net.UnknownHostException;

    public class EndPointImpl implements EndPoint, Comparable, Externalizable{

     //Constants
     
     /* Size of this object (in bytes) when marshalled. */
     public static final int SIZE = 6;
     
     //Fields
     protected InetAddress address;
     
     private int packedAddress;
     
     private int port;
     
     protected boolean local;
     
     //Constructors
     
     /**
      * Default constructor for externalization
      */
     public EndPointImpl(){
      
     }
     
     public EndPointImpl(InetAddress address, int port){
      this.address = address;
      this.port = port;
      packedAddress = NetWork.translate(address);
      local = NetWork.isLocal(packedAddress);
     }
     
     public EndPointImpl(int packedAddress, int port){
      this.packedAddress = packedAddress;
      this.port = port;
      this.local = NetWork.isLocal(packedAddress);
      //The InetAddress version of the address is computed
      //only when needed, to avoid uselesss DNS lookups.
      address = null;
     }
     
     public InetAddress getAddress() {
      if(address == null)
       try{
        address = NetWork.translate(packedAddress);
       }catch(UnknownHostException e){
        System.out.println(e.getMessage());
       }
       return address;
     }

     public int getIntAddress() {
      return this.packedAddress;
     }

     public int getPort() {
      return this.port;
     }

     public boolean isLocal() {
      return this.local;
     }

     public boolean isMulticastEndPoint() {
      return address.isMulticastAddress();
     }
     
     
     ////////////////////////////////////////////////////////////
     // Methods from Externalizable
     ////////////////////////////////////////////////////////////

     /**
      * Restores the content of this object form the marshaled data contained
      * in the specified input stream.
      *
      * @param in the stream to be read.
      */
     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
      
      packedAddress = in.readInt();
      port = (int) in.readUnsignedShort();
      local = NetWork.isLocal(packedAddress);
      address = null;
     }

     /**
      * Marshals the content of this object to the specified output stream.
      *
      * @param out the stream to be written
      */
     public void writeExternal(ObjectOutput out) throws IOException {
      
      out.writeInt(packedAddress);
      out.writeShort(port);
     }

     
     ////////////////////////////////////////////////////////////
     // Methods from Object and Comparable
     ////////////////////////////////////////////////////////////
     /**
      *  Compares this object with the specified object for order.
      */
     public int compareTo(Object obj) {
      if(this == obj){
       return 0;
      }
      else{
       EndPointImpl endpoint = (EndPointImpl) obj;
       if(packedAddress < endpoint.packedAddress){
        return -1;
       }
       else if(packedAddress == endpoint.packedAddress){
        if(port < endpoint.port)
         return -1;
        else if(port == endpoint.port)
         return 0;
        else
         return 1;
       }
       else{
        return 1;
       }
      }
     }
     
     public int hashcode(){
      return packedAddress ^ port;
     }

     /**
      * Compares two EndPointImpl objects for content equality.
      */
     public boolean equals (Object obj){
      if(!(obj instanceof EndPointImpl))
       return false;
      EndPointImpl endpoint = (EndPointImpl) obj;
      return (endpoint.packedAddress == packedAddress && endpoint.port == port);
     }
     
     /**
      * Returns a string representation of this object.
      */
     public String toString(){
      StringBuffer buf = new StringBuffer();
      buf.append("[");
      buf.append(NetWork.translateToString(packedAddress));
      buf.append(":");
      buf.append(port);
      buf.append("]");
      
      return buf.toString();
     }
     
     
     public static void main(String[] args) throws FileNotFoundException, IOException{
      InetAddress localhost = NetWork.getLocalHost();
      EndPointImpl endpoint = new EndPointImpl(localhost, 100);
      System.out.println("This EndPoint is : " + endpoint.toString());
      EndPointImpl endpoint2 = new EndPointImpl();
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("endpoint.txt"));
      try {
       endpoint.writeExternal(oos);
      } catch (IOException e) {
       e.printStackTrace();
      }
      oos.close();
      
      ObjectInputStream ios = new ObjectInputStream(new FileInputStream("endpoint.txt"));
      try {
       endpoint2.readExternal(ios);
      } catch (IOException e) {
       e.printStackTrace();
      } catch (ClassNotFoundException e) {
       e.printStackTrace();
      }
      ios.close();
      System.out.println("This EndPoint is : " + endpoint2.toString());
     }
    }


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 两个人日本免费完整版在线观看1| 成人精品国产亚洲欧洲| 久久免费精品视频| 久久精品国产亚洲香蕉| 99久久免费观看| 亚洲成色在线影院| 国产婷婷成人久久Av免费高清 | 亚洲精品一二三区| 国产在线观看麻豆91精品免费| 亚洲免费在线视频| 日本免费人成在线网站| 亚洲综合校园春色| 爽爽日本在线视频免费| 免费在线观看亚洲| 亚洲精品综合久久| 免费无码H肉动漫在线观看麻豆| 亚洲级αV无码毛片久久精品| 美女无遮挡拍拍拍免费视频| 亚洲成av人无码亚洲成av人| 国产精品免费一级在线观看| 免费高清A级毛片在线播放| 国产亚洲精品精品国产亚洲综合| 国产成人免费ā片在线观看老同学| 亚洲AV人无码综合在线观看| 99久久久国产精品免费无卡顿 | 国产精品久久亚洲一区二区| 亚洲国产一区明星换脸| 青青青国产手机频在线免费观看 | 无码人妻一区二区三区免费手机| 亚洲乱色伦图片区小说| 亚洲色偷拍区另类无码专区| 久久国产精品免费看| 亚洲熟妇久久精品| 亚洲线精品一区二区三区| 99re在线精品视频免费| 337P日本欧洲亚洲大胆精品 | 亚洲一本之道高清乱码| 国产国产成年年人免费看片| a级毛片免费完整视频| 亚洲精品人成网在线播放影院| 亚洲狠狠爱综合影院婷婷|