/* * Blob write request */ static glong gda_web_blob_op_write (GdaBlobOp *op, GdaBlob *blob, G_GNUC_UNUSED glong offset) { GdaWebBlobOp *bop; GdaBinary *bin; glong nbwritten = -1; g_return_val_if_fail (GDA_IS_WEB_BLOB_OP (op), -1); bop = GDA_WEB_BLOB_OP (op); g_return_val_if_fail (bop->priv, -1); g_return_val_if_fail (GDA_IS_CONNECTION (bop->priv->cnc), -1); g_return_val_if_fail (blob, -1); if (blob->op && (blob->op != op)) { /* use data through blob->op */ #define buf_size 16384 gint nread = 0; GdaBlob *tmpblob = g_new0 (GdaBlob, 1); gda_blob_set_op (tmpblob, blob->op); nbwritten = 0; for (nread = gda_blob_op_read (tmpblob->op, tmpblob, nbwritten, buf_size); nread > 0; nread = gda_blob_op_read (tmpblob->op, tmpblob, nbwritten, buf_size)) { glong tmp_written; tmp_written = -1; TO_IMPLEMENT; if (tmp_written < 0) { /* treat error */ gda_blob_free ((gpointer) tmpblob); return -1; } nbwritten += tmp_written; if (nread < buf_size) /* nothing more to read */ break; } gda_blob_free ((gpointer) tmpblob); } else { /* write blob using bin->data and bin->binary_length */ bin = (GdaBinary *) blob; g_warning("bin not used. length=%ld", bin->binary_length); /* Avoids a compiler warning. */ nbwritten = -1; TO_IMPLEMENT; } return nbwritten; }
/* * Blob write request */ static glong gda_oracle_blob_op_write (GdaBlobOp *op, GdaBlob *blob, glong offset) { GdaOracleBlobOp *bop; glong nbwritten = -1; g_return_val_if_fail (GDA_IS_ORACLE_BLOB_OP (op), -1); bop = GDA_ORACLE_BLOB_OP (op); g_return_val_if_fail (bop->priv, -1); g_return_val_if_fail (GDA_IS_CONNECTION (bop->priv->cnc), -1); g_return_val_if_fail (blob, -1); if (blob->op && (blob->op != op)) { /* use data through blob->op */ #define buf_size 16384 gint nread = 0; GdaBlob *tmpblob = g_new0 (GdaBlob, 1); gda_blob_set_op (tmpblob, blob->op); nbwritten = 0; for (nread = gda_blob_op_read (tmpblob->op, tmpblob, nbwritten, buf_size); nread > 0; nread = gda_blob_op_read (tmpblob->op, tmpblob, nbwritten, buf_size)) { glong tmp_written; tmp_written = -1; TO_IMPLEMENT; if (tmp_written < 0) { /* treat error */ gda_blob_free ((gpointer) tmpblob); return -1; } nbwritten += tmp_written; if (nread < buf_size) /* nothing more to read */ break; } gda_blob_free ((gpointer) tmpblob); } else { /* write blob using bin->data and bin->binary_length */ GdaBinary *bin; bin = (GdaBinary *) blob; nbwritten = -1; TO_IMPLEMENT; } return nbwritten; }
static glong gda_dir_blob_op_write (GdaBlobOp *op, GdaBlob *blob, glong offset) { GdaDirBlobOp *dirop; GdaBinary *bin; FILE *file; glong nbwritten; g_return_val_if_fail (GDA_IS_DIR_BLOB_OP (op), -1); dirop = GDA_DIR_BLOB_OP (op); GdaDirBlobOpPrivate *priv = gda_dir_blob_op_get_instance_private (dirop); if (offset >= G_MAXINT) return -1; g_return_val_if_fail (blob, -1); /* open file */ file = fopen (priv->complete_filename, "w+b"); /* Flawfinder: ignore */ if (!file) return -1; /* go to offset */ if (offset > 0) { if (fseek (file, offset, SEEK_SET) != 0) { fclose (file); return -1; } } if (gda_blob_get_op (blob) && (gda_blob_get_op (blob) != op)) { /* use data through blob->op */ #define buf_size 16384 gint nread = 0; GdaBlob *tmpblob = gda_blob_new (); gda_blob_set_op (tmpblob, gda_blob_get_op (blob)); nbwritten = 0; for (nread = gda_blob_op_read (gda_blob_get_op (tmpblob), tmpblob, 0, buf_size); nread > 0; nread = gda_blob_op_read (gda_blob_get_op (tmpblob), tmpblob, nbwritten, buf_size)) { GdaBinary *bin = gda_blob_get_binary (tmpblob); glong tmp_written; tmp_written = fwrite ((char *) gda_binary_get_data (bin), sizeof (guchar), gda_binary_get_size (bin), file); if (tmp_written < gda_binary_get_size (bin)) { /* error writing stream */ fclose (file); gda_blob_free (tmpblob); return -1; } nbwritten += tmp_written; if (nread < buf_size) /* nothing more to read */ break; } fclose (file); gda_blob_free (tmpblob); } else { bin = (GdaBinary *) blob; nbwritten = fwrite ((char *) (gda_binary_get_data (bin)), 1, gda_binary_get_size (bin), file); fclose (file); } return (nbwritten >= 0) ? nbwritten : -1; }
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; }
/* * Blob write request */ static glong gda_sqlite_blob_op_write (GdaBlobOp *op, GdaBlob *blob, glong offset) { GdaSqliteBlobOp *bop; GdaBinary *bin; glong nbwritten = -1; int len; g_return_val_if_fail (GDA_IS_SQLITE_BLOB_OP (op), -1); bop = GDA_SQLITE_BLOB_OP (op); GdaSqliteBlobOpPrivate *priv = gda_sqlite_blob_op_get_instance_private (bop); g_return_val_if_fail (priv->sblob, -1); g_return_val_if_fail (blob, -1); GdaSqliteProvider *prov = g_weak_ref_get (&priv->provider); g_return_val_if_fail (prov != NULL, -1); len = SQLITE3_CALL (prov, sqlite3_blob_bytes) (priv->sblob); if (len < 0) { g_object_unref (prov); return -1; } if (gda_blob_get_op (blob) && (gda_blob_get_op (blob) != op)) { /* use data through blob->op */ #define buf_size 16384 gint nread = 0; GdaBlob *tmpblob = gda_blob_new (); gda_blob_set_op (tmpblob, gda_blob_get_op (blob)); nbwritten = 0; for (nread = gda_blob_op_read (gda_blob_get_op (tmpblob), tmpblob, nbwritten, buf_size); nread > 0; nread = gda_blob_op_read (gda_blob_get_op (tmpblob), tmpblob, nbwritten, buf_size)) { int tmp_written; int rc; int wlen; if (nread + offset + nbwritten > len) wlen = len - offset - nbwritten; else wlen = nread; rc = SQLITE3_CALL (prov, sqlite3_blob_write) (priv->sblob, gda_binary_get_data (gda_blob_get_binary (tmpblob)), wlen, offset + nbwritten); if (rc != SQLITE_OK) tmp_written = -1; else tmp_written = wlen; if (tmp_written < 0) { /* treat error */ gda_blob_free ((gpointer) tmpblob); g_object_unref (prov); return -1; } nbwritten += tmp_written; if (nread < buf_size) /* nothing more to read */ break; } gda_blob_free ((gpointer) tmpblob); } else { /* write blob using bin->data and bin->binary_length */ int rc; int wlen; bin = gda_blob_get_binary (blob); if (gda_binary_get_size (bin) + offset > len) wlen = len - offset; else wlen = gda_binary_get_size (bin); rc = SQLITE3_CALL (prov, sqlite3_blob_write) (priv->sblob, gda_binary_get_data (bin), wlen, offset); if (rc != SQLITE_OK) nbwritten = -1; else nbwritten = wlen; } g_object_unref (prov); return nbwritten; }