ARTICLE AD BOX
If you read the JNI documentation, the Array Operations section of Chapter 4 explains how to access the raw bytes of a Java byte[] array (jbyteArray). You want the GetByteArrayElements() and ReleaseByteArrayElements() functions, eg:
jsize rawDataLen = GetArrayLength(env, data); jbyte* rawData = GetByteArrayElements(env, data, NULL); // use rawData as needed, then... ReleaseByteArrayElements(env, data, rawData, 0);You can then type-cast the jbyte* pointer to a uint8_t* pointer when giving it to the RawPacket.
Also, if you read the PcapPlusPlus documentation, it explains that the RawPacket class has an internal deleteRawDataAtDestructor flag that is true by default, which causes the packet to free its assigned rawData when it is no longer needed. But, you don't want that to happen in this case, because the packet will not own the memory of the Java byte array, only refer to it. So, you need to set that flag to false instead, and the only way I see to do that is to pass the rawData pointer to the packet's constructor instead of to its setRawData() method, eg:
// jbyteArray data jsize rawDataLen = GetArrayLength(env, data); jbyte* rawData = GetByteArrayElements(env, data, NULL); pcpp::RawPacket rawPacket(reinterpret_cast<uint8_t*>(rawData), static_cast<int>(rawDataLen), timestamp, false); // use rawPacket as needed, then... ReleaseByteArrayElements(env, data, rawData, 0);