src/share/classes/javax/sql/rowset/serial/SerialBlob.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -26,10 +26,12 @@
 package javax.sql.rowset.serial;
 
 import java.sql.*;
 import java.io.*;
 import java.lang.reflect.*;
+import java.util.Arrays;
+import java.util.Objects;
 
 
 /**
  * A serialized mapping in the Java programming language of an SQL
  * <code>BLOB</code> value.

@@ -446,10 +448,103 @@
      * @since 1.6
      */
     public void free() throws SQLException {
         throw new java.lang.UnsupportedOperationException("Not supported");
     }
+
+   /**
+     * Compares this SerialBlob to the specified object.  The result is {@code
+     * true} if and only if the argument is not {@code null} and is a {@code
+     * SerialBlob} object that represents the same sequence of bytes as this
+     * object.
+     *
+     * @param  obj
+     *         The object to compare this {@code SerialBlob} against
+     *  
+     * @return  {@code true} if the given object represents a {@code SerialBlob}
+     *          equivalent to this SerialBlob, {@code false} otherwise
+     *  
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if(obj == null || !(obj instanceof SerialBlob)) {
+            return false;
+        }
+
+        SerialBlob sb = (SerialBlob)obj;
+        if(this.len == sb.len) {
+            return Arrays.equals(buf, sb.buf);
+        }
+        return false;
+    }
+
+    /**
+     * Returns a hash code for this {@code SerialBlob}.
+     * @return  a hash code value for this object.
+     */
+    public int hashCode() {
+       return Objects.hash(buf, len, origLen);
+    }
+
+    /**
+     * Returns a clone of this {@code SerialBlob}. The copy will contain a
+     * reference to a clone of the internal byte array, not a reference
+     * to the original internal byte array of this {@code SerialBlob} object.
+     * The internal {@code Blob} field will be set to null.
+     *  
+     * @return  a clone of this SerialBlob
+     */ 
+    public Object clone() {
+        SerialBlob sb = null;
+        try {
+            sb = (SerialBlob) super.clone();
+            sb.buf = Arrays.copyOf(buf, (int)len);
+            sb.blob = null;
+
+        } catch (CloneNotSupportedException ex) {
+            // this shouldn't happen, since we are Cloneable
+        }
+       return sb;
+    }
+
+    /**
+     * readObject is called to restore the state of the SerialBlob from
+     * a stream.
+     */
+    private void readObject(ObjectInputStream s)
+            throws IOException, ClassNotFoundException {
+        
+        ObjectInputStream.GetField fields = s.readFields();
+       byte[] tmp = (byte[])fields.get("buf", null);
+       if (tmp == null)
+           throw new InvalidObjectException("buf is null and should not be!");
+       buf = tmp.clone();
+       len = fields.get("len", 0L);
+       origLen = fields.get("origLen", 0L);
+       blob = (Blob) fields.get("blob", null);
+       if(buf.length != len)
+           throw new InvalidObjectException("buf is not the expected size");
+    }
+    
+    /**
+     * writeObject is called to save the state of the SerialBlob
+     * to a stream.
+     */
+    private void writeObject(ObjectOutputStream s)
+            throws IOException, ClassNotFoundException {
+
+        ObjectOutputStream.PutField fields = s.putFields();
+        fields.put("buf", buf);
+        fields.put("len", len);
+        fields.put("origLen", origLen);
+        fields.put("blob", null);
+        fields.put("blob", blob instanceof Serializable ? blob : null);
+        s.writeFields();    
+    }
+
     /**
          * The identifier that assists in the serialization of this <code>SerialBlob</code>
      * object.
      */