1 /*
2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
533 * to the original internal byte array of this {@code SerialBlob} object.
534 * The underlying {@code Blob} object will be set to null.
535 *
536 * @return a clone of this SerialBlob
537 */
538 public Object clone() {
539 try {
540 SerialBlob sb = (SerialBlob) super.clone();
541 sb.buf = (buf != null) ? Arrays.copyOf(buf, (int)len) : null;
542 sb.blob = null;
543 return sb;
544 } catch (CloneNotSupportedException ex) {
545 // this shouldn't happen, since we are Cloneable
546 throw new InternalError();
547 }
548 }
549
550 /**
551 * readObject is called to restore the state of the SerialBlob from
552 * a stream.
553 */
554 private void readObject(ObjectInputStream s)
555 throws IOException, ClassNotFoundException {
556
557 ObjectInputStream.GetField fields = s.readFields();
558 byte[] tmp = (byte[])fields.get("buf", null);
559 if (tmp == null)
560 throw new InvalidObjectException("buf is null and should not be!");
561 buf = tmp.clone();
562 len = fields.get("len", 0L);
563 if (buf.length != len)
564 throw new InvalidObjectException("buf is not the expected size");
565 origLen = fields.get("origLen", 0L);
566 blob = (Blob) fields.get("blob", null);
567 }
568
569 /**
570 * writeObject is called to save the state of the SerialBlob
571 * to a stream.
572 */
573 private void writeObject(ObjectOutputStream s)
574 throws IOException {
575
576 ObjectOutputStream.PutField fields = s.putFields();
577 fields.put("buf", buf);
578 fields.put("len", len);
579 fields.put("origLen", origLen);
580 // Note: this check to see if it is an instance of Serializable
581 // is for backwards compatibility
582 fields.put("blob", blob instanceof Serializable ? blob : null);
583 s.writeFields();
584 }
585
586 /**
587 * Check to see if this object had previously had its {@code free} method
588 * called
589 *
590 * @throws SerialException
591 */
|
1 /*
2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
533 * to the original internal byte array of this {@code SerialBlob} object.
534 * The underlying {@code Blob} object will be set to null.
535 *
536 * @return a clone of this SerialBlob
537 */
538 public Object clone() {
539 try {
540 SerialBlob sb = (SerialBlob) super.clone();
541 sb.buf = (buf != null) ? Arrays.copyOf(buf, (int)len) : null;
542 sb.blob = null;
543 return sb;
544 } catch (CloneNotSupportedException ex) {
545 // this shouldn't happen, since we are Cloneable
546 throw new InternalError();
547 }
548 }
549
550 /**
551 * readObject is called to restore the state of the SerialBlob from
552 * a stream.
553 * @param s the {@code ObjectInputStream} to read from.
554 *
555 * @throws ClassNotFoundException if the class of a serialized object
556 * could not be found.
557 * @throws IOException if an I/O error occurs.
558 */
559 private void readObject(ObjectInputStream s)
560 throws IOException, ClassNotFoundException {
561
562 ObjectInputStream.GetField fields = s.readFields();
563 byte[] tmp = (byte[])fields.get("buf", null);
564 if (tmp == null)
565 throw new InvalidObjectException("buf is null and should not be!");
566 buf = tmp.clone();
567 len = fields.get("len", 0L);
568 if (buf.length != len)
569 throw new InvalidObjectException("buf is not the expected size");
570 origLen = fields.get("origLen", 0L);
571 blob = (Blob) fields.get("blob", null);
572 }
573
574 /**
575 * writeObject is called to save the state of the SerialBlob
576 * to a stream.
577 * @param s the {@code ObjectOutputStream} to write to.
578 + @throws IOException if I/O errors occur.
579 */
580 private void writeObject(ObjectOutputStream s)
581 throws IOException {
582
583 ObjectOutputStream.PutField fields = s.putFields();
584 fields.put("buf", buf);
585 fields.put("len", len);
586 fields.put("origLen", origLen);
587 // Note: this check to see if it is an instance of Serializable
588 // is for backwards compatibility
589 fields.put("blob", blob instanceof Serializable ? blob : null);
590 s.writeFields();
591 }
592
593 /**
594 * Check to see if this object had previously had its {@code free} method
595 * called
596 *
597 * @throws SerialException
598 */
|