This repository has been archived by the owner on Jan 24, 2018. It is now read-only.
forked from smartrevolution/gnupg-for-java
/
GnuPGData.java
155 lines (135 loc) · 4.6 KB
/
GnuPGData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* $Id: GnuPGData.java,v 1.1 2005/01/24 13:56:58 stefan Exp $
* (c) Copyright 2005 freiheit.com technologies gmbh, Germany.
*
* This file is part of Java for GnuPG (http://www.freiheit.com).
*
* Java for GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
package com.freiheit.gnupg;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
Holds the data that you want to work on and stores the results
of crypto operations.
@author Stefan Richter, stefan@freiheit.com
*/
public class GnuPGData extends GnuPGPeer{
/**
Use the factory methods from GnuPGContext to
generate GnuPGData-Objects.
Generates an empty data object. Use this, if you want
to create a data object to hold a result from a crypto
operation.
*/
protected GnuPGData(){
setInternalRepresentation(gpgmeDataNew());
}
/*
Use the factory methods from GnuPGContext to
generate GnuPGData-Objects.
FIXME: This is not working! Use it, and it will crash the JVM.
*/
// public GnuPGData(File f) throws IOException{
// this();
// if(f != null && f.exists() && !f.isDirectory() && f.canRead()){
// FileInputStream fin = new FileInputStream(f);
// this.read(fin);
// }
// }
/**
Use the factory methods from GnuPGContext to
generate GnuPGData-Objects.
Generates a new data object containing the given String.
@param str your string
*/
protected GnuPGData(String str){
this(str.getBytes());
}
/**
Use the factory methods from GnuPGContext to
generate GnuPGData-Objects.
Generates a new Data-Object containing the given byte array.
@param data your data
*/
protected GnuPGData(byte[] data){
long gpgmeDataNewFromMem = gpgmeDataNewFromMem(data);
setInternalRepresentation(gpgmeDataNewFromMem);
}
/*
FIXME: This is not working! Use it, and it will crash the JVM.
*/
// public void read(InputStream in) throws IOException{
// if(in != null){
// gpgmeDataRead(getInternalRepresentation(), in);
// }
// }
/**
Writes the data/string contained in this data object
to the given Java OutputStream.
@param out OutputStream, where the data/string should be written
*/
public void write(OutputStream out) throws IOException{
if (out != null) {
gpgmeDataWrite(getInternalRepresentation(), out);
}
}
/**
Helper method to print out the data/string from this data object.
@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(this.size());
try{
this.write(baos);
result = baos.toString();
}
catch(IOException ioe){
result = ioe.getMessage();
}
return result;
}
/**
This calls immediately the release method for the datastructure
in the underlying gpgme library. This method is called by
the finalizer of the class anyway, but you can call it yourself
if you want to get rid of this datastructure at once and don't want to
wait for the non-deterministic garbage-collector of the JVM.
*/
public void destroy(){
if(getInternalRepresentation() != 0){
gpgmeDataRelease(getInternalRepresentation());
setInternalRepresentation(0);
}
}
/**
Releases underlying datastructures. Simple calls the destroy() method.
*/
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;
private native void gpgmeDataRelease(long l);
private native void gpgmeDataRead(long data, InputStream in) throws IOException;
}
/*
* Local variables:
* c-basic-offset: 4
* indent-tabs-mode: nil
* compile-command: "ant -emacs -find build.xml"
* End:
*/