Docsity

CS251 Intermediate Programming: Input/Output Techniques and Buffering - Prof. Douglas J. W, Study notes of Computer Science

Partial preview of the text

Download CS251 Intermediate Programming: Input/Output Techniques and Buffering - Prof. Douglas J. W and more Study notes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Input/Output #2 Lecture 15

The

InputStream

and

OutputStream

types provide a way to

encrypted-compressed-Combining stream types allows you to construct things like anencryption, compression, etc.There are concrete stream classes for doing data formatting,capabilities.complex stream “pipelines” can be built out of primitiveThey also allow streams to be plugged into each other so thatdevices (like disk sectors or net work packets).abstract away much of the “quirkiness” of individual I/O

fi

le stream or a formatted-encrypted-

sinks.are constructed using other streams as data sources or datadata source or data “sink” (destination). Other stream classes Some stream classes are constructed using a physical device asclasses for each combination of desired attributes.net work stream without having thousands of specialized

String processed. A data element that takes many bytes (like abinary data and remembering how much of the data you’veThe buffer commands require managing variable-size blocks ofunusable.program with, but their poor performance makes them nearlyThe single-byte read/write commands are convenient to

) could be split among multiple blocks and require

anJava has additional classes that can be “dropped in” in place ofmanual re-assembly.

InputStream

to use buffering in such a way as to improve

These classes are calledwithout having to manage the buffers yourself.the performance of the single-byte read/write methods

BufferedInputStream

and

BufferedOutputStream

BufferedOutputStream

is a subclass of

OutputStream

and

can works similarly to

BufferedInputStream

When you

fi

rst perform a

write(ch)

operation on a

BufferedOutputStream

, it adds the byte to an internal byte

When the internal bufferarray without accessing the underlying stream.

fi

lls up,

BufferedOutputStream

does a

write(buffer)

command to send the entire buffer to

the underlying stream in a single transaction.

BufferedInputStream bis = new BufferedInputStream(fis);FileInputStream fis = new FileInputStream(myFile);

fis

bis

input stream interface

File read skip close

bufferread skip close

Constructing a

BufferedInputStream

is done

by connecting it to an underlying

InputStream

BufferedInputStream (it doesn’t matter what kind).

will use the

InputStream

methods on the underlying

in order to satisfy future requests quickly.stream to get data in blocks, which it will store

Remember to Write a program that copies a file three different ways:

(^) close()

(^) any file you read and to

(^) flush()

, then

(^) close()

any file you write. 1. (^) Use the single-byte

(^) read()

(^) and

(^) write()

(^) methods to copy a file, using

unbuffered

(^) FileInputStream

(^) and

(^) FileOutputStream

. (Lab 9b)

(^) Use the block methods,

(^) read(buffer)

(^) and

(^) write(buffer, offset,

length)

(^) to copy the same file using manual buffers. (Lab 9b)

. Use the single-byte

(^) read()

(^) and

(^) write()

(^) methods with a

BufferedInputStream

(^) and

(^) BufferedOutputStream

(^) to copy the same file.

3a

. Keep in mind that you can get unpredictable results if you read or write 4. possibility of randomly rearranging the bytes in the file.both the file stream and the buffer stream alternatively. This has the (^) Use

(^) System.currentTimeMillis()

(^) to time the three implementations.

This method returns a

(^) long

(^) containing the current system clock in

the two copies. Note any performance differences.milliseconds. Measure the difference in time ticks between the start and end of