IO Lecture 5. Everything is streams … almost In Java Input/Output is mainly stream based In...

Post on 21-Dec-2015

218 views 4 download

transcript

IO

Lecture 5

Everything is streams … almost

In Java Input/Output is mainly stream based In java.io:

• Console I/O

• File I/O

• One Exception: RandomAccessFile not a stream

Raw byte or multi byte characters. Later we will look at java.nio

• “nio”=New IO

A stream is

…sequential by nature Data is considered to be one contiguous

(one dimensional) group of information from beginning to end.

Dual stream hierarchies in Java

Byte-oriented vs character-oriented• Byte orientation has the longest history

• Internationalization movement induced (during the 90’s) a need for multibyte characters.

Byte-oriented classes• java.io.OutputStream

• java.io.InputStream Character-oriented classes

• java.io.Writer

• java.io.Reader

Stream Trees

Reader Tree

Writer Tree

Byte oriented streams

Read chapter 3. See figure 3-1 p158. A few special streams:•ByteArrayInputStream lets you read an array of

byte as though it were an InputStream object

•SequenceInputStream/-OutputStream makes it possible to concatenate data from two or more InputStream objects into a single stream.

•PipedInputStream/-OutputStream implements one half of a “pipe”. Pipes are very useful when communicating between threads. See also chapter 11.

Character-oriented streams

Most Byte stream classes has a corresponding character stream class.• E.g. FileReader <-> FileInputStreamFileWriter <-> FileOutputStream

Figure 3-9 p177 Adapter classes for Byte oriented data

• E.g. InputStreamReader takes an InputStream object and converts it into a Reader -object.

Commonly Used Readers and Writers

FileReader: read text from files

BufferedReader: wraps around other Readers to get whole lines of text at a time

FileWriter: write files

PrintWriter: println method automatically adds newline and flushes

Common Uses of java.io

Writing to System.out (a PrintStream) Reading from System.in (an InputStream)

public static void main(String[] args) throws IOException {

System.out.println("What's your favorite ice-cream flavor?");

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

String userInput = br.readLine();

System.out.println(userInput + " isn't an ice-cream flavor!");

}

Writing a file The long way to get a writerFileOutputStream fos = new FileOutputStream("test1");OutputStreamWriter writer = new

OutputStreamWriter(fos);

The short way to get a writerFileWriter writer = new FileWriter(fileName);

After getting a writer writer.write("This is a test file.\n");writer.write("Hello World.\n");writer.write("Goodbye World.\n");writer.close();

Reading a file

The long way to get a readerFileInputStream fis = new FileInputStream(fileName);InputStreamReader reader = new InputStreamReader(fis);

The short way to get a readerFileReader reader = new FileReader(fileName);

After getting a readerBufferedReader br = new BufferedReader(reader);String s = br.readLine();while (s != null) { System.out.println(s); s = br.readLine();}reader.close();

java.nio

New:• Buffer management

• Channels

• File locking

• Memory mapping

java.nio: Buffers

“Buffer” a well-known concept, but is often home brewed by developers. They are now predefined and performance-optimized. See figure 3-10 p179.• ByteBuffer, CharBuffer, DoubleBuffer,

FloatBuffer, IntBuffer, LongBuffer, MappedByteBuffer, ShortBuffer

java.nio: Channels

A flexible concept that includes any open connection to a program entity.•DatagramChannel: when working with datagram

sockets (UDP).•SocketChannel: for use with TCP/IP sockets.•FileChannel: for reading, writing, mapping,

manipulating files.•Pipe.SinkChannel/-SourceChannel: for use

with the writable/readable end of a pipe.

• For more info: read code examples in the book or on the web. See figure 3-12 p183.

java.nio: Memory mapping

MappedByteBuffer• We may map the contents of a file into a

region of memory.

File locking necessary See figure 3-13 p186-187.

Collections

Lecture 5

Programming…

The foundation in programming is connecting well-known algorithms with well-known data structures.

Hopefully the resulting program is easy to maintain.

Three basic interfaces Collection

• The root interface of the collection framework. Set

• Does not allow duplicate elements• May be ordered.• E.g. HashSet, TreeSet, LinkedHashSet.

List• Defines standard behavior for ordered collections (i.e.

sequences).• May contain duplicates.• E.g. LinkedList, ArrayList, Vector, Stack.

Collection traversal You traverse a collection with an “Iterator”, which is a

cursor object. An object that guides you through the collection sequentially. • (hasNext(), next()…)• Very simple, and very safe.• You don’t instantiate an Iterator, by calling some

constructor. You kindly ask for it. (Remember the “Factory” design pattern?)•Iterator it = someCollection.iterator();while (it.hasNext()){ Item I = (Item) it.next();}

• Notice that the it object is declared to implement the Iterator interface. We don’t really know from which class the object is instantiated.

Maps

Does not extend the “Collection” interface Base class for (Key -> value) datatypes Unsorted Map implementations

• E.g. LinkedHashMap, HashMap (fastest),

Sorted implementations• TreeMap,

The new Collections framework vs the old legacy framework

The new framework of Collection based classes (since Java 2), adopts the Iterator interface.

Older legacy classes use the Enumeration interface, which is similar to Iterator.• Iterator <-> Enumeration:

• iterator() <-> elements()

• hasNext() <-> hasMoreElements()

• next() <-> nextElement()

Thread safety The legacy collections are somewhat thread safe (but not

alltogether) since the methods are fully synchronized (for good and for worse).

The Collections framework is not thread-safe for efficiency reasons.• If multiple threads may access it, the collection may be wrapped in

a readOnlyCollection or synchronizedCollection• Collections.synchronizedCollection(Collection c)

Collections.unmodifiableCollection(Collection c) Iterator is strongly promoted in favour of Enumeration.

• If the collection is modified by another Iterator, you get a ConcurrentModificationException. Iterator is fail-safe. Enumeration is not.

Collections class

Contains wrappers for thread safety. … also contains algorithms

• Sort, shuffle, search, max, min, reverse, rotate, swap.

Observer/Observable

Lecture 5

Observer/Observable examplepublic class IntrusionDetectorextends Observable { private Random rand = new Random(); public void connect(){ if( rand.nextBoolean() ){ // Intrång setChanged(); notifyObservers(); } }}public class SysAdmimplements Observer { private String name; public SysAdm( String name, IntrusionDetector id ) { this.name = name; id.addObserver(this); } public void update( Observable server, Object err ){ System.out.println( name + " förhindrar intrång"); }}

Parsing Strings

Lecture 5

StringTokenizer

Java.util Break a string into separate tokens

• A token may be a word or a symbol

• Usually a sequence of character not including a whitespace.

• Token delimiter can be specified, and is default set to whitespaces.

Regular expressions

A regular expression is a string of characters that describe a sequence of characters as a pattern.

The pattern is often a mixture of literals and characters with special meaning.• See documentation for “java.util.regex”

• See figure 4-31, 4-32, 4-33, 4-44 p307-308.

RegExp: Basic steps

1. Create a Pattern object that encapsulates an expression.

2. Acquire a CharSequence object that holds the sequence of characters to test.

3. Get a Matcher object4. Call a method on the Matcher Object.

Find(), matches(), group()

Only two classes in java.util.regex

Pattern Matcher

But regular expressions are rich and worthwhile looking into.

Remember

…to keep an extra eye on the backslashes• In literal Java strings the backslash is an escape

character. The literal string "\\" is a single backslash.

• In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash. This regular expression as a Java string, becomes "\\\\". That's right: 4 backslashes to match a single one.

Test and confirm your belief