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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    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.getFileN ame()));

    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.getInputStrea m();

    DataInputStream dis = new DataInputStream (is);
    String data = "";
    int size = dis.available() ;
    int offset = 0;
    do{
    byte [] byteArray = new byte[size];
    dis.read(byteAr ray, offset, size);
    data += new String(byteArra y);
    offset = size + 1;
    size = dis.available() ;
    }while(size 0);
    byte [] fileData = data.getBytes() ;
    result = passAttachment( fileData);
    } catch (MessagingExcep tion e) {
    e.printStackTra ce();
    }
    finally{
    try {
    if (is != null)
    is.close();
    } catch (IOException ex) {
    ex.printStackTr ace();
    }
    }
    return result;
    }

    public native boolean passAttachment( byte[] buf);

    static { System.loadLibr ary("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

  • Alan Johnson

    #2
    Re: [OT] JNI/c++ - passing imagefile as a byte array to native library

    abhi_sri@gmx.ne t 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.getFileN ame()));
    >
    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.loadLibr ary("A");
    }
    private native static void writeToFile(byt e[] bytes);

    public static final void main(String[] args)
    {
    writeToFile("He llo, world!".getByte s());
    }
    }

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

    // A wrapper class to ensure that GetByteArrayEle ments is always properly
    // paired with a ReleaseByteArra yElements.
    class jniByteArray
    {
    private:
    // Purposely not implemented to prevent copying.
    jniByteArray(co nst jniByteArray &);
    jniByteArray & operator=(const jniByteArray &);

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

    ~jniByteArray()
    {
    m_env->ReleaseByteArr ayElements(m_by teArray, m_bytes, 0);
    }

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

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

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

    #include <fstream>

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

    std::ofstream out("data.bin", std::ios::binar y);
    out.write(b.get Bytes(), b.getSize());
    }


    --
    Alan Johnson

    Comment

    Working...