OutputStream - How does it work?

tdocs2

Well-Known Member
Licensed User
Longtime User
I do not know how to use OutputStream - the code below gives me an error on

Writer.WriteLine(OS.WriteBytes(Data,0,text2.Length-1))

B4X:
'try converting text2 which contains UFT8 characters 
Dim Data() As Byte
Data = text2.GetBytes("ISO-8859-1")  '("ASCII") - ASCII provides a limited set of 128 characters and not the ANSI equivalent

'1. deletefile in case
   If File.Exists (File.DirRootExternal& "/Test/",  "t120.txt") Then
      File.Delete(File.DirRootExternal& "/Test/",  "t120.txt")
   End If
'2. write new string file
   Dim OS As OutputStream
   OS.InitializeToBytesArray(120)
   Dim Writer As TextWriter
   Writer.Initialize2(File.OpenOutput(File.DirRootExternal& "/Test/",  "t120.txt",False),"ISO-8859-1")
   Writer.WriteLine(OS.WriteBytes(Data,0,text2.Length-1))
   Writer.Close
'3 read file ("ISO-8859-1")
   text2=""
   Dim Reader As TextReader
   Reader.Initialize(File.OpenInput(File.DirRootExternal& "/Test/",  "t120.txt"))
    text2 = Reader.ReadLine
   Reader.Close

   Log(text2)
   Log("t2 len" & text2.Length)

Obviously, I am missing the point.

Any help will be appreciated. Thank you.
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Corrected Issue

Thru trial and error, I corrected the error with the changes below. I am able to write the file. I have to do further testing to see if I am able to get the results I am looking for.

B4X:
'try converting text2 which contains UFT8 characters 
Dim Data() As Byte
Data = text2.GetBytes("ISO-8859-1")  '("ASCII") - ASCII provides a limited set of 128 characters and not the ANSI equivalent

'1. deletefile in case
   If File.Exists (File.DirRootExternal& "/Test/",  "t120.txt") Then
      File.Delete(File.DirRootExternal& "/Test/",  "t120.txt")
   End If
'2. write new string file
   Dim OS As OutputStream
   OS.InitializeToBytesArray(120)
   Dim Writer As TextWriter
   Writer.Initialize2(File.OpenOutput(File.DirRootExternal& "/Test/",  "t120.txt",False),"ISO-8859-1")
             OS.WriteBytes(Data,0,text2.Length-1) 'changed here *****
   Writer.Write(OS) 'and here ********  OS is treated as string variable
   Writer.Close
'3 read file ("ISO-8859-1")
   text2=""
   Dim Reader As TextReader
   Reader.Initialize(File.OpenInput(File.DirRootExternal& "/Test/",  "t120.txt"))
    text2 = Reader.ReadLine
   Reader.Close

   Log(text2)
   Log("t2 len" & text2.Length)

Best regards.
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
Character sets and SMS

Thank you, Erel.

The poor code was an experiment to learn to use OutputStream in an attempt to circumvent the SMS message limit in languages other than English.

From my additional research, the SMS message length for English is 140; for all others 70.

Thank you again for your efforts and for B4A.

Sandy
 
Last edited:
Upvote 0
Top