Open In App

DigestInputStream.toString() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The toString() method of java.security.DigestInputStream class provide the object of DigestInputStream in string format.
Syntax: 
 

public String toString()

Return Value: This method returns the object of DigestInputStream in string format.
Note : All the programs in this article won’t run on online IDE as no ‘name’ file exists. You can check this code on Java compiler on your system. To check this code, create a file ‘name’ on your system.
Below are the examples to illustrate the toString() method:
Example 1: 
 

Java




// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("MD5");
 
            // creating and initializing
            // object of InputStream
            InputStream is
                = new FileInputStream("f:/java/name.txt");
 
            // creating and initializing
            // object of DigestInputStream
            DigestInputStream di
                = new DigestInputStream(is, sr);
 
            // getting the string representation
            // of DigestInputStream
            // using toString() method
            String str = di.toString();
 
            // display the result
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Status : [Digest Input Stream] MD5 Message Digest from SUN, 

 

Example 2: 
 

Java




// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("SHA-1");
 
            // creating and initializing
            // object of InputStream
            InputStream is
                = new FileInputStream("f:/java/name.txt");
 
            // creating and initializing
            // object of DigestInputStream
            DigestInputStream di
                = new DigestInputStream(is, sr);
 
            // getting the string
            // representation of DigestInputStream
            // using toString() method
            String str = di.toString();
 
            // display the result
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Status : [Digest Input Stream] SHA-1 Message Digest from SUN, 

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/DigestInputStream.html#toString–
 



Last Updated : 05 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads