samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToNew.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:23 +0200
changeset 46 c75861f07646
child 47 f464a16d553a
permissions -rw-r--r--
Using real pieces of code the talk about bridges
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package org.apidesign.impl.security.extension;
     7 
     8 import org.apidesign.impl.security.friendapi.DigestImplementation;
     9 import org.apidesign.impl.security.friendapi.DigestProvider;
    10 import java.nio.ByteBuffer;
    11 import java.security.MessageDigest;
    12 import java.security.NoSuchAlgorithmException;
    13 import java.util.logging.Level;
    14 import java.util.logging.Logger;
    15 
    16 /**
    17  *
    18  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    19  */
    20 // BEGIN: day.end.bridges.BridgeToNew
    21 public class BridgeToNew implements DigestProvider {
    22 
    23     public DigestImplementation create(String algorithm) {
    24         try {
    25             final MessageDigest md = MessageDigest.getInstance(algorithm);
    26             return new DigestImplementation(algorithm) {
    27 
    28                 @Override
    29                 public void update(ByteBuffer bb) {
    30                     md.update(bb);
    31                 }
    32 
    33                 @Override
    34                 public byte[] digest() {
    35                     return md.digest();
    36                 }
    37             };
    38         } catch (NoSuchAlgorithmException ex) {
    39             Logger.getLogger(BridgeToNew.class.getName()).log(Level.INFO, null, ex);
    40             return null;
    41         }
    42     }
    43 }
    44 // END: day.end.bridges.BridgeToNew