コード例 #1
0
ファイル: I2CImp.c プロジェクト: aferre/rxtx-2.2pre2
/*----------------------------------------------------------
I2CPort.readArray

   accept:       offset (offset to start storing data in the jbarray) and 
                 Length (bytes to read)
   perform:      read bytes from the port into a byte array
   return:       bytes read on success
                 0 on read timeout
   exceptions:   IOException
   comments:     throws ArrayIndexOutOfBoundsException if asked to
                 read more than SSIZE_MAX bytes
----------------------------------------------------------*/ 
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_readArray( JNIEnv *env,
	jobject jobj, jbyteArray jbarray, jint offset, jint length )
{  
	int bytes;
	jbyte *body;
	unsigned char *buffer;
	int fd = get_java_var( env, jobj, "fd", "I" );
	int timeout = get_java_var( env, jobj, "timeout", "I" );

	if( (size_t) length > SSIZE_MAX || length < 0 ) {
		throw_java_exception( env, ARRAY_INDEX_OUT_OF_BOUNDS,
			"readArray", "Invalid length" );
		return -1;
	}

	buffer = (unsigned char *)malloc( sizeof( unsigned char ) * length );
	if( buffer == 0 ) {
		throw_java_exception( env, OUT_OF_MEMORY, "readArray",
			"Unable to allocate buffer" );
		return -1;
	}

	bytes = read_byte_array( fd, buffer, length, timeout );
	if( bytes < 0 ) {
		free( buffer );
		throw_java_exception( env, IO_EXCEPTION, "readArray",
			strerror( errno ) );
		return -1;
	}
	body = (*env)->GetByteArrayElements( env, jbarray, 0 );
	memcpy(body + offset, buffer, bytes);
	(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 );
	free( buffer );
	return (bytes ? bytes : -1);
}
コード例 #2
0
ファイル: RS485Imp.c プロジェクト: openmuc/jrxtx
/*----------------------------------------------------------
 RS485Port.readByte

 accept:      none
 perform:     Read a single byte from the port
 return:      The byte read
 exceptions:  IOException
 ----------------------------------------------------------*/
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_readByte(JNIEnv *env, jobject jobj) {
	int bytes;
	unsigned char buffer[1];
	int fd = get_java_var(env, jobj, "fd", "I");
	int timeout = get_java_var(env, jobj, "timeout", "I");

	bytes = read_byte_array(fd, buffer, 1, timeout);
	if (bytes < 0) {
		throw_java_exception(env, IO_EXCEPTION, "readByte", strerror( errno));
		return -1;
	}
	return (bytes ? (jint) buffer[0] : -1);
}
コード例 #3
0
ファイル: ParallelImp.c プロジェクト: dunk8888/RXTX
/*----------------------------------------------------------
LPRPort.readByte

   accept:      none
   perform:     Read a single byte from the port
   return:      The byte read
   exceptions:  IOException
----------------------------------------------------------*/
JNIEXPORT jint JNICALL LPRPort(readByte)( JNIEnv *env,
	jobject jobj )
{
	int bytes, fd, timeout;
	unsigned char buffer[ 1 ];

	fd = get_java_var( env, jobj,"fd","I" );
	timeout = get_java_var( env, jobj, "timeout","I");

	bytes = read_byte_array( fd, buffer, 1, 1, timeout );
	if( bytes < 0 )
	{
		throw_java_exception_system_msg( env, IO_EXCEPTION, "readByte" );

		return -1;
	}
	return (bytes ? (jint)buffer[ 0 ] : -1);
}
コード例 #4
0
ファイル: ParallelImp.c プロジェクト: dunk8888/RXTX
/*----------------------------------------------------------
LPRPort.readArray

   accept:       offset (bytes to skip) and Length (bytes to read)
   perform:      read bytes from the port into a byte array
   return:       bytes read on success
                 0 on read timeout
   exceptions:   IOException
   comments:     throws IOException if asked to read > SSIZE_MAX
----------------------------------------------------------*/
JNIEXPORT jint JNICALL LPRPort(readArray)( JNIEnv *env,
	jobject jobj, jbyteArray jbarray, jint offset, jint length )
{
	int bytes, i, fd, threshold, timeout;
	jbyte *body;
	unsigned char *buffer;
	fd = get_java_var( env, jobj,"fd","I" );
	threshold = get_java_var( env, jobj,"threshold","I" );
	timeout = get_java_var( env, jobj,"threshold","I" );

	if( (size_t) length < 1 || (size_t) length > SSIZE_MAX )
	{
		throw_java_exception( env, IO_EXCEPTION, "readArray",
			"Invalid length" );
		return -1;
	}

	buffer = (unsigned char *)malloc( sizeof( unsigned char ) * length );
	if( buffer == 0 )
	{
		throw_java_exception( env, IO_EXCEPTION, "readArray",
			"Unable to allocate buffer" );

		return -1;
	}

	bytes = read_byte_array( fd, buffer, length, threshold, timeout );
	if( bytes < 0 )
	{
		free( buffer );
		throw_java_exception_system_msg( env, IO_EXCEPTION, "readArray" );

		return -1;
	}

	body = (*env)->GetByteArrayElements( env, jbarray, 0 );
	for( i = 0; i < bytes; i++ ) body[ i + offset ] = buffer[ i ];
	(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 );
	free( buffer );
	return (bytes ? bytes : -1);
}