1 /*
2 * Copyright (c) 2003, 2013, 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
458 * the number of bytes in the {@code Blob}
459 * @throws SerialException if the {@code free} method had been previously
460 * called on this object
461 *
462 * @since 1.6
463 */
464 public InputStream getBinaryStream(long pos, long length) throws SQLException {
465 isValid();
466 if (pos < 1 || pos > this.length()) {
467 throw new SerialException("Invalid position in BLOB object set");
468 }
469 if (length < 1 || length > len - pos + 1) {
470 throw new SerialException("length is < 1 or pos + length >"
471 + "total number of bytes");
472 }
473 return new ByteArrayInputStream(buf, (int) pos - 1, (int) length);
474 }
475
476
477 /**
478 * This method frees the {@code SeriableBlob} object and releases the
479 * resources that it holds. The object is invalid once the {@code free}
480 * method is called. <p> If {@code free} is called multiple times, the
481 * subsequent calls to {@code free} are treated as a no-op. </P>
482 *
483 * @throws SQLException if an error occurs releasing the Blob's resources
484 * @since 1.6
485 */
486 public void free() throws SQLException {
487 if (buf != null) {
488 buf = null;
489 if (blob != null) {
490 blob.free();
491 }
492 blob = null;
493 }
494 }
495
496 /**
497 * Compares this SerialBlob to the specified object. The result is {@code
498 * true} if and only if the argument is not {@code null} and is a {@code
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, ClassNotFoundException {
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 compatibiity
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 */
592 private void isValid() throws SerialException {
593 if (buf == null) {
594 throw new SerialException("Error: You cannot call a method on a "
595 + "SerialBlob instance once free() has been called.");
596 }
597 }
598
599 /**
600 * The identifier that assists in the serialization of this
601 * {@code SerialBlob} object.
|
1 /*
2 * Copyright (c) 2003, 2014, 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
458 * the number of bytes in the {@code Blob}
459 * @throws SerialException if the {@code free} method had been previously
460 * called on this object
461 *
462 * @since 1.6
463 */
464 public InputStream getBinaryStream(long pos, long length) throws SQLException {
465 isValid();
466 if (pos < 1 || pos > this.length()) {
467 throw new SerialException("Invalid position in BLOB object set");
468 }
469 if (length < 1 || length > len - pos + 1) {
470 throw new SerialException("length is < 1 or pos + length >"
471 + "total number of bytes");
472 }
473 return new ByteArrayInputStream(buf, (int) pos - 1, (int) length);
474 }
475
476
477 /**
478 * This method frees the {@code SerialBlob} object and releases the
479 * resources that it holds. The object is invalid once the {@code free}
480 * method is called. <p> If {@code free} is called multiple times, the
481 * subsequent calls to {@code free} are treated as a no-op. </P>
482 *
483 * @throws SQLException if an error occurs releasing the Blob's resources
484 * @since 1.6
485 */
486 public void free() throws SQLException {
487 if (buf != null) {
488 buf = null;
489 if (blob != null) {
490 blob.free();
491 }
492 blob = null;
493 }
494 }
495
496 /**
497 * Compares this SerialBlob to the specified object. The result is {@code
498 * true} if and only if the argument is not {@code null} and is a {@code
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, ClassNotFoundException {
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 compatibiiity
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 */
592 private void isValid() throws SerialException {
593 if (buf == null) {
594 throw new SerialException("Error: You cannot call a method on a "
595 + "SerialBlob instance once free() has been called.");
596 }
597 }
598
599 /**
600 * The identifier that assists in the serialization of this
601 * {@code SerialBlob} object.
|