473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Jun 6 '07 #1
1 9265
ab******@gmx.net 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::ofstream out("data.bin", std::ios::binary);
out.write(b.getBytes(), b.getSize());
}
--
Alan Johnson
Jun 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
6
by: Germic | last post by:
Hi, I've never got an answer to this question in any group till now.... How do we pass a array of structures between a C# and a C DLL, I am using RCW interop? The C# Library gets an array of...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
9
by: =?Utf-8?B?U2hhcm9u?= | last post by:
In my managed code (C#) I have a some dozens of single dimension byte array. I get each of this arrays from another unmanaged code (first DLL). I need to stitch these arrays to a dual dimensions...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
11
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as...
0
by: eitanyan | last post by:
I am trying to create a Java to .Net interop and the way I am doing it is by creating a C# com object and a native unmanaged c++ dll that uses JNIEnv of java. the java is loading the native c++ dll...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.