2 * Copyright 2010 Sun Microsystems, Inc.
3 * All rights reserved. Use is subject to license terms.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.mysql.clusterj.tie;
21 import java.nio.ByteBuffer;
23 import com.mysql.ndbjtie.ndbapi.NdbBlob;
25 import com.mysql.clusterj.ClusterJFatalInternalException;
27 import com.mysql.clusterj.core.store.Blob;
29 import com.mysql.clusterj.core.util.I18NHelper;
30 import com.mysql.clusterj.core.util.Logger;
31 import com.mysql.clusterj.core.util.LoggerFactoryService;
36 class BlobImpl implements Blob {
38 /** My message translator */
39 static final I18NHelper local = I18NHelper
40 .getInstance(BlobImpl.class);
43 static final Logger logger = LoggerFactoryService.getFactory()
44 .getInstance(BlobImpl.class);
46 private NdbBlob ndbBlob;
48 public BlobImpl(NdbBlob blob) {
52 public Long getLength() {
53 long[] length = new long[1];
54 int returnCode = ndbBlob.getLength(length);
55 handleError(returnCode, ndbBlob);
59 public void readData(byte[] array, int length) {
60 // int[1] is an artifact of ndbjtie to pass an in/out parameter
61 int[] lengthRead = new int[] {length};
62 ByteBuffer buffer = ByteBuffer.allocateDirect(array.length);
63 int returnCode = ndbBlob.readData(buffer, lengthRead);
64 handleError(returnCode, ndbBlob);
65 if (lengthRead[0] != length) {
66 throw new ClusterJFatalInternalException(
67 local.message("ERR_Blob_Read_Data", length, lengthRead[0]));
69 // now copy into user space
73 public void writeData(byte[] array) {
74 // TODO can we really skip this when updating to an empty blob?
75 if (array.length == 0) return;
76 ByteBuffer buffer = null;
77 if (array.length > 0) {
78 buffer = ByteBuffer.allocateDirect(array.length);
82 int returnCode = ndbBlob.writeData(buffer, array.length);
83 handleError(returnCode, ndbBlob);
86 public void setValue(byte[] array) {
87 // TODO can we really skip this when updating to an empty blob?
88 if (array.length == 0) return;
89 ByteBuffer buffer = null;
90 if (array.length > 0) {
91 buffer = ByteBuffer.allocateDirect(array.length);
95 int returnCode = ndbBlob.setValue(buffer, array.length);
96 handleError(returnCode, ndbBlob);
99 public void setNull() {
100 int returnCode = ndbBlob.setNull();
101 handleError(returnCode, ndbBlob);
104 public void close() {
105 int returnCode = ndbBlob.close(true);
106 handleError(returnCode, ndbBlob);
109 protected static void handleError(int returnCode, NdbBlob ndbBlob) {
110 if (returnCode == 0) {
113 Utility.throwError(returnCode, ndbBlob.getNdbError());