View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb.utils;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.security.DigestOutputStream;
21  import java.security.MessageDigest;
22  
23  /**
24   * Convinience class to provide MD5 and length of a stream.
25   * 
26   * ATTENTION: don't use outside of jdeb
27   *  
28   * @author Torsten Curdt <tcurdt@vafer.org>
29   */
30  public class InformationOutputStream extends DigestOutputStream {
31  
32  	private final MessageDigest digest;
33  	private long size;
34  	
35  	public InformationOutputStream(OutputStream pStream, MessageDigest pDigest) {
36  		super(pStream, pDigest);
37  		digest = pDigest;
38  		size = 0;
39  	}
40  	
41  	public String getMd5() {
42  		return Utils.toHex(digest.digest());
43  	}
44  	
45  	public void write(byte[] b, int off, int len) throws IOException {
46  		super.write(b, off, len);
47  		size += len;
48  	}
49  
50  	public void write(int b) throws IOException {
51  		super.write(b);
52  		size++;
53  	}
54  
55  	public long getSize() {
56  		return size;
57  	}
58  }