CS51 - Fall 2009 - Lecture 35
Exercise 19.7.3
Review of Readers and Writers
- Input
- FileReader
- BufferedReader
- Output
- FileWriter
- PrintWriter
- Readers and writers deal with characters
Streams
- A stream is a sequence of data
- Streams deal with bytes (instead of characters)
- What is a byte?
- 8 bits
- What is a bit?
- a 0 or a 1
- A byte is a common unit of storage in computer (kilobyte (KB), megabyte (MB), ...)
- Are a way for a program in java to communicate to external components (i.e. outside of processor/memory)
- What external components might we want to talk to?
- files (disk drives)
- internet/network (i.e. other computers)
- memory
- devices (e.g. cdrom, disk drive, ipod)
- Streams are directional
- input streams bring data into the program (ancestor of class InputStream)
- output streams send data out from the program (ancestor of class OutputStream)
- Different types of streams depending on the data that you want to read or write. What type of data might we want to read or write?
- built in data types
- DataInputStream(InputStream in)
- readInt()
- readDouble()
- ...
- DataOutputStream(OutputStream out)
- writeInt(int i)
- writeDouble(double d)
- ...
- Objects
- ObjectInputStream(InputStream in)
- ObjectOutputStream(OutputStream out)
- FileInputStream and FileOutputStream vs. FileReader and FileWriter
- FileReader and FileWriter can be thought of as character streams
- what is the difference between using a DataOutputStream to print a double vs. a PrintWriter
- Buffered streams
- Most streams are unbuffered, that is when you read or write a character, it reads or writes a character
- What's the problems with this?
- To alleviate this, we can do buffered I/O
- BufferedReader(Reader in)
- InputStreamReader is a reader
- InputStreamReader(InputStream in)
- BufferedWriter(Writer out)
- OutputStreamWriter is a writer
- OutputStreamWrtier(OutputStream out)
- Note also: InputStreamReader and OutputStreamReader allow us to go from our FileReader/FileWriter classes to Streams
show
DrawingPanelNetwork demo
- save and load file functionality
- save some file and look at the data as a text file
- can see some data we might, but it is a "binary" file... it has bytes written representing the data
- can also transmit data over the network
- What is a server and a client?
- servers and clients communicate over the network
- the main difference is that servers are started first and wait for clients to collect
- also, there is usually a many to one relationship, with many clients for a single client, for example:
- web (http) servers
- mail (smtp) servers
- ftp servers
- file servers (e.g. nfs)
look at
DrawingPanelNetwork code
- top code is basically the
DrawingPanel code
- To read and write this data, what type of Stream will we need?
- writeToStream
- Takes as input an OutputStream (similar to what we did for SantasList)
- creates a new ObjectOutputStream
- what do you think the flush() method does?
- don't actually need it here since close() will flush the data
- don't forget to close()
- readToStream
- Similarly, takes as input an InputStream
- creates a new ObjectInputStream
- note that we need to type cast it
- Why do you think we wrote these methods to take as input a Stream?
- we can use the same methods to do the for our network transmitting
- Sockets
- a socket is a framework for bidirectional communication
- sockets are specified by an IP address and a port
- every computer has an IP address, think of it like a phone number
- consists of 4 numbers 0 to 255 xxx.xxx.xxx.xxx
- or, you can think of it as 4 bytes
- think of the port, like an extension, it uniquely identifies what program/process on a computer another program is talking to
- the server only need to specify a port, which is the port it is listening on
- "localhost", is a keyword for specifying the current computer
- beAServer()
- ServerSocket is used to wait for a connection from a client
- note that we only need to specify the port
- drawingSocket.accept()
- another blocking operation
- waits for the client to connect and gives us the socket for the connection
- socket.getOutputStream()
- uses writeToStream to transmit the data
- beAClient()
- creates a new socket using the ip and the port specified
- socket.getInputStream()
- can leverage readToStream