1 19 20 package org.openide.util.io; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.Serializable ; 28 import java.util.Arrays ; 29 30 38 public final class NbMarshalledObject implements Serializable { 39 40 private static final long serialVersionUID = 7842398740921434354L; 41 private final static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 43 48 private byte[] objBytes = null; 49 50 55 private int hash; 56 57 64 public NbMarshalledObject(Object obj) throws IOException { 65 if (obj == null) { 66 hash = 17; 67 68 return; 69 } 70 71 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 72 ObjectOutputStream out = new NbObjectOutputStream(bout); 73 out.writeObject(obj); 74 out.flush(); 75 objBytes = bout.toByteArray(); 76 77 int h = 0; 78 79 for (int i = 0; i < objBytes.length; i++) { 80 h = (37 * h) + objBytes[i]; 81 } 82 83 hash = h; 84 } 85 86 95 public Object get() throws IOException , ClassNotFoundException { 96 if (objBytes == null) { 98 return null; 99 } 100 101 ByteArrayInputStream bin = new ByteArrayInputStream (objBytes); 102 ObjectInputStream ois = new NbObjectInputStream(bin); 103 104 try { 105 return ois.readObject(); 106 } catch (RuntimeException weird) { 107 StringBuffer buf = new StringBuffer ((objBytes.length * 2) + 20); 109 buf.append("Bad ser data: "); 111 for (int i = 0; i < objBytes.length; i++) { 112 int b = objBytes[i]; 113 114 if (b < 0) { 115 b += 256; 116 } 117 118 buf.append(HEX[b / 16]); 119 buf.append(HEX[b % 16]); 120 } 121 122 IOException ioe = new IOException (weird.toString() + ": " + buf); ioe.initCause(weird); 124 throw ioe; 125 } finally { 126 ois.close(); 127 } 128 } 129 130 133 public int hashCode() { 134 return hash; 135 } 136 137 143 public boolean equals(Object obj) { 144 if (obj == this) { 145 return true; 146 } 147 148 if ((obj != null) && obj instanceof NbMarshalledObject) { 149 NbMarshalledObject other = (NbMarshalledObject) obj; 150 151 return Arrays.equals(objBytes, other.objBytes); 152 } else { 153 return false; 154 } 155 } 156 } 157 | Popular Tags |