The AsyncStreams object allows you to read from an InputStream and write to an OutputStream in the background without blocking the main thread. See the AsyncStreams Tutorial. NewData event is raised when new data is available. Error event is raised when an error was encountered. You should check LastException to find the error. Terminated event is raised when the other side has terminated the connection. NewStream event is only raised in prefix mode when the other side sends a stream with WriteStream. This event is raised after the complete stream was received successfully. The event includes the saved stream folder and name. Note that the file name is an arbitrary string.
Events:
NewData (Buffer() As Byte) Error Terminated NewStream (Dir As String, FileName As String)
CompressedStreams object allows you to compress and decompress data using gzip or zlib compression methods. There are two options for working with CompressedStreams: Wrapping another stream by calling WrapInputStream or WrapOutputStream. Compressing or decompressing the data in memory. The following example demonstrates the usage of this object: SubGlobals
EndSub
SubActivity_Create(FirstTimeAsBoolean)
DimsbAsStringBuilder sb.Initialize 'Concatenation operations are much faster with StringBuilder than with String. Fori = 1To10000 sb.Append("Playing with compressed streams.").Append(CRLF)
Next DimoutAsOutputStream DimsAsString DimcompressAsCompressedStreams s = sb.ToString 'Write the string without compressing it (we could have used File.WriteString instead). out = File.OpenOutput(File.DirTemp, "test.txt", False)
WriteStringToStream(out, s)
'Write the string with gzip compression. out = File.OpenOutput(File.DirTemp, "test.gz", False)
out = compress.WrapOutputStream(out, "gzip")
WriteStringToStream(out, s)
'Write the string with zlib compression out = File.OpenOutput(File.DirTemp, "test.zlib", False)
out = compress.WrapOutputStream(out, "zlib")
WriteStringToStream(out, s)
'Read data from a compressed file DiminAsInputStream in = File.OpenInput(File.DirTemp, "test.zlib")
in = compress.WrapInputStream(in, "zlib")
DimreaderAsTextReader reader.Initialize(in)
DimlineAsString line = reader.ReadLine Msgbox(line, "First line")
reader.Close
'In memory compression / decompression Dimdata() AsByte data = "Playing with in-memory compression.".GetBytes("UTF8")
Dimcompressed(), decompressed() AsByte compressed = compress.CompressBytes(data, "gzip")
decompressed = compress.DecompressBytes(compressed, "gzip")
'In this case the compressed data is longer than the decompressed data. 'The data is too short for the compression to be useful. Log("Compressed: " & compressed.Length)
Log("Decompressed: " & decompressed.Length)
Msgbox(BytesToString(decompressed,0, decompressed.Length, "UTF8"), "")
EndSub SubWriteStringToStream(OutAsOutputStream, sAsString)
DimtAsTextWriter t.Initialize(Out)
t.Write(s)
t.Close'Closes the internal stream as well EndSub
CountingInputStream and CountingOutputStream allow you to monitor the reading or writing progress. Counting streams wrap the actual stream and provide a Count property which allows you to get the number of bytes read or written. Counting streams are useful when the reading or writing operations are done in the background. You can then use a timer to monitor the progress. This example logs the downloading progress: SubProcess_Globals DimhcAsHttpClient DimcoutAsCountingOutputStream DimlengthAsInt Dimtimer1AsTimer EndSub SubGlobals
This object allows you to non-sequentially access files and bytes arrays. You can also use it to encode numbers to bytes (and vice versa). Note that assets files (files added with the file manager) cannot be opened with this object as those files are actually packed inside the APK file. A short tutorial about the encryption methods is available here.