跳至主要内容

inside java virtual machine notes (7)

inside java virtual machine notes (7) 2009-10 -08 13:00 / / On CD-ROM in file security/ex2/TextFileDisplayer.javaimport com.artima.security.doer.Doer; import java.io.FileReader; import java.io.CharArrayWriter; import java.io.IOException ; public class TextFileDisplayer implements Doer (private String fileName; public TextFileDisplayer (String fileName) (this.fileName = fileName;) public void doYourThing () (try (FileReader fr = new FileReader (fileName); try (CharArrayWriter caw = new CharArrayWriter ( ); int c; while ((c = fr.read ())!=- 1) (caw.write (c);) System.out.println (caw.toString ());} catch (IOException e) ( ) finally (try (fr.close ();) catch (IOException e ){}}} catch (IOException e ){}}} When you create aTextFileDisplayer object, you must pass afile path name to its constructor.The TextFileDisplayer constructor stores the passed path name in the filename instance variable.When you invoke doYourThing () on the TextFileDisplayer object, it will attempt to open and read the contents of the file and print them at the standard output.
Another example of adoYourThing ( ) method comes from classes Friend and Stranger, which appeared earlier in this chapter in the code signing example and are shown again here to refresh your memory:
/ / On CD-ROM in file / / security/ex2/com / artima / security / friend / Friend.javapackage com.artima.security.friend; import com.artima.security.doer.Doer; import java.security.AccessController; import java.security.PrivilegedAction; public class Friend implements Doer (private Doer next; private boolean direct; public Friend (Doer next, boolean direct) (this.next = next; this.direct = direct;) public void doYourThing () (if (direct) (next.doYourThing ();) else (AccessController . doPrivileged (new PrivilegedAction () (public Object run () (next.doYourThing (); return null ;}});}}}// On CD-ROM in file / / security/ex2/com/artima/security / stranger / Stranger.javapackage com.artima.security.stranger; import com.artima.security.doer.Doer; import java.security.AccessController; import java.security.PrivilegedAction; public class Stranger implements Doer (private Doer next; private boolean direct; public Stranger (Doer next, boolean direct) (this.next = next; this.direct = direct;) public void doYourThing () (if (direct) (next.doYourThing ();) else (AccessController.doPrivileged (new PrivilegedAction () (public Object run () (next.doYourThing (); return null ;}});}}} Friendand Stranger have much in common.They have identical instance variables, constructors, and doYourThing () methods.They differ only in their package and simple names.When you create anew Friend or Stranger object, you must pass to the constructor aboolean value and areference to another object whose class implements the Doer interface.The constructor stores the passed Doer reference in the instance variable, next, and the boolean value in the instance variable, direct.When doYourThing () is invoked on either aFriend or Stranger object, the method invokes doYourThing (), either directly or indirectly, on the Doer reference contained in next.If direct is true, Friend or Stranger's doYourThing () just invokes doYourThing () directly on next.Otherwise, Friend or Stranger's doYourThing () invokes doYourThing () on next indirectly, by way of adoPrivileged () call.
/ / On CD-ROM in file / / security/ex2/com/artima/security/friend/Friend.javapackage com.artima.security.friend; import com.artima.security.doer.Doer; import java.security.AccessController; import java.security.PrivilegedAction; public class Friend implements Doer (private Doer next; private boolean direct; public Friend (Doer next, boolean direct) (this.next = next; this.direct = direct;) public void doYourThing () (if (direct) (next. doYourThing ();) else (AccessController.doPrivileged (new PrivilegedAction () (public Object run () (next.doYourThing (); return null ;}});}}}// On CD-ROM in file / / security / ex2/com/artima/security/stranger/Stranger.javapackage com.artima.security.stranger; import com.artima.security.doer.Doer; import java.security.AccessController; import java.security.PrivilegedAction; public class Stranger implements Doer (private Doer next; private boolean direct; public Stranger (Doer next, boolean direct) (this.next = next; this.direct = direct;) public void doYourThing () (if (direct) (next.doYourThing (); ) else (AccessController.doPrivileged (new PrivilegedAction () (public Object run () (next.doYourThing (); return null ;}});}}} Friendand Stranger have much in common.They have identical instance variables, constructors, and doYourThing () methods.They differ only in their package and simple names.When you create anew Friend or Stranger object, you must pass to the constructor aboolean value and areference to another object whose class implements the Doer interface.The constructor stores the passed Doer reference in the instance variable, next, and the boolean value in the instance variable, direct.When doYourThing () is invoked on either aFriend or Stranger object, the method invokes doYourThing (), either directly or indirectly, on the Doer reference contained in next.If direct is true, Friend or Stranger's doYourThing () just invokes doYourThing () directly on next.Otherwise, Friend or Stranger's doYourThing () invokes doYourThing () on next indirectly, by way of adoPrivileged () call.
Another example of adoYourThing () method comes from classes Friend and Stranger, which appeared earlier in this chapter in the code signing example and are shown again here to refresh your memory:

inside java virtual machine notes (7) 2009-10-08 13:00 / / On CD-ROM in file security/ex2/TextFileDisplayer.javaimport com.artima.security.doer.Doer; import java.io. FileReader; import java.io.CharArrayWriter; import java.io.IOException; public class TextFileDisplayer implements Doer (private String fileName; public TextFileDisplayer (String fileName) (this.fileName = fileName;) public void doYourThing () (try (FileReader fr = new FileReader (fileName); try (CharArrayWriter caw = new CharArrayWriter (); int c; while ((c = fr.read ())!=- 1) (caw.write (c);) System.out.println (caw.toString ());} catch (IOException e) () finally (try (fr.close ();) catch (IOException e ){}}} catch (IOException e ){}}} When you create aTextFileDisplayer object , you must pass afile path name to its constructor.The TextFileDisplayer constructor stores the passed path name in the filename instance variable.When you invoke doYourThing () on the TextFileDisplayer object, it will attempt to open and read the contents of the file and print them at the standard output.

评论

此博客中的热门博文

How to Design a Store Front Sign

In this Article we will talk about designing a store front for a business. What color or size should your Letters & designs be? What to put on your sign and why?, then be ready to Get in touch with sign people and get your best deal. Large lettering with out a front sign could save you time and money Do your products have a special shape or color to emulate on your sign? Kodak is always in yellow and black, Coca-Cola white on red, Etc. Muffler places, have a sign in the shape of a muffler. your phone number on the store front some place, but not on the identification sign, (door, or window) Your Product & logos can be arranged on the front sign, or window. Not on the Pole sign, Store hours & phone number could fit in your design and by using the product color, it could add product recognition & enhance your design. Suppliers may provide plenty of advert...

Can Microsoft and Yahoo Co-Create New Businesses?

Negotiating The Future of Management Podcast Current Interactive Case Study Respectfully there's a parallel in modern history here. China-HongKong merger. It started as Two System One China. Controversial at that time. Now, many don't mind which system is better (respectfully just an opinion). Both are getting the best of each other under one country. Families are reunited regularly. Corporate identities sometimes focuses too much on what's different as a strategy. It may provide superiority, but does it always guarantee sustainability? Nature advocates the flourishing of various species; pursues diversity not for purposes of destruction but for better adaptability. Of the whole system. Being different doesn't mean one must separate oneself from the "whole" system. "Different" here is to provide a unique contribution to pserve the "whole". Task / role distribution , not identity separation. Preserving the whole system by b...

MP3 firmware upgrade --- switch

1: The most simple if the host machine using Sigma firmware and upgrade program is one! MP3 can be connected to the computer as long as the firmware installed on the computer, and then click on the face of that to upgrade the line of MP3! three antelope 7530 master points in several ways is to first get the firmware is sent to the machine within the sub-sub-folder off the machine DVR will automatically upgrade! Detailed steps have been described in general firmware! Shenzhen City Qing Wei Digital Appliance Co., Ltd.-KINGWAY http://www.kingwon.com.cn/ Technology Co., Ltd. Shenzhen, Pu-chih http://www.poozi.com/down.asp?le ...% D4% D8 & offset = 5 Section Crystal: http://www.fangke-model.com/ Chuanqi (ONKI): http://www.onki.cn/download.asp brother extremely grateful! CDT CDT http://xinvo.com/web/ ExcelStor MP3 http://www.hua-sun.com/xiazai.htm Bester http://www.szbaisite.com Beto www.beto.cn Butterfly www.it8.com.cn Acorp www.acorp.net.cn BES www.bes-chi...