Android Question A question about job.GetInputStream

jkhazraji

Active Member
Licensed User
Longtime User
We usually use job.GetInputStream in whole, e.g. to save it to a file as in the following code:

B4X:
            Dim OutStream As OutputStream
            OutStream = File.OpenOutput(File.DirInternal, dlFileName, False) 
            File.Copy2(Job.GetInputStream,OutStream) ' save the file
            OutStream.Close
but what about saving only a part of it(e.g. the last 3000 bytes of this 'inputstream')?
Anybody helps??😇
 

drgottjr

Expert
Licensed User
Longtime User
"stream" says it all. how long is a stream anyway? i would just like the last 3000 liters of water from that stream that runs by my house, please. where do the last 3000 liters begin?

there is a java method relating to the length of an inputstream, but java itself says the call is not reliable. if your job depends on getting this right, you might not want to rely on it.

you have to consume the inputstream, and then you can step 3000 bytes from the end. depending on the length of the stream, you might be able to do it in memory. you'll probably have to save it to a file, as you've been doing. you can then access 3000 bytes from the end of the file with random access.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
'Returns the number of bytes skipped
Sub SkipNBytes (Input As InputStream, N As Int) As Int
    Dim buffer(Min(N, 8000)) As Byte
    Dim count As Int
    Do While count < N
        Dim read As Int = Input.ReadBytes(buffer, 0, Min(buffer.Length, N - count))
        If read = -1 Then Return count
        count = count + read
    Loop
    Return count
End Sub

This code will handle large files.
B4X:
Dim in As InputStream = Job.GetInputStream
Dim TotalSize As Int = SkipNBytes(in, 0x7fffffff)
in.Close
in = Job.GetInputStream
SkipNBytes(in, TotalSize - 3000) 'assuming that it is large enough
File.Copy2(in, OutStream)
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
"stream" says it all. how long is a stream anyway? i would just like the last 3000 liters of water from that stream that runs by my house, please. where do the last 3000 liters begin?

there is a java method relating to the length of an inputstream, but java itself says the call is not reliable. if your job depends on getting this right, you might not want to rely on it.

you have to consume the inputstream, and then you can step 3000 bytes from the end. depending on the length of the stream, you might be able to do it in memory. you'll probably have to save it to a file, as you've been doing. you can then access 3000 bytes from the end of the file with random access.
Thanks for your well said reply. Well, Let's say I saved the whole stream to a file then how could I get rid of a 1K header to get the last 'sweetest' 30K flood of the stream? Is there a way to trim it?
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
Everything you need is in my answer above.
@Erel.. Sir, the code skips the last part and returns the first one which is the other way around. i.e. if the file is 4K long the code returns the first 1K not the last 3K. I want to get rid of the header of the downloaded file and keep the bulk following it to save it to a file. Thanks for your patience.:)
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
It took me few minutes to realize that if I can skip the last part then I could skip the first.
 
Upvote 0
Top