Problem reading reading efficiently C and Java l
Problem: reading, reading efficiently: C++ and Java l l 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 l Java uses both Streams and Readers ä streams, e. g. , Input. Stream, File. Input. Stream, Data. Input. Stream, . . . • streams deal with bytes, not characters ä readers, e. g. , Input. Stream. Reader, Buffered. Reader, File. Reader, String. Reader, … • readers deal with unicode characters, not raw bytes l Line-oriented reading (see Word. Enumerator. java) Buffered. Reader = new Buffered. Reader(new File. Reader(“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 l l 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 l Buffered readers allow for line-at-a-time reading ä sometimes we want to read word-at-a-time ä the String. Tokenizer 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 String. Tokenizer implementation of Enumeration • see Word. Enumeration. java for an example, see the related class Stream. Tokenizer in package java. io l Using the Word. Enumeration adapter makes it easier to re-use the design from C++, might be easier to use String. Buffer Duke CPS 108 17. 4
What about Java GUIs? l The AWT (abstract window toolkit) has several widgets and layout managers ä layout manager determines how widgets are placed in a window • • ä ä flow layout (left-to-right) border layout (north, south, east, west, center) grid layout (m x n grid of widgets) gridbag layout (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 l l Some class/object is a listener, implements a listener interface ä Action. Listener: buttons, menus, textfields, listboxes ä Item. Listener: listbox, checkbox ä Text. Listener: textcomponent (field, area) ä Mouse. Listener, Mouse. Motion. Listener ä Window. Listener: Window ä Key. Listener These are interfaces, hence implementations of all methods must be provided ä Mouse. Listener has five methods: mouse. Pressed, mouse. Released, mouse. Entered, mouse. Exited, mouse. Clicked ä Mouse. Listener. Adapter can be used but it’s a class • single inheritance, but nested classes provide some relief Duke CPS 108 17. 6
- Slides: 6