Blame |
Last modification |
View Log
| RSS feed
package pt.estgp.es.exemplos.hibernate.utils;
import java.io.*;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author Jorge Machado
* @date 27/Mai/2008
* @see jomm.utils
*/
public class StreamsUtils
{
public static byte[] readBytes
(5+0%2Fdocs%2Fapi+InputStream">InputStream stream
) throws 1.5.0/docs/api/java/io/IOException.html">IOException
{
1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream b =
new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
int readedBytes
;
byte[] buf =
new byte[1024
];
while ((readedBytes = stream.
read(buf
)) > 0
)
{
b.
write(buf, 0, readedBytes
);
}
b.
close();
return b.
toByteArray();
}
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
{
1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream b =
new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
int readedBytes
;
byte[] buf =
new byte[1024
];
while ((readedBytes = stream.
read(buf
)) > 0
)
{
b.
write(buf, 0, readedBytes
);
}
b.
close();
return b.
toString();
}
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
{
f.
getParentFile().
mkdirs();
1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out =
new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(f
);
inputStream2OutputStream
(stream,out
);
}
/**
* ATENTION this method does not close the given stream
* @param stream
* @param f
* @return
* @throws IOException
*/
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
{
f.
getParentFile().
mkdirs();
1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out =
new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(f
);
return inputStream2OutputStreamGetMd5
(stream, out
);
}
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
{
inputStream2OutputStream
(stream,out,
true,
true);
}
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
{
int readedBytes
;
byte[] buf =
new byte[1024
];
while ((readedBytes = stream.
read(buf
)) > 0
)
{
out.
write(buf, 0, readedBytes
);
}
if(closeIn
)
stream.
close();
if(closeOut
)
out.
close();
}
/**
* Return digest Md5 from consumed bytes
* * ATENTION this method does not close the given stream
* @param stream
* @param out
* @return Md5 HEX
* @throws IOException
*/
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
{
1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest messageDigest =
null;
try {
messageDigest =
1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.
getInstance("MD5");
} catch (1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e
) {
e.
printStackTrace();
}
1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream digestInputStream =
new 1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream(stream, messageDigest
);
int readedBytes
;
byte[] buf =
new byte[1024
];
while ((readedBytes = digestInputStream.
read(buf
)) > 0
)
{
out.
write(buf, 0, readedBytes
);
}
messageDigest = digestInputStream.
getMessageDigest();
1.5.0/docs/api/java/lang/String.html">String md5 = BytesUtils.
toHex(messageDigest.
digest());
out.
close();
return md5
;
}
/**
* Return digest Md5 from consumed bytes
* * ATENTION this method does not close the given stream
* @param stream
* @param out
* @return Md5 HEX
* @throws IOException
*/
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
{
1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest messageDigest =
null;
try {
messageDigest =
1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.
getInstance("MD5");
} catch (1.5.0/docs/api/java/security/NoSuchAlgorithmException.html">NoSuchAlgorithmException e
) {
e.
printStackTrace();
}
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
);
int readedBytes
;
byte[] buf =
new byte[1024
];
while ((readedBytes = digestInputStream.
read(buf
)) > 0)
{
//nottinhg to do only calculate nd5
}
messageDigest = digestInputStream.
getMessageDigest();
1.5.0/docs/api/java/lang/String.html">String md5 = BytesUtils.
toHex(messageDigest.
digest());
digestInputStream.
close();
return md5
;
}
}