Transcript slides17

Problem: reading, reading efficiently: C++ and Java


Many C++ stream implementations are slow, sometimes
reading a file needs to be a fast operation
 we could use raw C I/O, but this isn’t C++/OO, requires
that we adapt e.g., use char * instead of string
 solution: hide the raw and fast C I/O behind a class, we
implement a wrapper or adapter class
The general Adapter pattern is richer than this application
 given a class, or set of classes, how can we use them if the
classes don’t conform to our application’s interface
• example: given another group’s scandir library implement
your ls program [that used your library originally]

design for adaptation, you can’t anticipate all uses, make it
simple to adapt your interface when needed
Duke CPS 108
17. 1
Using the Reader hierarchy in Java

Java uses both Streams and Readers
 streams, e.g., InputStream, FileInputStream,
DataInputStream, ...
• streams deal with bytes, not characters

readers, e.g., InputStreamReader, BufferedReader,
FileReader, StringReader, …
• readers deal with unicode characters, not raw bytes

Line-oriented reading (see WordEnumerator.java)
BufferedReader = new BufferedReader(new FileReader(“poe.txt”);


what if we want word-at-a-time reading?
how can we use iterator/enumeration pattern?
• Use an adapter to provide the interface we’re accustomed to
Duke CPS 108
17. 2
Some C++/C I/O concepts


In C++ streams are used for I/O
 streams can be bound to files, strings, buffer source
In C, two abstractions/primitives are used for reading: FILE
and file descriptors
 FILE is an adapter (in some sense) to system-dependent
structures that correspond to files, it’s an opaque type
 use FILE * since we don’t really know what a FILE is
• fscanf, fprintf, fgets, fputs, fgetc, ungetc, …
• also for stdin, stdout, stderr, e.g., scanf, printf, ...

file descriptor is an int associated with a FILE
• really a map to a system-maintained FILE object
• primitive read, write, lseek, … (see fsread.c)
Duke CPS 108
17. 3
Java reading

Buffered readers allow for line-at-a-time reading
 sometimes we want to read word-at-a-time
 the StringTokenizer class breaks a string into tokens
• a token is often typed, e.g., string, int, double, etc., when
implementing a compiler, here a token is a substring
• similar C string function called strtok (see tokens.cc)

whitespace-delimited words can be tokenized using
StringTokenizer implementation of Enumeration
• see WordEnumeration.java for an example, see the related
class StreamTokenizer in package java.io

Using the WordEnumeration adapter makes it easier to re-use
the design from C++, might be easier to use StringBuffer
Duke CPS 108
17. 4
What about Java GUIs?

The AWT (abstract window toolkit) has several widgets and
layout managers
 layout manager determines how widgets are placed in a
window
•
•
•
•


flow layout
border layout
grid layout
gridbag layout
(left-to-right)
(north, south, east, west, center)
(m x n grid of widgets)
(fine grained placement control)
resizing and redrawing can be tricky
events, e.g., mouse-down, button-press, etc. must be
listened to by objects taking action on event
• similar to Qt signal/slot idea
• events/event listeners correspond to signals/slots
Duke CPS 108
17. 5
Listeners, Listener Adapters


Some class/object is a listener, implements a listener interface
 ActionListener: buttons, menus, textfields, listboxes
 ItemListener: listbox, checkbox
 TextListener: textcomponent (field, area)
 MouseListener, MouseMotionListener
 WindowListener: Window
 KeyListener
These are interfaces, hence implementations of all methods
must be provided
 MouseListener has five methods: mousePressed,
mouseReleased, mouseEntered, mouseExited, mouseClicked

MouseListenerAdapter can be used but it’s a class
• single inheritance, but nested classes provide some relief
Duke CPS 108
17. 6