Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2028 | es | 1 | package pt.estgp.es.exemplos.hibernate.utils; |
2 | |||
3 | import java.io.*; |
||
4 | import java.security.DigestInputStream; |
||
5 | import java.security.MessageDigest; |
||
6 | import java.security.NoSuchAlgorithmException; |
||
7 | |||
8 | /** |
||
9 | * @author Jorge Machado |
||
10 | * @date 27/Mai/2008 |
||
11 | * @see jomm.utils |
||
12 | */ |
||
13 | public class StreamsUtils |
||
14 | { |
||
15 | public static byte[] readBytes(5+0%2Fdocs%2Fapi+InputStream">InputStream stream) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
16 | { |
||
17 | 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream b = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream(); |
||
18 | int readedBytes; |
||
19 | byte[] buf = new byte[1024]; |
||
20 | while ((readedBytes = stream.read(buf)) > 0) |
||
21 | { |
||
22 | b.write(buf, 0, readedBytes); |
||
23 | } |
||
24 | b.close(); |
||
25 | return b.toByteArray(); |
||
26 | } |
||
27 | |||
28 | public static 1.5.0/docs/api/java/lang/String.html">String readString(5+0%2Fdocs%2Fapi+InputStream">InputStream stream) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
29 | { |
||
30 | 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream b = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream(); |
||
31 | int readedBytes; |
||
32 | byte[] buf = new byte[1024]; |
||
33 | while ((readedBytes = stream.read(buf)) > 0) |
||
34 | { |
||
35 | b.write(buf, 0, readedBytes); |
||
36 | } |
||
37 | b.close(); |
||
38 | return b.toString(); |
||
39 | } |
||
40 | |||
41 | public static void inputStream2File(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/io/File.html">File f) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
42 | { |
||
43 | f.getParentFile().mkdirs(); |
||
44 | 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(f); |
||
45 | inputStream2OutputStream(stream,out); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * ATENTION this method does not close the given stream |
||
50 | * @param stream |
||
51 | * @param f |
||
52 | * @return |
||
53 | * @throws IOException |
||
54 | */ |
||
55 | public static 1.5.0/docs/api/java/lang/String.html">String inputStream2FileGetMd5(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/io/File.html">File f) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
56 | { |
||
57 | f.getParentFile().mkdirs(); |
||
58 | 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(f); |
||
59 | return inputStream2OutputStreamGetMd5(stream, out); |
||
60 | } |
||
61 | |||
62 | public static void inputStream2OutputStream(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 5+0%2Fdocs%2Fapi+OutputStream">OutputStream out) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
63 | { |
||
64 | inputStream2OutputStream(stream,out,true,true); |
||
65 | } |
||
66 | |||
67 | public static void inputStream2OutputStream(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 5+0%2Fdocs%2Fapi+OutputStream">OutputStream out,boolean closeOut,boolean closeIn) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
68 | { |
||
69 | int readedBytes; |
||
70 | byte[] buf = new byte[1024]; |
||
71 | while ((readedBytes = stream.read(buf)) > 0) |
||
72 | { |
||
73 | out.write(buf, 0, readedBytes); |
||
74 | } |
||
75 | if(closeIn) |
||
76 | stream.close(); |
||
77 | if(closeOut) |
||
78 | out.close(); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Return digest Md5 from consumed bytes |
||
83 | * * ATENTION this method does not close the given stream |
||
84 | * @param stream |
||
85 | * @param out |
||
86 | * @return Md5 HEX |
||
87 | * @throws IOException |
||
88 | */ |
||
89 | public static 1.5.0/docs/api/java/lang/String.html">String inputStream2OutputStreamGetMd5(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 5+0%2Fdocs%2Fapi+OutputStream">OutputStream out) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
90 | { |
||
91 | 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest messageDigest = null; |
||
92 | try { |
||
93 | messageDigest = 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.getInstance("MD5"); |
||
94 | } catch (1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e) { |
||
95 | e.printStackTrace(); |
||
96 | } |
||
97 | |||
98 | 1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream digestInputStream = new 1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream(stream, messageDigest); |
||
99 | int readedBytes; |
||
100 | byte[] buf = new byte[1024]; |
||
101 | while ((readedBytes = digestInputStream.read(buf)) > 0) |
||
102 | { |
||
103 | out.write(buf, 0, readedBytes); |
||
104 | } |
||
105 | messageDigest = digestInputStream.getMessageDigest(); |
||
106 | 1.5.0/docs/api/java/lang/String.html">String md5 = BytesUtils.toHex(messageDigest.digest()); |
||
107 | out.close(); |
||
108 | return md5; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Return digest Md5 from consumed bytes |
||
113 | * * ATENTION this method does not close the given stream |
||
114 | * @param stream |
||
115 | * @param out |
||
116 | * @return Md5 HEX |
||
117 | * @throws IOException |
||
118 | */ |
||
119 | public static 1.5.0/docs/api/java/lang/String.html">String getFileMd5(1.5.0/docs/api/java/io/File.html">File f) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
120 | { |
||
121 | 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest messageDigest = null; |
||
122 | try { |
||
123 | messageDigest = 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.getInstance("MD5"); |
||
124 | } catch (1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e) { |
||
125 | e.printStackTrace(); |
||
126 | } |
||
127 | |||
128 | 1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream digestInputStream = new 1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(f), messageDigest); |
||
129 | int readedBytes; |
||
130 | byte[] buf = new byte[1024]; |
||
131 | while ((readedBytes = digestInputStream.read(buf)) > 0) |
||
132 | { |
||
133 | //nottinhg to do only calculate nd5 |
||
134 | } |
||
135 | messageDigest = digestInputStream.getMessageDigest(); |
||
136 | 1.5.0/docs/api/java/lang/String.html">String md5 = BytesUtils.toHex(messageDigest.digest()); |
||
137 | digestInputStream.close(); |
||
138 | return md5; |
||
139 | } |
||
140 | } |