File: GdaInputStream.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (174 lines) | stat: -rw-r--r-- 4,674 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2008 - 2013 Vivien Malerba <malerba@gnome-db.org>
 * Copyright (C) 2010 David King <davidk@openismus.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "GdaInputStream.h"
#include "jni-wrapper.h"
#include <libgda/libgda.h>
#include <libgda/gda-blob-op.h>
#include <glib/gi18n-lib.h>

jclass GdaInputStream__class = NULL;

JNIEXPORT void
JNICALL Java_GdaInputStream_initIDs (JNIEnv *env, jclass klass)
{
	GdaInputStream__class = (*env)->NewGlobalRef (env, klass);
}

JNIEXPORT jintArray
JNICALL Java_GdaInputStream_readData (JNIEnv *jenv, G_GNUC_UNUSED jobject obj,
				      jlong gda_blob_pointer, jlong offset, jlong size)
{
	GdaBlob *blob = (GdaBlob*) jni_jlong_to_cpointer (gda_blob_pointer);
	jintArray jdata;
	if (!blob) {
		jclass cls;
		cls = (*jenv)->FindClass (jenv, "java/lang/IllegalArgumentException");
		if (!cls) {
			/* Unable to find the exception class */
			return NULL;
		}
		(*jenv)->ThrowNew (jenv, cls, _("Invalid argument: NULL"));
		return NULL;
	}

	guchar *raw_data;
	jint *data;
	GdaBlob *nblob = NULL;
	gint real_size;
	if (blob->op) {
		nblob = g_new0 (GdaBlob, 1);
		gda_blob_set_op (nblob, blob->op);
		real_size =  gda_blob_op_read (nblob->op, nblob, offset, size);
		if (real_size < 0) {
			/* throw an exception */
			jclass cls;
			cls = (*jenv)->FindClass (jenv, "java/sql/SQLException");
			if (!cls) {
				/* Unable to find the exception class */
				return NULL;
			}
			(*jenv)->ThrowNew (jenv, cls, _("Can't read BLOB"));
			return NULL;
		}
		raw_data = ((GdaBinary*) nblob)->data;
	}
	else {
		GdaBinary *bin = (GdaBinary *) blob;
		if (offset + size > bin->binary_length)
			real_size = bin->binary_length - offset;
		else
			real_size = size;
		raw_data = bin->data + offset;
	}

	/* convert bin->data to a jintArray */
	int i;
	data = g_new (jint, real_size);
	for (i = 0; i < real_size; i++) 
		data[i] = (jint) (raw_data[i]);

	jdata = (*jenv)->NewIntArray (jenv, real_size);
	if ((*jenv)->ExceptionCheck (jenv)) {
		jdata = NULL;
		goto out;
	}
	
	(*jenv)->SetIntArrayRegion (jenv, jdata, 0, real_size, data);
	if ((*jenv)->ExceptionCheck (jenv)) {
		jdata = NULL;
		(*jenv)->DeleteLocalRef (jenv, jdata);
		goto out;
	}

 out:
	g_free (data);
	if (nblob)
		gda_blob_free ((gpointer) nblob);

	return jdata;
}

JNIEXPORT jbyteArray JNICALL
Java_GdaInputStream_readByteData (JNIEnv *jenv, G_GNUC_UNUSED jobject obj,
				      jlong gda_blob_pointer, jlong offset, jlong size)
{
	GdaBlob *blob = (GdaBlob*) jni_jlong_to_cpointer (gda_blob_pointer);
	jbyteArray jdata;
	if (!blob) {
		jclass cls;
		cls = (*jenv)->FindClass (jenv, "java/lang/IllegalArgumentException");
		if (!cls) {
			/* Unable to find the exception class */
			return NULL;
		}
		(*jenv)->ThrowNew (jenv, cls, _("Invalid argument: NULL"));
		return NULL;
	}

	guchar *raw_data;
	GdaBlob *nblob = NULL;
	gint real_size;
	if (blob->op) {
		nblob = g_new0 (GdaBlob, 1);
		gda_blob_set_op (nblob, blob->op);
		real_size =  gda_blob_op_read (nblob->op, nblob, offset, size);
		if (real_size < 0) {
			/* throw an exception */
			jclass cls;
			cls = (*jenv)->FindClass (jenv, "java/sql/SQLException");
			if (!cls) {
				/* Unable to find the exception class */
				return NULL;
			}
			(*jenv)->ThrowNew (jenv, cls, _("Can't read BLOB"));
			return NULL;
		}
		raw_data = ((GdaBinary*) nblob)->data;
	}
	else {
		GdaBinary *bin = (GdaBinary *) blob;
		if (offset + size > bin->binary_length)
			real_size = bin->binary_length - offset;
		else
			real_size = size;
		raw_data = bin->data + offset;
	}

	/* convert bin->data to a jintArray */
	jdata = (*jenv)->NewByteArray (jenv, real_size);
	if ((*jenv)->ExceptionCheck (jenv)) {
		jdata = NULL;
		goto out;
	}
	
	(*jenv)->SetByteArrayRegion (jenv, jdata, 0, real_size, (const jbyte *) raw_data);
	if ((*jenv)->ExceptionCheck (jenv)) {
		jdata = NULL;
		(*jenv)->DeleteLocalRef (jenv, jdata);
		goto out;
	}

 out:
	if (nblob)
		gda_blob_free ((gpointer) nblob);

	return jdata;
}