001    /*
002     * Copyright 2005 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.vafer.jdeb.utils;
017    
018    import java.io.IOException;
019    import java.io.OutputStream;
020    import java.security.DigestOutputStream;
021    import java.security.MessageDigest;
022    
023    /**
024     * Convinience class to provide MD5 and length of a stream.
025     * 
026     * ATTENTION: don't use outside of jdeb
027     *  
028     * @author Torsten Curdt <tcurdt@vafer.org>
029     */
030    public class InformationOutputStream extends DigestOutputStream {
031    
032            private final MessageDigest digest;
033            private long size;
034            
035            public InformationOutputStream(OutputStream pStream, MessageDigest pDigest) {
036                    super(pStream, pDigest);
037                    digest = pDigest;
038                    size = 0;
039            }
040            
041            public String getMd5() {
042                    return Utils.toHex(digest.digest());
043            }
044            
045            public void write(byte[] b, int off, int len) throws IOException {
046                    super.write(b, off, len);
047                    size += len;
048            }
049    
050            public void write(int b) throws IOException {
051                    super.write(b);
052                    size++;
053            }
054    
055            public long getSize() {
056                    return size;
057            }
058    }