Docsity

Input/Output in CS251 Intermediate Programming: Streams and Memory Layers - Prof. Douglas , Study notes of Computer Science

Partial preview of the text

Download Input/Output in CS251 Intermediate Programming: Streams and Memory Layers - Prof. Douglas and more Study notes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Input/Output #1^ Lecture 14

Input/Output

memories at the top and large/slow memories at the bottom.Computer memory is stacked in layers, with small/fastfor data storage.industrial control, etc.), the major use of I/O in computing isAlthough I/O is used for many things (user input, printing,input/output operations.processing units that communicate with each other through Historically, computing has been modeled as discrete

the same performance through caching.memory technology available, while still getting mostlycheaper than if they were entirely made of the fastest Combining memory types allows computers to be muchto a different data set.iterate over the same data set many times before movingother program repetition, computer programs oftenThis takes advantage of the fact that, due to loops and

Input/Output (3)

memory automatically.automatically moves data bet ween primary and secondary Virtual memory is an exception to this in that itmemory usually requires explicit program instructions.Moving information bet ween the various layers of secondarymemory is usually handled automatically by the system.Moving information bet ween the various layers of primarymemory.there is a discontinuity bet ween “primary” and “secondary” Because of the history of computer technology development,

Input/Output (4)

memory that require different treatment by soft ware: There are some distinctions bet ween primary and secondary

Input/Output (6)

There are t wo kinds of streams:

Output streams that write data to secondary storage.Input streams that read data from secondary storage.

of concrete stream classes for doingstream and output stream interfaces, along with a numberThere are abstract stream classes in Java to provide input

fi

le I/O, net work I/O, etc.

Getting a stream to a

fi

le requires a different setup from

The Java stream classes are in thestream, it behaves just like all other streams.getting a stream to a net work server, but once you have the

java.io

package.

source, such as a net work host orInput streams supply data, as a sequence of bytes, from a data

fi

le.

The details of the underlying storage (like the sectors of a

fi

le

be interpreted in either a signed or unsigned format.all integer data is signed. Java streams allow integer data towith data generated by non-Java code, it is not safe to assume Because I/O is a situation where Java code is likely to interactor the packets of a net work connection) are hidden.

Byte I/O in Java uses unsigned bytes stored in

int

s. The rightmost

eight bits of the

int

are used to hold the stream value. The other 24

bits are ignored. Stream data bytes have values bet ween

0

and

255

.

An

int

value of

-

is a code to indicate there is no more data in the

Signed bytes can still be stored in streams:stream.

Negative

byte

(^) value:

Positive

byte

value:

1 (^1) (^1) (^1) (^1) (^0) (^1)

1

46 23 6 1 (^8) (^4) (^2) 1

Sign

(^1) (^1) (^1) (^1) (^1) (^1)

1 (^1)

48361 2918 6904 8402 4201 215 652 (^1) (^1) (^1) 1 (^1) (^0) (^1) 1

46 23 6 1 (^8) (^4) (^2) 1

821

(^1) (^1) (^1) (^1) (^1) (^1)

1 (^1)

(^1) (^1) 1 (^1) (^1) (^1) (^1) 1

86723

63556

...

Sign

(^1) (^1) (^1) (^1) (^1) (^0) (^1)

1

(^0) (^0) (^0) (^0) (^0) 0 (^0) 0

48361 2918 6904 8402 4201 215 652 (^1) 1 (^1) (^1) (^1) (^0) (^1) 1

46 23 6 1 (^8) (^4) (^2) 1

821

0 (^0) (^0) (^0) (^0) (^0) (^0)

0

(^0) (^0) (^0) 0 (^0) (^0) (^0) 0

86723

63556

...

Sign

1 (^1) (^1) (^1) (^1) (^0) (^1)

1

46 23 6 1 (^8) (^4) (^2) 1

Sign

Original
byte
Cast to
int
Stream data
Read into
int
Cast to
byte

(^0) (^0) 0 (^0) (^0) (^1) (^0) 0

46 23 6 1 (^8) (^4) (^2) 1

Sign

(^0) 0 (^0) (^0) (^0) (^0) (^0) 0

48361 2918 6904 8402 4201 215 652 (^0) (^0) (^0) (^0) 0 (^1) (^0) 0

46 23 6 1 (^8) (^4) (^2) 1

821

(^0) 0 (^0) (^0) (^0) (^0) (^0) (^0) (^0) (^0) (^0) (^0)

0 (^0) (^0) 0

86723

63556

...

Sign

(^0) (^0) 0 (^0) (^0) (^1) (^0) 0

46 23 6 1 (^8) (^4) (^2) 1

Sign

(^0) 0 (^0) (^0) (^0) (^0) (^0) 0

48361 2918 6904 8402 4201 215 652 (^0) (^0) (^0) (^0) 0 (^1) (^0) 0

46 23 6 1 (^8) (^4) (^2) 1

821

(^0) 0 (^0) (^0) (^0) (^0) (^0) (^0) (^0) (^0) (^0) (^0)

0 (^0) (^0) 0

86723

63556

. ..

Sign

(^0) (^0) 0 (^0) (^0) (^1) (^0) 0

(sign extension)

(sign extension)

Initially, there are 10 bytes in the stream.Suppose we have the following stream:

InputStream is

gives us

If we callaccess to the stream.

int b = is.read()

, one byte will be removed from the

stream and returned.

is

is

b

If we create a buffer array of 5 bytes:

byte[] buffer = new byte[5];

and call

is.read(buffer)

, up to

fi ve bytes will be moved from the

stream to the array.

is

[0]
[1]
[2]
[3]
[4]

buffer

The

read()

method has the option of returning less data than you

keep the CPU busy while the hard drive is accessing data.you what it has while it reads more from the disk. This helps you tofrom the disk, but not enough to satisfy your request, Java will giveask for to improve performance. If some data has already been read

At this point, the stream is empty and further calls to

is.read()

will return

(which is clearly distinguishable from an unsigned byte

from the stream).

is

If you need to read a certain amount of data from a

fi le before

{ while (totalBytesRead < buffer.length)byte[] buffer = new byte[4096]; int totalBytesRead = 0; beginning processing, you could do this:

int bytesRead = is.read(buffer, totalBytesRead,

buffer.length - totalBytesRead);

if (bytesRead == -1)

break;

totalBytesRead += bytesRead;

Java also has built-in utilities to accomplish the same thing. }

1.

Open a

FileInputStream

to the message file.

2.

Use the

read()

method to read each byte from the file, stopping when the

3. end-of-file is reached.

Subtract

17

from each

byte

and cast the result to a

char

.

4.

Print each

char

to the console.

You can deal with Write a program that does the following things:

IOException

s either by writing a

try

/ catch

block or

by adding a

throws

clause to your

main()

method.