12
http:// improvejava.blogspot.in/ 1 ByteArrayInputStream class

Byte arrayinputstream.50

  • View
    264

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 1

ByteArrayInputStream class

Page 2: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 2

Objectives

On completion of this period, you would be able to learn• Other I/O stream classes• ByteArrayInputStream • ByteArrayOutputStream• BufferedInputStream • BufferedOutputStream

Page 3: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 3

Recap

In the last class, you have studied about the types Stream Class

• Byte Streams are two types1. InputStream class2. OutputStream class

• FileInputStream and FileOutputStream are the subclasses of byte stream

• Let us explore some more stream classes

Page 4: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 4

ByteArrayInputStream class

• Data stored in memory is handled by ByteArrayInputStream

• This class is used to read bytes from a memory• Objects of this class are used to create input stream

with memory buffers as data source• Constructors

• ByteArrayInputStream(byte array[ ])• ByteArrayInputStream(byte array[ ], int start, int

numBytes)

Page 5: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 5

ByteArrayOutputStream class

• Data stored in memory is handled by ByteArrayOutputStream

• This class is used to write bytes into memory• Objects of this class are used to create output stream

with memory buffers as data sink• Constructors

• ByteArrayOutputStream( )• ByteArrayOutputStream(int numBytes)• In the first form, a buffer of 32 bytes is created.• In the second, a buffer is created with a size equal to that

specified by numBytes

Page 6: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 6

Filtered Byte Stream class

• Byte streams access the data in byte form• Raw bytes can not be used for any useful purpose• For converting bytes to useful forms such as char,

string, int, etc filtered streams are used• Streams that can take other stream as arguments are

called filtered streams• FilterInputStream

• BufferedInputStream is a subclass of it• FilterOutputStream

• BufferedOutputStream is a subclass of it In many practical situations only filtered streams are

useful

Page 7: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 7

BufferedInputStream class

• This is a subclass of FilterInpuStream• This class is used to increase the efficiency of reading

bytes from an input stream• Fetching a byte and sending it to the destination one

after the another is a time consuming process • Instead, bytes can be read and stored in buffer

memory before sending it to the destination• Once the buffer is full, the bytes can be sent to the

destination

Page 8: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 8

BufferedOutputStream class

• This class is a subclass of FilterOutputStream• This class is used to send buffered bytes to an

underlying output stream in an efficient wayConstructors

• BufferedOutputStream(OutputStream outputStream)• BufferedOutputStream(OutputStream outputStream, int bufSize)

• The first form creates a buffered stream using a buffer of 512 bytes. In the second form,

• the size of the buffer is passed in bufSize.

Page 9: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 9

Summary

• Data stored in memory is handled by ByteArrayInputStream and is used to read bytes from a memory

• ByteArrayOutputStream is used to write bytes into memory

• FilteredStreams are used for converting bytes to useful forms such as char, string, int, etc

Page 10: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 10

Quiz

1. ---- is used to read bytes from a memory

A. ByteArrayOutputStreamB. ByteArrayInputStreamC. ReaderD. None

Page 11: Byte arrayinputstream.50

2. --- is used to write bytes into memory

A. ByteArrayInputStreamB. ByteArrayOutputStreamC. WriterD. None

http://improvejava.blogspot.in/ 11

Quiz

Page 12: Byte arrayinputstream.50

http://improvejava.blogspot.in/ 12

Frequently Asked Questions

1. Write about ByteArrayInput and ByteArrayOutput streams

2. Write about FilteredByte Streams