MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlobImpl.java
1 /*
2  * Copyright 2010 Sun Microsystems, Inc.
3  * All rights reserved. Use is subject to license terms.
4  *
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.
8  *
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.
13  *
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
17  */
18 
19 package com.mysql.clusterj.tie;
20 
21 import java.nio.ByteBuffer;
22 
23 import com.mysql.ndbjtie.ndbapi.NdbBlob;
24 
25 import com.mysql.clusterj.ClusterJFatalInternalException;
26 
27 import com.mysql.clusterj.core.store.Blob;
28 
29 import com.mysql.clusterj.core.util.I18NHelper;
30 import com.mysql.clusterj.core.util.Logger;
31 import com.mysql.clusterj.core.util.LoggerFactoryService;
32 
36 class BlobImpl implements Blob {
37 
39  static final I18NHelper local = I18NHelper
40  .getInstance(BlobImpl.class);
41 
43  static final Logger logger = LoggerFactoryService.getFactory()
44  .getInstance(BlobImpl.class);
45 
46  private NdbBlob ndbBlob;
47 
48  public BlobImpl(NdbBlob blob) {
49  this.ndbBlob = blob;
50  }
51 
52  public Long getLength() {
53  long[] length = new long[1];
54  int returnCode = ndbBlob.getLength(length);
55  handleError(returnCode, ndbBlob);
56  return length[0];
57  }
58 
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]));
68  }
69  // now copy into user space
70  buffer.get(array);
71  }
72 
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);
79  buffer.put(array);
80  buffer.flip();
81  }
82  int returnCode = ndbBlob.writeData(buffer, array.length);
83  handleError(returnCode, ndbBlob);
84  }
85 
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);
92  buffer.put(array);
93  buffer.flip();
94  }
95  int returnCode = ndbBlob.setValue(buffer, array.length);
96  handleError(returnCode, ndbBlob);
97  }
98 
99  public void setNull() {
100  int returnCode = ndbBlob.setNull();
101  handleError(returnCode, ndbBlob);
102  }
103 
104  public void close() {
105  int returnCode = ndbBlob.close(true);
106  handleError(returnCode, ndbBlob);
107  }
108 
109  protected static void handleError(int returnCode, NdbBlob ndbBlob) {
110  if (returnCode == 0) {
111  return;
112  } else {
113  Utility.throwError(returnCode, ndbBlob.getNdbError());
114  }
115  }
116 
117 }