Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > JNI/c++ - passing imagefile as a byte array to native library

Reply
Thread Tools

JNI/c++ - passing imagefile as a byte array to native library

 
 
Guest
Posts: n/a
 
      06-06-2007
Hi all,

I am writing a sendmail milter application in Java. The incoming mails will
usually have image file as attachments. My application is currently able to
extract the ImageFile and save it on the filesystem. This part is working
perfectly.

((MimeBodyPart)p).saveFile(new File(p.getFileName()));

However, I would like to pass this file as a byte array to a c++ library
instead and do the file saving and additional processing there instead. I
know very little c++ though. Can someone help please? I am also not sure if
the code I have written is doing what I think it is doing i.e. converting
the file into a byte array and passing it to the native library.

My current Java code is as following :

private Boolean passToDLL(Part p, Boolean result) throws IOException {
InputStream is = null;
try {
is = p.getInputStream();

DataInputStream dis = new DataInputStream(is);
String data = "";
int size = dis.available();
int offset = 0;
do{
byte [] byteArray = new byte[size];
dis.read(byteArray, offset, size);
data += new String(byteArray);
offset = size + 1;
size = dis.available();
}while(size > 0);
byte [] fileData = data.getBytes();
result = passAttachment(fileData);
} catch (MessagingException e) {
e.printStackTrace();
}
finally{
try {
if (is != null)
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}

public native boolean passAttachment(byte[] buf);

static { System.loadLibrary("hello");}


My c++ code

#include <jni.h>
#include "MsgParser.h"
#include <stdio.h>

JNIEXPORT jboolean JNICALL Java_MsgParser_passAttachment
(JNIEnv *env, jobject obj, jbyteArray array){

// not sure what to do here to save image file data in bytearray to
harddisk.

}


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      06-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all,
>
> I am writing a sendmail milter application in Java. The incoming mails will
> usually have image file as attachments. My application is currently able to
> extract the ImageFile and save it on the filesystem. This part is working
> perfectly.
>
> ((MimeBodyPart)p).saveFile(new File(p.getFileName()));
>
> However, I would like to pass this file as a byte array to a c++ library
> instead and do the file saving and additional processing there instead. I
> know very little c++ though. Can someone help please? I am also not sure if
> the code I have written is doing what I think it is doing i.e. converting
> the file into a byte array and passing it to the native library.
>


This is off-topic for this comp.lang.c++. Setting followup to
comp.lang.java.programmer.

Perhaps this example will help in some way:

// A.java
class A
{
static
{
System.loadLibrary("A");
}
private native static void writeToFile(byte[] bytes);

public static final void main(String[] args)
{
writeToFile("Hello, world!".getBytes());
}
}

// A.cpp
#include <jni.h>
#include <cstddef>
#include <new>

// A wrapper class to ensure that GetByteArrayElements is always properly
// paired with a ReleaseByteArrayElements.
class jniByteArray
{
private:
// Purposely not implemented to prevent copying.
jniByteArray(const jniByteArray &);
jniByteArray & operator=(const jniByteArray &);

JNIEnv * m_env;
jbyteArray m_byteArray;
jbyte * m_bytes;
jboolean m_isCopy;
public:
jniByteArray(JNIEnv * env, jbyteArray bytes)
: m_env(env)
, m_byteArray(bytes)
, m_bytes(env->GetByteArrayElements(bytes, &m_isCopy))
{
if (!m_bytes)
throw std::bad_alloc();
}

~jniByteArray()
{
m_env->ReleaseByteArrayElements(m_byteArray, m_bytes, 0);
}

char * getBytes()
{
return reinterpret_cast<char *>(m_bytes);
}

std::size_t getSize() const
{
return std::size_t(m_env->GetArrayLength(m_byteArray));
}

bool isCopy() const
{
return bool(m_isCopy) ;
}
};

#include <fstream>

extern "C"
JNIEXPORT void JNICALL Java_A_writeToFile(JNIEnv * env,
jclass, jbyteArray bytes)
{
jniByteArray b(env, bytes);

std:fstream out("data.bin", std::ios::binary);
out.write(b.getBytes(), b.getSize());
}


--
Alan Johnson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM
PIL's ImageFile Parser and PNG's Silfheed Python 2 11-13-2008 10:09 PM
JNI/c++ - passing imagefile as a byte array to native library Java 2 06-07-2007 09:47 AM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments