root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / util / md5 / MD5InputStream.java @ 21606
History | View | Annotate | Download (3.34 KB)
1 |
package es.prodevelop.gvsig.mobile.fmap.util.md5; |
---|---|
2 |
|
3 |
import java.io.FilterInputStream; |
4 |
import java.io.IOException; |
5 |
import java.io.InputStream; |
6 |
|
7 |
import org.apache.log4j.Logger; |
8 |
|
9 |
/**
|
10 |
* MD5InputStream, a subclass of FilterInputStream implementing MD5
|
11 |
* functionality on a stream.
|
12 |
* <p>
|
13 |
* Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br>
|
14 |
* (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
|
15 |
* Some changes Copyright (c) 2002 Timothy W Macinta <br>
|
16 |
* <p>
|
17 |
* This library is free software; you can redistribute it and/or
|
18 |
* modify it under the terms of the GNU Library General Public
|
19 |
* License as published by the Free Software Foundation; either
|
20 |
* version 2.1 of the License, or (at your option) any later version.
|
21 |
* <p>
|
22 |
* This library is distributed in the hope that it will be useful,
|
23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
25 |
* Library General Public License for more details.
|
26 |
* <p>
|
27 |
* You should have received a copy of the GNU Library General Public
|
28 |
* License along with this library; if not, write to the Free
|
29 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
30 |
* <p>
|
31 |
* See http://www.twmacinta.com/myjava/fast_md5.php for more information
|
32 |
* on this file.
|
33 |
* <p>
|
34 |
* Please note: I (Timothy Macinta) have put this code in the
|
35 |
* com.twmacinta.util package only because it came without a package. I
|
36 |
* was not the the original author of the code, although I did
|
37 |
* optimize it (substantially) and fix some bugs.
|
38 |
*
|
39 |
* @author Santeri Paavolainen <santtu@cs.hut.fi>
|
40 |
* @author Timothy W Macinta (twm@alum.mit.edu) (added main() method)
|
41 |
**/
|
42 |
/************************************************
|
43 |
* *
|
44 |
* Modfied By: *
|
45 |
* Prodevelop Integraci?n de Tecnolog?as SL *
|
46 |
* Conde Salvatierra de ?lava , 34-10 *
|
47 |
* 46004 Valencia *
|
48 |
* Spain *
|
49 |
* *
|
50 |
* +34 963 510 612 *
|
51 |
* +34 963 510 968 *
|
52 |
* gis@prodevelop.es *
|
53 |
* http://www.prodevelop.es *
|
54 |
* *
|
55 |
* gvSIG Mobile Team 2006 *
|
56 |
* *
|
57 |
************************************************/
|
58 |
|
59 |
public class MD5InputStream extends FilterInputStream { |
60 |
|
61 |
private static Logger logger = Logger.getLogger(MD5InputStream.class.getName()); |
62 |
/**
|
63 |
* MD5 context
|
64 |
*/
|
65 |
private MD5 md5;
|
66 |
|
67 |
/**
|
68 |
* Creates a MD5InputStream
|
69 |
* @param in The input stream
|
70 |
*/
|
71 |
public MD5InputStream (InputStream in) { |
72 |
super(in);
|
73 |
|
74 |
md5 = new MD5();
|
75 |
} |
76 |
|
77 |
/**
|
78 |
* Read a byte of data.
|
79 |
* @see java.io.FilterInputStream
|
80 |
*/
|
81 |
public int read() throws IOException { |
82 |
int c = in.read();
|
83 |
|
84 |
if (c == -1) |
85 |
return -1; |
86 |
|
87 |
if ((c & ~0xff) != 0) { |
88 |
logger.debug("MD5InputStream.read() got character with (c & ~0xff) != 0)!");
|
89 |
} else {
|
90 |
md5.Update(c); |
91 |
} |
92 |
|
93 |
return c;
|
94 |
} |
95 |
|
96 |
/**
|
97 |
* Reads into an array of bytes.
|
98 |
*
|
99 |
* @see java.io.FilterInputStream
|
100 |
*/
|
101 |
public int read (byte bytes[], int offset, int length) throws IOException { |
102 |
int r;
|
103 |
|
104 |
if ((r = in.read(bytes, offset, length)) == -1) |
105 |
return r;
|
106 |
|
107 |
md5.Update(bytes, offset, r); |
108 |
|
109 |
return r;
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* Returns array of bytes representing hash of the stream as
|
114 |
* finalized for the current state.
|
115 |
* @see MD5#Final
|
116 |
*/
|
117 |
public byte[] hash () { |
118 |
return md5.Final();
|
119 |
} |
120 |
|
121 |
public MD5 getMD5() {
|
122 |
return md5;
|
123 |
} |
124 |
|
125 |
|
126 |
|
127 |
} |
128 |
|