1 2 24 package org.objectweb.joram.shared.messages; 25 26 import org.objectweb.joram.shared.excepts.*; 27 import org.objectweb.joram.shared.messages.MessageTracing; 28 import org.objectweb.util.monolog.api.BasicLevel; 29 30 import java.io.*; 31 import java.util.*; 32 33 37 public class MessageBody implements Cloneable , Serializable { 38 39 40 transient private int type; 41 42 43 transient private byte[] body_bytes = null; 44 45 transient private HashMap body_map = null; 46 47 transient private String body_text = null; 48 49 transient public boolean saved = false; 50 51 54 public MessageBody() {} 55 56 void setType(int type) { 57 this.type = type; 58 } 59 60 61 public byte[] getBodyBytes() { 62 return body_bytes; 63 } 64 65 66 public HashMap getBodyMap() { 67 return body_map; 68 } 69 70 71 public String getBodyText() { 72 return body_text; 73 } 74 75 76 public void setBodyBytes(byte[] bytes) { 77 body_bytes = bytes; 78 } 79 80 81 public void setBodyMap(HashMap map) { 82 body_map = map; 83 } 84 85 86 public void setBodyText(String text) { 87 body_text = text; 88 } 89 90 91 public Object clone() { 92 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 93 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 94 "MessageBody.clone()"); 95 try { 96 MessageBody clone = (MessageBody) super.clone(); 97 if (getBodyMap() != null) { 98 clone.setBodyMap(new HashMap()); 99 if (getBodyMap().keySet() != null) { 100 for (Iterator it = getBodyMap().keySet().iterator(); it.hasNext(); ) { 101 Object key = it.next(); 102 clone.getBodyMap().put(key,getBodyMap().get(key)); 103 } 104 } 105 } 106 return clone; 107 } catch (CloneNotSupportedException cE) { 108 return null; 109 } 110 } 111 112 private static void writeString(ObjectOutputStream os, 113 String s) throws IOException { 114 if (s == null) { 115 os.writeInt(-1); 116 } else if (s.length() == 0) { 117 os.writeInt(0); 118 } else { 119 byte[] bout = s.getBytes(); 120 os.writeInt(bout.length); 121 os.write(bout); 122 } 123 } 124 125 private static String readString(ObjectInputStream is) throws IOException { 126 int length = is.readInt(); 127 if (length == -1) { 128 return null; 129 } else if (length == 0) { 130 return ""; 131 } else { 132 byte[] bin = new byte[length]; 133 is.readFully(bin); 134 return new String (bin); 135 } 136 } 137 138 private void writeObject(ObjectOutputStream os) 139 throws IOException { 140 os.writeInt(type); 141 if (type == MessageType.TEXT) { 142 writeString(os, body_text); 143 } else if ((type == MessageType.OBJECT) || 144 (type == MessageType.STREAM) || 145 (type == MessageType.BYTES)) { 146 if (body_bytes == null) { 147 os.writeInt(-1); 148 } else { 149 os.writeInt(body_bytes.length); 150 os.write(body_bytes); 151 } 152 } else if (type == MessageType.MAP) { 153 os.writeObject(body_map); 154 } 155 } 156 157 private void readObject(ObjectInputStream is) 158 throws IOException, ClassNotFoundException { 159 type = is.readInt(); 160 if (type == MessageType.TEXT) { 161 body_text = readString(is); 162 } else if ((type == MessageType.OBJECT) || 163 (type == MessageType.STREAM) || 164 (type == MessageType.BYTES)) { 165 int length = is.readInt(); 166 if (length == -1) { 167 body_bytes = null; 168 } else { 169 body_bytes = new byte[length]; 170 is.readFully(body_bytes); 171 } 172 } else if (type == MessageType.MAP) { 173 body_map = (HashMap) is.readObject(); 174 } 175 } 176 177 public String toString() { 178 StringBuffer buff = new StringBuffer (); 179 buff.append('('); 180 buff.append(super.toString()); 181 buff.append(",type="); 182 buff.append(type); 183 buff.append(",body_map="); 184 buff.append(body_map); 185 buff.append(",body_text="); 186 buff.append(body_text); 187 buff.append(",body_bytes="); 188 buff.append(body_bytes); 189 buff.append(",saved="); 190 buff.append(saved); 191 buff.append(')'); 192 return buff.toString(); 193 } 194 } 195 | Popular Tags |