Skip to content
This repository has been archived by the owner on Jan 24, 2018. It is now read-only.

Commit

Permalink
add GnuPGData.size() to improve ByteArrayOutputStream performance
Browse files Browse the repository at this point in the history
Since ByteArrayOutputStreams are used to get data out of GnuPGData, add a
size() method so that the ByteArrayOutputStream can pre-allocate the memory
needed, or at least close to the final number.
  • Loading branch information
eighthave committed Mar 13, 2014
1 parent eb3d094 commit 1c912a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions jni/GnuPGData.c
Expand Up @@ -5,9 +5,17 @@
#include "gpgmeutils.h"

#include <gpgme.h>
#include <data.h>

#define BUFSIZE 1024


JNIEXPORT jsize JNICALL
Java_com_freiheit_gnupg_GnuPGData_gpgmeSize(JNIEnv * env, jobject self, jlong data)
{
return (jsize) (DATA(data))->data.mem.size;
}

JNIEXPORT jlong JNICALL
Java_com_freiheit_gnupg_GnuPGData_gpgmeDataNewFromMem(JNIEnv * env,
jobject self,
Expand Down
9 changes: 7 additions & 2 deletions src/com/freiheit/gnupg/GnuPGData.java
Expand Up @@ -99,11 +99,11 @@ public void write(OutputStream out) throws IOException{
/**
Helper method to print out the data/string from this data object.
@return String representation of the data contained in this data object (expect weired results with binary data)
@return String representation of the data contained in this data object (expect weird results with binary data)
*/
public String toString(){
String result = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream(this.size());
try{
this.write(baos);
result = baos.toString();
Expand Down Expand Up @@ -135,6 +135,11 @@ protected void finalize(){
destroy();
}

public int size() {
return gpgmeSize(getInternalRepresentation());
}

private native int gpgmeSize(long l);
private native long gpgmeDataNewFromMem(byte[] plain);
private native long gpgmeDataNew();
private native void gpgmeDataWrite(long l, OutputStream out) throws IOException;
Expand Down

0 comments on commit 1c912a1

Please sign in to comment.