samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:25 +0200
changeset 47 f464a16d553a
permissions -rw-r--r--
Simplified to does not contain the friend API, instead the SPI is directly define by the API
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package org.apidesign.api.security;
     7 
     8 import java.nio.ByteBuffer;
     9 import org.apidesign.spi.security.Digestor;
    10 
    11 /**
    12  *
    13  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    14  */
    15 final class DigestImplementation<Data> {
    16     private static final DigestorAccessorImpl ACCESSOR = new DigestorAccessorImpl();
    17     
    18     private final Digestor<Data> digestor;
    19     private final String algorithm;
    20     private Data data;
    21     
    22     private DigestImplementation(Digestor<Data> digestor, String algorithm, Data d) {
    23         this.digestor = digestor;
    24         this.algorithm = algorithm;
    25         this.data = d;
    26     }
    27     
    28     static <Data> DigestImplementation create(Digestor<Data> digestor, String algorithm) {
    29         Data d = ACCESSOR.create(digestor, algorithm);
    30         if (d == null) {
    31             return null;
    32         } else {
    33             return new DigestImplementation(digestor, algorithm, d);
    34         }
    35     }
    36 
    37     byte[] digest(ByteBuffer bb) {
    38         ACCESSOR.update(digestor, data, bb);
    39         return ACCESSOR.digest(digestor, data);
    40     }
    41 }