2011-03-27 7 views
7

Tengo un CipherOutputStream respaldado por otro OutputStream. Una vez que haya terminado de escribir todos los datos que necesito encriptados en CipherOutputStream, debo adjuntar algunos datos no encriptados.¿Cómo puedo forzar que CipherOutputStream termine de encriptar pero deje la secuencia subyacente abierta?

The documentation for CipherOutputStream dice que al llamar al flush() no se forzará la salida del bloque final del encriptador; para eso necesito llamar al close(). Pero close() también cierra el OutputStream subyacente, que aún tengo que escribir más.

¿Cómo puedo forzar al último bloque fuera del encriptador sin cerrar la secuencia? ¿Debo escribir mi propio NonClosingCipherOutputStream?

Respuesta

6

Si no tiene una referencia al Cipher, puede pasar un FilterOutputStream al método que crea el CipherOutputStream. En el FilterOutputStream, anule el método close para que no cierre la secuencia.

+0

Usaría esta respuesta como base para crear algo como NonClosingCipherOutputStream, pero inspirado por el ejemplo de 'DigestOutputStream.on (...)' Crearía un método 'cipherFinish()' después del cual la transmisión simplemente pasaría datos a el sistema operativo subyacente. –

0

Si usted tiene una referencia al objeto de que los CipherCipherOutputStream envolturas, que deben ser capaces de hacer lo que hace CipherOutputStream.close():

llamada Cipher.doFinal, entonces flush() la CiperOutputStream, y continuar.

+0

Así que votaron esta inicialmente, ya que parecía una buena idea, pero no lo es. cipher.doFinal() devuelve los mismos bytes una y otra vez, de modo que si los inserta manualmente en la secuencia de salida, luego escribe más cosas y, finalmente, cierra CipherOutputStream, se vuelven a insertar. –

1

tal vez usted puede envolver su OutputStream antes de poner en un cipheroutputstream

/** 
* Represents an {@code OutputStream} that does not close the underlying output stream on a call to {@link #close()}. 
* This may be useful for encapsulating an {@code OutputStream} into other output streams that does not have to be 
* closed, while closing the outer streams or reader. 
*/ 
public class NotClosingOutputStream extends OutputStream { 

    /** The underlying output stream. */ 
    private final OutputStream out; 

    /** 
    * Creates a new output stream that does not close the given output stream on a call to {@link #close()}. 
    * 
    * @param out 
    *   the output stream 
    */ 
    public NotClosingOutputStream(final OutputStream out) { 
     this.out = out; 
    } 

    /* 
    * DELEGATION TO OUTPUT STREAM 
    */ 

    @Override 
    public void close() throws IOException { 
     // do nothing here, since we don't want to close the underlying input stream 
    } 

    @Override 
    public void write(final int b) throws IOException { 
     out.write(b); 
    } 

    @Override 
    public void write(final byte[] b) throws IOException { 
     out.write(b); 
    } 

    @Override 
    public void write(final byte[] b, final int off, final int len) throws IOException { 
     out.write(b, off, len); 
    } 

    @Override 
    public void flush() throws IOException { 
     out.flush(); 
    } 
} 

esperanza de que ayuda a

Cuestiones relacionadas