in the last video we started tidying up
our download XML function to write it
more how you do it in Kotlin rather than
converted Java now that was a useful
exercise and seeing things like the
catch blocks being changed will help you
to write better Kotlin code now the
catch block change didn't save us a lot
of code but when we replaced our while
loop with a single line in Kotlin well
that saved quite a bit of code
now unless code means fewer errors so
that's definitely an improvement so as
promised i'm now going to replace the
download XML function with just one line
of Kotlin so the first thing to do is to
deal to delete all the code we've got in
here in there I'm going to delete
everything out of the download XML
function look so now at the moment we're
getting an error because the function is
not returning anything so we better tell
it what to return so let's do return URL
URL path dot retakes and that's it let's
make sure it works before we look at
what's actually going on you can see the
code is just literally one line so let's
start bring up the logcat we'll clear it
and we'll actually run it and then if
you need to see the emulator screen we
just need to see the log count output
and you can see that everything's worked
as it's done before we're still seeing
the XML downloaded code and the various
uncreate doing background so on and so
forth that's all working so basically
the oncreate starting and finishing just
like before then the background task
executes with the URL that we provided
now there's no logging for download XML
because there's nothing really to log it
obviously works though because we get
we're getting the XML logged by the on
post execute function now I will say
that there was nothing wrong with our
previous code and you'll see in a moment
why you might need to use code like that
so the steps we went through in the last
video were important I didn't just get
you to type in all that code just so you
can see how impressive this is although
I will say it is pretty impressive but
let's actually have a look at what's
going on
how Commons let us replace all that code
with just a single line now Cullen
provides something called extension
functions which are a great way to add
extra functions to existing classes
now here the Kotlin library's added read
text as an extension function to the URL
class no URL is a class from the
standard Java library in the package
java.net now you can check that by
holding down ctrl or command on URL and
hovering over URL as I'm doing there now
I won't click on it because I don't yet
have the API 26 source code but the
tooltip tells us all we need to know and
you can see where it's coming from
Java declarations java.net so URL is a
Java class that's been around for a long
time now the colon developers wanted to
give it a read text method but they
can't go messing about with standard
Java libraries
so instead Kotlin allows extension
functions to be attached to existing
classes and call us so they really
belong to that class so that's what read
text is an extension function now we can
have a look at it by ctrl or command
clicking read text let's do that this
takes us to the Kotlin readwrite module
where Kotlin added all sorts of
functions to standard Java classes in
fact we've been here before because if
you scroll a short way up we should be
able to see the reader dot read text
there's reader copy to and reader dot
read text we looked at those earlier
alright so back down to what URL don't
read text
and the docstring there says reads the
entire content of this URL as a string
and that's just what our previous code
was doing we just did it the long way so
read text is a function that returns a
string so it works by calling the read
bytes function then returning the result
of Dennis a string by calling the two
string function now read bytes appears
at the end of the file
you can see that and then read bots it
calls open stream and uses that to read
bytes from it now this is very similar
to what we've seen so you probably won't
be surprised by what we see when I
control click on it or command or
control click read bytes we're gonna
come over here the oit don't read bias
and click on read bytes over there we've
been here as well so her read bytes uses
that copy to function to read from the
stream and copy to use as a while loop
there it is just above
as you can see on the screen there at
the top that's just above the read bytes
extension function now before we go back
to our e-text in the readwrite Kotlin
code notice are covered that it is the
caller's responsibility to close this
dream now this may worry you but it
needn't so if we close our iOS dreams
Katie now and we're back at readwrite
Katie the use function actually takes
care of closing down the resources as we
saw earlier so that's handled for us now
the last thing that I want to mention
here is that the second line for the doc
string for read text this method is not
recommended on huge files now that's why
our previous code may be useful so
getting it all working and Kotlin wasn't
a wasted exercise you know if you're
dealing with a lot of data it's
important to remember that read text
will read the whole lot into a string in
memory
now what's meant by huge depends on the
environment you might think nothing of
reading a gigabyte on a desktop computer
with 32 gigabytes of RAM but we're
writing apps for Android devices though
so even a couple of megabytes can be
considered huge now the Downloads that
we'll be dealing with for this feat are
just a few kilobytes but if you
experiment with different feeds
definitely keep an eye on their size now
as you browse through these Kotlin
libraries to get a feel for what's
provided make sure you also pay
attention to any comments like this one
Kotlin can make coding a lot easier but
it's up to you to only use features when
they are appropriate with great power
comes great responsibility
now before I finish I'm going to check
the import section of main activity and
it claims down read/write cought at the
moment the Kotlin plugin isn't very good
at removing unused import so we may
still have the Java net and Java dot IO
imports for the code we deleted now in
my case I haven't because I've optimized
imports to be automatically updated or
the unused imports will be removed
automatically but if you've got any
unused imports here now there would be a
good time to get rid of them and what
you should end up is a little green dot
will end up with is a little green tick
in the right-hand corner we need to when
you're when you're saying that you know
that you're ready to continue all right
so let's finish the video here and in
the next one we're gonna make a start on
passing out the information we want
from the XML that we've downloaded see
you in the next video