Home » Java programming language

Java SecurityManager checkRead() method with example

SecurityManager Class checkRead() method: Here, we are going to learn about the checkRead() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 14, 2019

Syntax:

    public void checkRead(FileDescriptor  file_des);
    public void checkRead(String fi);
    public void checkRead(String fi , Object cntxt);

SecurityManager Class checkRead() method

  • checkRead() method is available in java.lang package.
  • checkRead(FileDescriptor file_des) method invokes checkPermission with the RuntimePermission("readFileDescriptor") to read the file from the given file descriptor.
  • checkRead(String fi) method invokes checkPermission with the RuntimePermission(fi,"read") to read the file from the given fi parameter.
  • checkRead(String fi , Object cntxt) method invokes checkPermission with the FilePermission(fi,"read") to read file from the given fi parameter when cntxt is an instance of AccessControlContext otherwise it may throw an exception when cntxt is not instance of AccessControlContext.checkRead(FileDescriptor file_des), checkRead(String fi), checkRead(String fi , Object cntxt) methods may throw an exception at the time of reading file from different media.
  • checkRead(FileDescriptor file_des):
    • SecurityException – This exception may throw when the calling thread is not allowed to read the file from the given file descriptor.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkRead(String fi):
    • SecurityException – This exception may throw when the calling thread is not allowed to read the file from the given fi(file) parameter.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkRead(String fi , Object cntxt):
    • SecurityException – This exception may throw when the calling thread is not allowed to read files from the given fi (file) parameter or when cntxt(context) parameter is not an instance of AccessControlContext.
    • NullPointerException – This exception may throw when the given parameter is null.
  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, FileDescriptor file_des - This parameter represents the system-specific file descriptor.
  • In the second case, String fi - This parameter represents the system-specific file name.
  • In the third case, "String fi, Object cntxt"
    • String fi – Similar as defined in the second case.
    • Object cntxt – This parameter represents the system-specific security context.

Return value:

The return type of this method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of checkRead () method of SecurityManager class

import java.security.*;
import java.io.*;

public class CheckRead extends SecurityManager {
    public static void main(String[] args) {
        FileDescriptor file_desc = new FileDescriptor();
        String fi = "getProperties().doc";
        AccessControlContext cntxt = AccessController.getContext();

        // By using setProperty() method is to set the policy property 
        // with security manager
        System.setProperty("java.security.policy", "file:/C:/java.policy");

        // Instantiating a CheckRead object
        CheckRead cr = new CheckRead();

        // By using setSecurityManager() method is to set the
        // security manager
        System.setSecurityManager(cr);

        // By using checkRead(FileDescriptor) method is to
        //check that read file by using file descriptor
        cr.checkRead(file_desc);

        // By using checkRead(String) method is to
        // check that read file by using String 
        cr.checkRead(fi);

        // By using checkRead(String,cntxt) method is to
        // check that read file by using String when cntxt is an 
        // instance of AccessControlContext
        cr.checkRead(fi, cntxt);

        // Display the message 
        System.out.println("Accepted..");
    }
}

Output

Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "readFileDescriptor")
	at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
	at java.base/java.security.AccessController.checkPermission(AccessController.java:897)
	at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)
	at java.base/java.lang.SecurityManager.checkRead(SecurityManager.java:637)
	at CheckRead.main(CheckRead.java:26)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.