Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read and calculate hash of file on the internet
    text
    copied!<p>I have a url of a file on the Internet. I need to calculate the SHA1 hash, and read this file by each line. I know how to do this, but I read this file twice which probably isn't a very good solution. </p> <p>How can I do this more effectively?</p> <p>Here is my code:</p> <pre><code>URL url = new URL(url); URLConnection urlConnection = url.openConnection(); urlConnection.setConnectTimeout(1000); urlConnection.setReadTimeout(1000); logger.error(urlConnection.getContent() + " "); InputStream is = urlConnection.getInputStream(); // first reading of file is: int i; File file = new File("nameOfFile"); BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName())); while ((i = bis.read()) != -1) { bos.write(i); } bos.flush(); bis.close(); sha1(file); // second reading of file is: BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; while ((line = reader.readLine()) != null) { // do something } protected byte[] sha1(final File file) throws Exception { if (file == null || !file.exists()) { return null; } final MessageDigest messageDigest = MessageDigest.getInstance(SHA1); InputStream is = new BufferedInputStream(new FileInputStream(file)); try { final byte[] buffer = new byte[1024]; for (int read = 0; (read = is.read(buffer)) != -1;) { messageDigest.update(buffer, 0, read); } } finally { IOUtils.closeQuietly(is); } return messageDigest.digest(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload