|
| |
"Java & Security"
Vol. 5, Issue 10, p. 52
Listing 1
//CodeMySecurityManager.java
public class MySecurityManager extends SecurityManager
{
private boolean flag;
public MySecurityManager(boolean flag)
{
this.flag = flag;
}
// Override checkRead function
public void checkRead (String s)
{
if(!flag)
{
throw new SecurityException("checkRead");
}
}
// Override checkWrite function
public void checkWrite (String s)
{
if(!flag)
{
throw new SecurityException("checkWrite");
}
}
}
Listing 2
// Code MyTest.java
import java.io.*;
public class MyTest
{
public static void main (String[] args)
{
try
{
MySecurityManager secMgr = new MySecurityManager(true);
// Set the security manager
System.setSecurityManager(secMgr);
}
catch (SecurityException excp)
{
System.out.println("Can't Change the SecurityManager!");
}
// Construct an Object
MyTest test = new MyTest();
test.testFunc();
}
public void testFunc()
{
try
{
BufferedReader is = new BufferedReader(
new FileReader("input.txt"));
DataOutputStream os = new DataOutputStream(
new FileOutputStream("out-
put.txt"));
System.out.println("Successfully opened the files for
read/write ");
String readString;
while ((readString = is.readLine()) != null)
{
os.writeBytes(readString);
os.writeByte('\n');
}
System.out.println("Successfully performed the
read/write operation");
is.close();
os.close();
}
catch (IOException excp)
{
System.err.println("IOException.");
}
}
}
|
|
All Rights Reserved
Copyright © 2004 SYS-CON Media, Inc.
E-mail: info@sys-con.com
Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.
|