

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















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
(^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:
gives us
If we callaccess to the stream.
, one byte will be removed from the
stream and returned.
is
is
b
If we create a buffer array of 5 bytes:
and call
, up to
fi ve bytes will be moved from the
stream to the array.
is
buffer
The
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
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:
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.