while ((str = stderr.readLine()) != null)
{
if (str.indexOf( "Error:" ) == 1)
{
proc.waitFor();
throw new PGPException( str.substring( 7 ).trim() );
}
/* If text was signed then there is an output like:
* Good signature from user "Joachim Lingner <jlingner@gmx.de>".
*/
else if (str.indexOf( "signature from user" ) != -1)
{
String user = str.substring( str.indexOf( '\"' )-1, str.lastIndexOf( '\"' ) ).trim();
info = new SignatureInfo();
info._userId = user;
info._bSignatureChecked = true;
info._bMessageVerified = (str.indexOf( "Good" ) == 0);
}
else if (str.indexOf( "Key matching expected Key ID" ) == 1)
{
String user = str.substring( 28, str.indexOf( "not found" ) ).trim();
info = new SignatureInfo();
info._userId = user;
info._bSignatureChecked = false;
info._bMessageVerified = false;
}
}
// provide output data
InputStream stdin = proc.getInputStream();
byte buf[] = new byte[0x10000];
while ((nRead = stdin.read( buf )) >= 0)
{
if (DEBUG)
{
System.out.print( "# writing output from stdin: " );
for ( int n = 0; n < nRead; ++n )
{
System.out.print( (char)buf[n] );
if (n < buf.length-1)
System.out.print( ", " );
}
}
//output.writeBytes( buf );
// One could assume that the implementer of XOutputStream
// checks for an EOF or a final token that denotes the end
// of the valid data package. Evidently this is not the
// case therefore we can't blindly write the whole array but
// have to write the same number of bytes as received from
// PGP in the InputS tream (Process.getInputStream()).
// If we wrote the whole buffer, then all the data are interpreted
// as pertaining to the mail.
// workaround: create a buffer with the size of the valid data block
// and use it in output.writeBytes rather then "buf"
if(nRead == -1)
aPlainText.writeBytes(buf);
else{
byte accurate[]=new byte[nRead];
System.arraycopy( buf, 0, accurate, 0, nRead);
aPlainText.writeBytes( accurate);
}
// ------------------------------------------------------------------
}
if (DEBUG)
System.out.println( "# output given." );
if (DEBUG && info != null)
{
if (info._bSignatureChecked)
System.out.println( "# "+(info._bMessageVerified ? "Good" : "Bad")+" signature for user "+info._userId+"!" );
else
System.out.println( "# user "+info._userId+" could not be verified!" );
}
if (DEBUG)
System.out.println( "### waiting for process to end..." );
proc.waitFor();
if (DEBUG)
{
System.out.println( "### process died." );
System.out.flush();
}
}
catch (InterruptedException exc)
{
throw new PGPException( exc.getMessage() );
}
catch (java.io.IOException exc)
{
throw new IOException( exc.getMessage(), null );
}