Code example for DigestOutputStream

Methods: write

0
    ) 
    public final void test_write$BII_3() 
        throws NoSuchAlgorithmException, 
               IOException { 
        // check precondition 
        assertTrue(MY_MESSAGE_LEN % (CHUNK_SIZE+1) != 0);
         
        for (int k=0; k<algorithmName.length; k++) {
            try { 
                ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
                MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
                DigestOutputStream dos = new DigestOutputStream(bos, md);
                 
                // write message by chunks 
                for (int i=0; i<MY_MESSAGE_LEN/(CHUNK_SIZE+1); i++) {
                    dos.write(myMessage, i*(CHUNK_SIZE+1), CHUNK_SIZE+1);
                } 
                // write remaining bytes 
                dos.write(myMessage,
                        MY_MESSAGE_LEN/(CHUNK_SIZE+1)*(CHUNK_SIZE+1),
                        MY_MESSAGE_LEN % (CHUNK_SIZE+1));
                // check write 
                assertTrue("write", Arrays.equals(myMessage, bos.toByteArray()));
                // check that associated digest has been updated properly 
                assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(),
                        MDGoldenData.getDigest(algorithmName[k])));
                return; 
            } catch (NoSuchAlgorithmException e) {
                // allowed failure 
            } 
        } 
        fail(getName() + ": no MessageDigest algorithms available - test not performed");
    }