Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich



Similar documents
INPUT AND OUTPUT STREAMS

Today s Outline. Computer Communications. Java Communications uses Streams. Wrapping Streams. Stream Conventions 2/13/2016 CSE 132

Simple Java I/O. Streams

JAVA.UTIL.SCANNER CLASS

CS 1302 Ch 19, Binary I/O

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

D06 PROGRAMMING with JAVA

Visit us at

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Programming in Java

Decorator Pattern [GoF]

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

CS 106 Introduction to Computer Science I

Basics of Java Programming Input and the Scanner class

Introduction to Java

13 File Output and Input

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

JAVA - FILES AND I/O

WRITING DATA TO A BINARY FILE

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

FILE I/O IN JAVA. Prof. Chris Jermaine Prof. Scott Rixner

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

Creating a Simple, Multithreaded Chat System with Java

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

AVRO - SERIALIZATION

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

Programming Methods & Java Examples

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

All rights reserved. Copyright in this document is owned by Sun Microsystems, Inc.

Using Files as Input/Output in Java 5.0 Applications

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

What is an I/O Stream?

Files and input/output streams

Object-Oriented Programming in Java

Sending and Receiving Data via Bluetooth with an Android Device

Chapter 2: Elements of Java

Topic 11 Scanner object, conditional execution

Event-Driven Programming

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. AVRO Tutorial

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

ZEMRIS

Java Fundamental Classes Reference

Introduction to Java. CS 3: Computer Programming in Java

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

AP Computer Science File Input with Scanner

Java Interview Questions and Answers

Continuous Integration Part 2

6.1. Example: A Tip Calculator 6-1

Connecting to WebSphere ESB and WebSphere Process Server

Software Architectures

public static void main(string[] args) { System.out.println("hello, world"); } }

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Java Network. Slides prepared by : Farzana Rahman

Serializing Data with Protocol Buffers. Vinicius Vielmo Cogo Smalltalks, DI, FC/UL. February 12, 2014.

Mobila applikationer och trådlösa nät

Building a Multi-Threaded Web Server

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Course Intro Instructor Intro Java Intro, Continued

Decision-making Computer Science Lesson to Prepare for UIL Computer Science Contest

5 HDFS - Hadoop Distributed System

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Java (12 Weeks) Introduction to Java Programming Language

Comp 248 Introduction to Programming

A File System Design for the Aeolus Security Platform. Francis Peter McKee

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

When the transport layer tries to establish a connection with the server, it is blocked by the firewall. When this happens, the RMI transport layer

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Lecture 7: Class design for security

Character Translation Methods

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

B.Sc (Honours) - Software Development

Sample CSE8A midterm Multiple Choice (circle one)

Stream Classes and File I/O

Computer Programming I

CS 193A. Data files and storage

Some Scanner Class Methods

Illustration 1: Diagram of program function and data flow

Week 1: Review of Java Programming Basics

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Input / Output Framework java.io Framework

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Visualize Your Cloud Data Using the Graph Template Language

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

Bandwidth-limited systems. Chapter 8 Bandwidth-Limited Systems. Two flavors of limited bandwidth - 1. Outline of the chapter

Building Java Programs

Chapter 3. Input and output. 3.1 The System class

Interactive Programs and Graphics in Java

Java from a C perspective. Plan

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Web Development in Java

Objects for lexical analysis

HP AppPulse Mobile. Adding HP AppPulse Mobile to Your Android App

ECE 122. Engineering Problem Solving with Java

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Building Java Programs

Streaming Lossless Data Compression Algorithm (SLDC)

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

An Overview of Java. overview-1

Transcription:

Principles of Software Construction: Objects, Design, and Concurrency Design Case Study: Stream I/O Some answers Fall 2014 Charlie Garrod Jonathan Aldrich School of Computer Science 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis

A challenge for you Identify the design patterns in this lecture For each design pattern you recognize, write: The class name The design pattern If you have time: At least one design goal or principle achieved by the pattern in this context Hints: Use the slides online to review the lecture Design patterns include at least: Adapter Decorator Iterator Marker Interface Template Method toad 2

The stream abstraction A sequence of bytes May read 8 bits at a time, and close java.io.inputstream abstract int read(); int read(byte[] b); May write, flush and close java.io.outputstream void flush(); abstract void write(int b); void write(byte[] b); toad 3

The stream abstraction A sequence of bytes May read 8 bits at a time, and close java.io.inputstream abstract int read(); int read(byte[] b); Template Method Iterator? May write, flush and close java.io.outputstream void flush(); abstract void write(int b); void write(byte[] b); Template Method toad 4

The reader/writer abstraction A sequence of characters in some encoding May read one character at a time and close java.io.reader abstract int read(); int read(char[] c); May write, flush and close java.io.writer void flush(); abstract void write(int c); void write(char[] b); toad 5

The reader/writer abstraction A sequence of characters in some encoding May read one character at a time and close java.io.reader abstract int read(); int read(char[] c); May write, flush and close java.io.writer void flush(); abstract void write(int c); void write(char[] b); Template Method Iterator? Template Method toad 6

Implementing streams java.io.fileinputstream Reads from files, byte by byte java.io.bytearrayinputstream Provides a stream interface for a byte[] Many APIs provide streams for network connections, database connections, e.g., java.lang.system.in, Socket.getInputStream(), Socket.getOutputStream(), toad 7

Implementing streams java.io.fileinputstream Reads from files, byte by byte java.io.bytearrayinputstream Provides a stream interface for a byte[] Adapter Many APIs provide streams for network connections, database connections, e.g., java.lang.system.in, Socket.getInputStream(), Socket.getOutputStream(), toad 8

Implementing readers/writers java.io.inputstreamreader Provides a Reader interface for any InputStream, adding additional functionality for the character encoding Read characters from files/the network using corresponding streams java.io.chararrayreader Provides a Reader interface for a char[] Some convenience classes: FileReader, StringReader, toad 9

Implementing readers/writers java.io.inputstreamreader Provides a Reader interface for any InputStream, adding additional functionality for the character encoding Read characters from files/the network using corresponding streams java.io.chararrayreader Provides a Reader interface for a char[] Adapter Adapter Some convenience classes: FileReader, StringReader, Adapter toad 10

A better design to add functionality to streams «interface» OutputStream +write() +close() +flush() 1 - out FileOutputStream - file ByteArrayOutputStream - buffer FilterOutputStream delegating all calls to other output stream +write() +close() +flush() +write() +write() +close() +flush() 1 GZipOutputStream AESEncryptionStream DataOutputStream +compress() +encrypt() +writeint() +writestring() +writefloat() toad 11

A better design to add functionality to streams «interface» OutputStream +write() +close() +flush() 1 - out Decorator FileOutputStream - file ByteArrayOutputStream - buffer FilterOutputStream delegating all calls to other output stream +write() +close() +flush() +write() +write() +close() +flush() 1 GZipOutputStream AESEncryptionStream DataOutputStream +compress() +encrypt() +writeint() +writestring() +writefloat() toad 12

To read and write arbitrary objects Your object must implement the java.io.serializable interface Methods: none If all of your data fields are themselves Serializable, Java can automatically serialize your class If not, will get runtime NotSerializableException! Can customize serialization by overriding special methods See QABean.java and FileObjectExample.java toad 13

To read and write arbitrary objects Your object must implement the java.io.serializable interface Methods: none Marker Interface If all of your data fields are themselves Serializable, Java can automatically serialize your class If not, will get runtime NotSerializableException! Can customize serialization by overriding special methods See QABean.java and FileObjectExample.java toad 14

The java.util.scanner Provides convenient methods for reading from a stream java.util.scanner: Scanner(InputStream source); Scanner(File source); boolean hasnextint(); int nextint(); boolean hasnextdouble(); double nextdouble(); boolean hasnextline(); String nextline(); boolean hasnext(pattern p); String next(pattern p); toad 15

The java.util.scanner Provides convenient methods for reading from a stream java.util.scanner: Scanner(InputStream source); Scanner(File source); boolean hasnextint(); int nextint(); boolean hasnextdouble(); double nextdouble(); boolean hasnextline(); String nextline(); boolean hasnext(pattern p); String next(pattern p); Iterator toad 16