1 46 package org.mr.core.net.messages; 47 48 57 58 import java.io.UnsupportedEncodingException ; 59 import java.nio.ByteBuffer ; 60 61 public class NetworkMessageID extends NetworkMessage { 62 private boolean initId; 63 private String name; 64 65 protected NetworkMessageID(ByteBuffer buf) { 66 super(buf); 67 byte[] array = new byte[getLength() - 1]; 68 byte b = buf.get(); 69 buf.get(array); 70 this.initId = (b == 1); 71 try { 72 this.name = new String (array, "UTF-8"); 73 } catch (UnsupportedEncodingException e) {} 74 } 76 public NetworkMessageID(boolean initId, String name) 77 throws UnsupportedEncodingException 78 { 79 super(NetworkMessage.NET_ID, 80 (short) (name.getBytes("UTF-8").length + 1)); 81 this.initId = initId; 82 this.name = name; 83 } 84 85 89 public String getName() { 90 return name; 91 } 92 93 public boolean getInitId() { 94 return this.initId; 95 } 96 97 public void write(ByteBuffer buf) { 98 super.write(buf); 99 try { 100 buf.put((byte) (initId ? 1 : 0)); 101 buf.put(this.name.getBytes("UTF-8")); 102 } catch (UnsupportedEncodingException e) {} 103 } 104 } | Popular Tags |