/*
 * Source.c
 *
 * Copyright (c) 2007  Vreixo Formoso
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See COPYING file for details.
 */

#include "org_pykix_libburnia_libburn_Source.h"

#include "libburn.h"
#include "libisofs.h"

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    burn_file_source_new
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
 */
JNIEXPORT jlong JNICALL Java_org_pykix_libburnia_libburn_Source_burn_1file_1source_1new
(
	JNIEnv *env, jclass cls, jstring path, jstring subPath
)
{
	const char *cpath = NULL;
	const char *subpath = NULL;
	struct burn_source *src;

	cpath = (*env)->GetStringUTFChars(env, path, NULL);
	if ( cpath == NULL ) {
		return (jlong) 0; /* OutOfMemoryError already thrown */
	}
	if ( subPath != NULL ) {
		subpath = (*env)->GetStringUTFChars(env, subPath, NULL);
		if (subpath == NULL) {
			return (jlong) 0; /* OutOfMemoryError already thrown */
		}
	}
	
	src = burn_file_source_new(cpath, subpath);

	(*env)->ReleaseStringUTFChars(env, path, cpath);
	(*env)->ReleaseStringUTFChars(env, subPath, subpath);
	
	return (jlong) src;
}

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    burn_fd_source_new
 * Signature: (IIJ)J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libburn_Source_burn_1fd_1source_1new
(
	JNIEnv *env, jclass cls, jint datafd, jint subfd, jlong size
)
{
	return (jlong) burn_fd_source_new(datafd, subfd, size);
}

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    iso_source_new_ecma119
 * Signature: (JIII)J
 */
JNIEXPORT jlong JNICALL Java_org_pykix_libburnia_libburn_Source_iso_1source_1new_1ecma119
(
	JNIEnv *env, jclass cls, jlong volumeset, jint volnum, jint level, jint flags)
{

	return (jlong) iso_source_new_ecma119 ( (struct iso_volset *) volumeset, volnum, level, flags);
}

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    read
 * Signature: (J[BI)I
 */
JNIEXPORT jint JNICALL Java_org_pykix_libburnia_libburn_Source_read
(
	JNIEnv *env, jclass cls, jlong burnSource, jbyteArray byteBuffer, jint size
)
{
	struct burn_source *source;
	jbyte *buffer = NULL;

	source = (struct burn_source *) burnSource;

	buffer = (*env)->GetByteArrayElements(env, byteBuffer, NULL);
	if (buffer == NULL) {
		return -1; /* exception occurred */
	}
	
	int n = source->read(source, buffer, size);
	
	(*env)->ReleaseByteArrayElements(env, byteBuffer, buffer, 0);
	
	return n;
}

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    read_sub
 * Signature: (J[BI)I
 */
JNIEXPORT jint JNICALL Java_org_pykix_libburnia_libburn_Source_read_1sub
(
	JNIEnv *env, jclass cls, jlong burnSource, jbyteArray byteBuffer, jint size
)
{
	struct burn_source *source;
	jbyte *buffer = NULL;

	source = (struct burn_source *) burnSource;

	buffer = (*env)->GetByteArrayElements(env, byteBuffer, NULL);
	if (buffer == NULL) {
		return -1; /* exception occurred */
	}
	
	int n = source->read_sub(source, buffer, size);
	
	(*env)->ReleaseByteArrayElements(env, byteBuffer, buffer, 0);
	
	return n;
}

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    get_size
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_org_pykix_libburnia_libburn_Source_get_1size
(
	JNIEnv *env, jclass cls, jlong burnSource
)
{
	struct burn_source *source;

	source = (struct burn_source *) burnSource;

	off_t size = source->get_size(source);

	return size;
}

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    set_size
 * Signature: (JJ)I
 */
JNIEXPORT jint JNICALL Java_org_pykix_libburnia_libburn_Source_set_1size
(
	JNIEnv *env, jclass cls, jlong burnSource, jlong size
)
{
	struct burn_source *source;

	source = (struct burn_source *) burnSource;

	int n = source->set_size(source, size);

	return n;
}

/*
 * Class:     org_pykix_libburnia_libburn_Source
 * Method:    burn_source_free
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_org_pykix_libburnia_libburn_Source_burn_1source_1free
(
	JNIEnv *env, jclass cls, jlong s
)
{
	burn_source_free( (struct burn_source *) s);
}

