CS4812Java - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS4812Java

Description:

... are similar to serialized objects, except that the ... This produces greater security, and selective serialization ... Serialization & the transient State ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 29
Provided by: davidd1
Category:

less

Transcript and Presenter's Notes

Title: CS4812Java


1
CS4812--Java
  • Lecture 2
  • File I/O Fun with Streams

2
Java I/O Fun with Streams
  • Last week, we noticed that there were three types
    of basic streams in Java
  • System.in
  • System.out
  • System.err
  • You are already familiar with most of these
    streams. E.g.,
  • System.out.println (This is the out stream)

3
Java IO -- Basic Divisions (1.0)
  • InputStream
  • through inheritance, all derivatives of
    InputStream have the basic method read() to
    access a byte or an array of bytes
  • OutputStream
  • Through inheritance, all derivatives of
    OutputStream have the basic method write() to
    write a single byte

4
Types of InputStreams (1.0)
5
Decorator Classes--eh?
  • Java IO uses the concept of decorator objects
    to provide layers of functionality to IO classes
  • Concept A decorator pattern wraps your inner
    object, all using the same interface devices.
  • Pro/Con Flexibility with the cost of complexity
  • Example notice how may IO classes feature the
    readLine() method.

6
FilterInputStream Class
  • The FilterInputStream offers a grab bag of
    methods
  • Sun this is the base class for enhancing input
    stream functionality
  • Eckel we couldnt figure out where else to put
    this stuff, but it seemed like it belonged
    together TIJ (9th ed.)
  • Avoid it--use InputStreamReader (JDK 1.1)

7
Keyboard Access Use InputStreamReader (1.1)
  • For reading keystrokes, use an InputStreamReader
  • Wrap with a BufferedReader decorator
  • public IOHelper(boolean DEBUG)
  • this.DEBUG DEBUG
  • //// For reading data from
    standard in (stdin)
  • iStreamReader new
    InputStreamReader(System.in)
  • bReader new BufferedReader(iStreamRe
    ader)
  • // constructor

8
Hierarchy of BufferedReader
9
BufferedReader in Action
  • public String readLine()
  • String strInput ""
  • try strInput bReader.readLine()
  • catch (Exception e)
  • System.err.println ("Error
    reading from keyboard\n\t"
  • e.toString())
  • e.printStackTrace()
  • System.exit(0)
  • return strInput
  • // readLine()

10
The File Class (1.0)
  • Misnomer the File class does not necessarily
    refer to a single file. It can represent the
    name of a single file, or the names of a set of
    files.
  • The method list() returns a String array of
    file names
  • Therefore, filepath would be a better name
  • There is no JDK 1.1 equivalent

11
A Directory Listing
  • public class ls
  • public static void main (String arg) try
  • File path new File (.)
  • String listing path.list()
  • for(int countlisting.length count--gt0)
  • System.out.println(listcount)
  • catch(Exception e)e.printStackTrace()

12
A FilenameFilter Example
  • public void printDirListing()
  • final String list / MUST be
    final /
  • final File path new File(".") /
    MUST be final /
  • list path.list( new
    FilenameFilter()
  • public boolean accept
    (File dir, String n)
  • String f new
    File(n).getName().toLowerCase()
  • // don't list
    .java files
  • return
    f.indexOf(".class")-1

  • f.indexOf(".java")-1
  • // accept
  • //FilenameFilter
  • ) // new listing for path with
    anonymous inner class
  • for (int i0 ilt list.length i)
  • System.out.println
    ("\t"listi)
  • // printDirListing()

13
Object Serialization--eh?
  • Normally, objects live only during the life of
    the program. Stop the program, and the object
    disappears
  • JDK 1.1 offers the Serializable interface to
    create lightweight persistence.
  • Persistent objects live beyond the life of the
    program, usually in byte from on disk or on a
    network.

14
Object Serialization--Oh, yea. That stuff
  • You may have heard about object persistence as
    a new Java buzzword.
  • The Serializable interface is lightweight because
    you must manually create/load persistent objects.
  • Necessary for remote method invocation (RMI),
    where objects live on another machine

15
Object Serialization--More
  • Serialization is also necessary for Java Bean
    design, where functional components have state
    information created and saved during design, not
    runtime.
  • How? Just have your class impelement the
    Serializable interface
  • E.g., the Equation.java class

16
Saving Serialized Objects
  • Simple create an OutputStream, and wrap it with
    an ObjectOutputStream.
  • Read your e-mail!!!
  • Twin methods
  • writeObject()
  • readObject() / requires InputStream wrapped by
    ObjectInputStream /
  • Restoring objects requires .class file!
  • Therefore, dont modify your Equation.java files!

17
Externalization of Object
  • JDK 1.1 also has the java.io.Externalizable
    class. This interface extends Serializable and
    adds two methods you must implement
  • public abstract void readExternal(ObjectInput in)
    throws IOException, ClassNotFoundException
  • public abstract void writeExternal(ObjectOutput
    out) throws IOException

18
Externalized Objects--More
  • Externalized Objects are similar to serialized
    objects, except that the objects themselves
    control the field creation during reading and
    writing.
  • The readExternal and writeExternal are called
    with readObject() and writeObject()
  • This produces greater security, and selective
    serialization
  • Constructors for externalized objects must be
    public

19
Serialization the transient State
  • There may be time when we want to serialize an
    object, but omit certain key features,like
    password or other sensitive data.
  • Even objects and data identified as private get
    serialized
  • Solution use the transient keyword
  • private transient String password pssst.
  • Data and methods identified as transient are not
    serialized

20
String Parsing--Tips
  • You might consider the following sequence of
    steps to prepare your String equations
  • / Given String eq . . . /
  • eq removeY(eq)
  • eq removeWhiteSpaces(eq)
  • eq convertTrig(eq)
  • eq insertParens(eq)
  • eq insertOperands(eq)
  • eq insertSpaces(eq)

21
Removing the Y portion
  • Reasoning
  • We know all equations will start with the
    expression y .
  • We cant count on spacing, so we shouldnt just
    lop off the left two characters.
  • How do we do we separate equations out in
    mathematics? -- look for the sign.
  • Everything to the left of the sign may be
    considered junk, since were always solving for
    y.
  • java.lang.String.substring() method might be
    useful

22
Removing the Y portion
  • We can find where in the String the sign is
    located using
  • java.lang.String.indexOf()
  • Then just take the substring starting at the
    index 1
  • Remember Strings elements are numbered from zero!

23
Removing the Y portion
  • /
  • removeY (String) -- remove the "y" portion
    of the equation, based on location of sign
  • All characters to the left of the sign
    are removed
  • _at_param String eq -- the equation that might
    contain 'y '
  • _at_return String eq -- the equation without the
    y expression
  • /
  • public String removeY(String eq)
  • if (eq.indexOf("")!-1)
  • return eq.substring(eq.indexOf("")
    1, eq.length())
  • else return eq
  • // removeY

24
A peak at the insertOperands() method
  • We assume that our operands and function calls
    are all single char expressions, and that all
    white space has been purged
  • Lets look at an example to develop the logic of
    inserting operands
  • 3s(x)5x-xx / the y is omitted! /
  • We want this to read
  • 3 s (x) 5 x x x

25
insertOperands() -- logic
  • We can use two pointers to traverse the length of
    the String current and previous chars. The
    current char is added to a new String, except
    that occasional are added, depending on the
    relationship between the current and previous char

26
insertOperands() -- logic
  • 3s(x)5x-xx

x /x adjacent
fcn adjacent
27
insertOperands() method
  • We therefore need helper methods to
  • identify a char as a function (s,S,c,C,t,T,l,L)
  • identify a char as a number (0-9 and ,)
  • public boolean isNumber(char c)
  • return (Character.isDigit(c) c'.'
    c'E' c'e'
  • c'P' c'p') // isNumber
  • identify a char as an operand (,,,-,/, etc.)

28
insertOperands() -- Logic Table
Write a Comment
User Comments (0)
About PowerShow.com