In this and VIVIR see how we can process bite or the interface that is files containing non-terrestrial 2
data like images that enable processing files is probably one of the most common activities we will 3
also see how we can do this in an efficient way. 4
And for that we will look at a picnic called buffering and in the process we will also look at a design 5
pattern called decorator pattern. 6
In fact decorator pattern is very heavily used within the I IO package at the end of the lesson. 7
We will also do a shot them all of what we learned. 8
It's just like building the lesson. 9
But by the end of it they will have a solid understanding of how we can process byte oriented files 10
efficiently. 11
We already know that we can use file input stream for reading data from files. 12
Similarly there is also a fine output stream for writing that or twice as we saw earlier. 13
These classes extend input stream an output stream process since read and write metrics in input and 14
output stream classes are abstract. 15
These classes provide an implementation for those matters. 16
We all need to look at how we can create a file input stream object. 17
And here is how we can create a file output stream. 18
The input parameter is simply the filename. 19
There are a couple of other overloaded constrictors but this one I think is the most commonly used. 20
Not that this constructor would create a new file if the file does not exist but a file does exist then 21
it could simply override the fine but with file input stream a new file is not created. 22
If it does not exist the constructor will simply throw the file off one exception and that makes sense 23
as there is no point creating an empty file and read nothing from it. 24
And here are some reasons for throwing a file not one exception plus one specific to file input stream 25
and we just discussed it. 26
Second one is specific to file output string. 27
An exception will be generated if the file for some reason cannot be created for writing. 28
Third and Fourth results apply to both file input as well as file output streams. 29
Third is when the file is actually the directory first is when the file exists. 30
But for some reason the file cannot be opened for reading or writing. 31
We saw that the BBC could read what methods were used for reading or writing single backs even when 32
we were using other variations of related matters where a byte array is used. 33
They were internally still relying on the basic redirect matters but the problem is reading or writing 34
single bytes in this way is way too expensive as each method invocation triggers or disk access. 35
As an analogy it is similar to shopping without a car that is each item is carried separately to the 36
checkout counter. 37
So what we need here is a shopping cart and in programming terms that would correspond to something 38
called buffering with buffering Eury the block of bytes into a memory buffer. 39
And then you read from that memory buffer which is much more faster than reading from the disk. 40
Similarly you would also write a block of bytes into the memory buffer and then flush all of the data 41
. 42
That is a once in a single operation. 43
So once again we are not hitting the disk for each byte that we want to write and group offering. 44
There are two specialized classes called buffer input stream and buffer output string and these classes 45
are subclasses off filter input stream unfiltered Puchi which in turn subclass input stream an output 46
stream process and a memory buffer that we are talking about is nothing but simple binary so both buffer 47
input stream and buffer output stream have this binary and the default buffer sizes are 8000 bytes. 48
But if needed we can set the buffer size to a different value by invoking one of the constructors. 49
But the thing is buffered input and buffer output stream classes do not work independently. 50
They work with other streams like file and put on file output streams that is buffered streams only 51
provide buffering as the core functionality and do not take the responsibility of dealing directly with 52
files on the disk. 53
So to provide buffering a buffer stream will be chain are connected to another string. 54
For example here a buffered input stream is chained to a file input stream which does the actual reading 55
from the file. 56
The blue Donard box you are can be visualized as the buffer on in terms of quote This is how it would 57
look. 58
The buffered input stream has a constructor which takes an input stream object as input. 59
And that's how the two classes have changed since the input is an input stream. 60
We can pass any input stream object but as we can specify an input stream object or any other instance 61
of the subclass of input string class. 62
Not that it's got to be a subclass of the input stream class as input stream class is abstract and we 63
cannot instantiate it. 64
No this kind of chaining is an example of a good object oriented design. 65
As in sort of maintaining a single huge class we have several smaller classes with each class doing 66
one thing and being able to extend the functionality of one class by changing it. 67
And in that class for example here we are extending the functionality of filing stream class by creating 68
that buffered input stream class. 69
Not this design is nothing but decorator design pattern. 70
He had buffered input stream is called as a decorator as it is create the file input stream object with 71
additional buffer functionality. 72
The decorator is also referred to as a proper class as it is dropping another object and a very important 73
design principle called the Open Closed principle serves as motivation for decorator's bargain and according 74
to this principle classes should be open for extension but glose for modification. 75
And that is what is happening here. 76
To add new functionality we are simply changing classes rather than modifying them does highlighting 77
the possibility of introducing any new bugs but the question is how are these classes open for extension 78
and for that to happen. 79
Boot decorator as well as the object that is being decorated must have a common supertype. 80
In our example both buffered input stream unfiled input stream input stream class as a common supertype 81
. 82
This way we can pass it on the Baho input stream object in place of a file input stream object and the 83
read method can be in word as if it was word on the file input stream object and the matter of the buffer 84
input stream object will give us the buffering benefit. 85
Internally it accesses the read method of the file input stream object and will buffer the bytes read 86
on. 87
Here is the precise logic. 88
So this is the read method in the buffer input stream class and it is basically an implementation of 89
the abstract read method in the input stream class. 90
So here is the buffer has some unread bytes than the first underbite is written. 91
Otherwise it means that either the buffer is empty or all its bytes have already been read in either 92
is willing to fill the buffer with fresh data. 93
And for that the read method of the underlying stream is invoked not the underlying stream is simply 94
a chain string that is fine input stream in Arties. 95
So by having a common supertype variable to extend the functionality of the file input stream class 96
by providing buffering as an additional capability. 97
Also note that the method declaration here has the keyword synchronized which is a multi-threading feature 98
that is used to add white data corruption. 99
No because the matter is synchronized GBM has to ensure that only one thread is able to access the matter 100
at any given point of time so some additional checks are performed when such kind of matters are in 101
work thus causing slowness in execution speed and due to this. 102
The second Bushnell of freedom can be booked their bikes are read in groups and here is a second version 103
. 104
Although this method is also synchronized since we are reading groups of bytes rather than single bytes 105
there'll be fewer muttering locations. 106
This method would read length number of bytes from the buffer into the input array starting at index 107
position offset undeclared instant number of bytes read or minus one. 108
If nostri Mr. minus one. 109
Let's also look at how the matter is implemented. 110
Most of the buffer has the requested amount of data then the data would be copied from the buffer into 111
the input binary. 112
But if the buffer does not have the entire data but has only partial data then the partial data is first 113
copied into the input and next the buffer is filled by fetching data from the underlying stream. 114
How does the read matter of the underlying stream is in work and the buffer itself is pozières input 115
to the matter handler once the buffer is full. 116
The remaining data is copied from the buffer into the input it know the Perkis is then the buffer does 117
not have any They died on. 118
And this gives If the buffer size is greater than the input array size then buffer is filled with fresh 119
data unlearnt number of bytes is copied into the input area. 120
But if that's not the case that is input array size is greater than or equal to the buffer size then 121
the data is copied directly from the underlying stream. 122
And today it. 123
So data is not copied into the buffer as it would add no value. 124
So he had that read method of the underlings I mean isn't good. 125
The input byte Daria's argument. 126
In fact in this case it doesn't make sense to use buffered input stream in the first place as in working 127
code could have directly access the read method of the underlying stream. 128
So it is just an extra word. 129
So there's no internal logic. 130
I would encourage you to look at the source code of this method just to get a feel of how this logic 131
is actually implemented. 132
Now here is the right. 133
Matter of the buffered output stream and the correct input data to the buffer. 134
But if buffer is full then data in the buffer is forced through us to the underlying string then the 135
buffer is filled with the input data. 136
And here is the second version of the right method. 137
It simply writes the input or the contents into the buffer. 138
If buffer does not have enough space then the buffer is face flushed and then the array contents are 139
copied into the buffer not flushing the right method of the underlying stream is used. 140
And we simply pass a buffer as the argument here count would be the index position of the last bite 141
. 142
That does not yet returned to the underlying stream but if input array size is greater than or equal 143
to the buffer size then buffer is fast flossed unlike in the case of read. 144
The recordings are written directly to the underlying stream. 145
Notice how the right method of the underlying stream is in would get different arguments for both these 146
actions. 147
So in this case using buffer output stream is of no value unright method in the final output stream 148
could have been invoked directly. 149
So if you want to read or write some large amount of data in a single discusses in which case you don't 150
need any buffering then you can directly access the read or white matter of the appropriate file stream 151
class by passing the byte address in a book. 152
But if you want to read data in smaller chunks which is generally the case then buffering is the way 153
to go. 154
So those are the right metrics. 155
Also like read amateur's due to synchronization usually in web this version of the right method if you 156
want better speed Novantrone method is involved on the decorator then it would internally invoke the 157
clothes method of the underlying decorator stream not have the decorator is a buffer to put stream then 158
invoke invoking close method would first flush all the bar contents and then close off the underlying 159
stream isn't war. 160
So for flushing internally Frush matter is used. 161
But you don't have to invoke this method explicitly as both Boatright as well as disclosed method. 162
Flush the buffer contents automatically. 163
Also just note that there is nothing like closing a decorator's dream as it is simply a class providing 164
some functionality like buffer. 165
That is it is not really dealing with some physical device like in the case of a file stream. 166
So decorator's simply invoke the close matter of the underlying string not the underlying stream is 167
also articulator then it would Indonesia with the Close method on the object that it is decorating situating 168
need not be restricted to just two classes. 169
It can be more than two also. 170
So that's about it. 171
Just to summarize we use buffering technique to perform fast streets or faster writes internally a byte 172
is used between the buffer data and the chaining process that we discuss is nothing but the decorator 173
bargain. 174
Now let's go ahead and do a very good bit more of what we just learned from for this demo. 175
I have created a new package called com doc sematic square that I will and I have also created a new 176
class within this package called I am all for this more we are going to test two things. 177
One is file processing without using buffering. 178
And for this we have this matter. 179
And the second one is file processing with buffering. 180
OK. 181
And that's the second matter tested later. 182
So the main thing is we are going to test both read and write operations and we're going to do a copy 183
operation which means that we are going to read file a bite from a file and we're going to write those 184
bytes to generate new file. 185
OK so that's the copy operation. 186
So let's just go ahead and look at that. 187
Matter. 188
So the input file name is what I learned or JPEG and output file name rather than hyphen or Rajib So 189
it's a gyp quite honored set on fire. 190
And here is the matter. 191
Here we are creating an instance of this class called file and we are doing the act in order to get 192
the size of the fight. 193
But let's not worry about this file class. 194
We will look at it later. 195
So let's look at the read and write operations here. 196
Here is a try catch block. 197
So we are using the new try with resources syntax which has been introduced in Java 7. 198
OK. 199
And this does not require And finally block on here within the parenthesis we are creating an instance 200
of the file input stream and input that constructor is the filename of the input file. 201
And here we are creating an instance off file output stream and we are passing the output file in here 202
. 203
Onboard these explanations should be separated by a semicolon. 204
Here you are measuring the start time. 205
And here we are actually doing the in the while loop we are actually doing the both the read and write 206
operation. 207
So here in the parentheses we are reading the input file. 208
Ok so we are invoking the read method on the file input string. 209
So in this four input stream outhe's for output stream. 210
So that's the normal the standard conventions which we use for these streams. 211
So we are reading it under read motard we know it does a byte OK. 212
Every location read answer byte. 213
And if it reaches the end of the stream it returns a minus one. 214
So as long as it does not it has a doctorate and minus one it means that it is able to read some bytes 215
and the bytes read is stored in this variable called byte read which is of type. 216
OK so now within the within the while loop the byte that has been read is being written into the output 217
string. 218
Ok so we are invoking the right method and we are passing the byte that has been read. 219
And here you are getting the elapsed time and we are simply printing the elapsed time. 220
So let's just go ahead and run this method. 221
As you can see the file size is around 5 and B. 222
So how are some of the video here. 223
Because it doesn't take and I don't quantify seconds. 224
So now let's go ahead and test the second one which uses buffering here and here is the matter. 225
So within the try catch block this time we are using a buffered input stream and it has changed a bit 226
. 227
I need distrained with the file and put string on the file input stream has the input file name as a 228
parameter. 229
So the only addition here is we are changing with buffering put on buffered output streams can we are 230
still using the private resource syntax and we're going to use the second versions of the both the read 231
and write methods which read and write in groups of bytes. 232
OK so we are we have to pass a byte array to them. 233
OK I'm on the byte resizes photos and bytes and we know that the buffered input and output stream also 234
use a local buffer local array but the before sites for that area is around 8 toes and bytes. 235
So here we are using photos and bytes. 236
So we will read into this Arri into this binary and we will just pass that binary to the right method 237
in order to generate the output file. 238
So here we are creating the local byte array and within the Y loop we are first invoking the read mattered 239
and we are passing this byte array. 240
So photos and bytes will be read into this array. 241
And as long as as long as this method is not returning minus one we are going to execute the while matter 242
and within the sorted with a widebody. 243
And here we are invoking the right method on the output stream on the buffer output stream unbe are 244
passing the Bible. 245
So the contents of the Bible will be returned to the buffer output stream and we are though the offset 246
here is zero and the number of bytes that we want to write as this one which has been read the number 247
of bytes that has been read. 248
So that is returned by the read method. 249
So that's about it. 250
And let's just run this. 251
Now it's just taking 21 milliseconds you see. 252
So that's the difference here because we are using buffered inputs buffer streams which has taken only 253
21 milliseconds whereas for when we did not use the buffering technique it has taken around 25 seconds 254
. 255
OK. 256
And within the buffer or within the buffering technique we are using the second version of the read 257
and write matters because if we had we used only the read method it could how it would be a little bit 258
cooler because all the read metrics on the right methods are synchronized. 259
So we are reducing the number of matter in locations by reading and writing in groups. 260
So we are using the second version of the Read matter. 261
So that's about it. 262
This new class is available in the resources section and you can just take a look at it and play around 263
with it. 264
Thank you. 265
And happy courting