BinaryStream

From Xojo Documentation

Class (inherits from Object)

BinaryStream objects are used to read and write data to and from a binary file. The benefit of using BinaryStreams rather than text streams is that you can read from and write to any position in the file. Text files must be read sequentially from the start to the end.

Properties
BytePosition Length LittleEndian
Methods
Close ReadInt8 WriteInt16
EndOfFile ReadPString WriteInt32
Flush ReadSingle WriteInt64
Handle ReadUInt16 WriteInt8
Read ReadUInt32 WritePString
ReadBoolean ReadUInt64 WriteSingle
ReadCurrency ReadUInt8 WriteUInt16
ReadDouble Write WriteUInt32
ReadInt16 WriteBoolean WriteUInt64
ReadInt32 WriteCurrency WriteUInt8
ReadInt64 WriteDouble
Shared Methods
Create Open
Constructors

Constructor(Handle as Integer, Type as Integer)


Constructor(mb as MemoryBlock)


Constructor(s as String)



Class Constants

The following class constants can be used to specify the value of the Type parameter of the Handle property.

Class Constant Description
HandleTypeFileNumber A file descriptor
HandleTypeFilePointer A file pointer
HandleTypeMacFileSpecPointer An FSSpec
HandleTypeMacFileRefNum A File reference number
HandleTypeWin32Handle A Windows32 OS handle

Interfaces

The BinaryStream class implements the Readable and Writeable class interfaces.

Notes

When the BinaryStream goes out of scope, it is flushed and closed.

Endian Information

What is the LittleEndian property? The Windows and Linux operating systems store binary values in the reverse order from the macOS machines. If you were using the ReadInt16 or ReadIn32 methods to read data from a file that was in Little Endian format, you would get incorrect data. Data is read in Big Endian format by default. Most Mac files are in Big Endian format. If you are reading a file that is in Little Endian format, you will need to set the LittleEndian property to True before you begin reading the file. This applies to writing data with WriteInt16 and WriteInt32.

You can use the constants TargetLittleEndian and TargetBigEndian to determine which byte order is being used for a particular compile.

For example, in big endian (like the macOS), the value 258 would be stored as:

01 02

while in Little Endian, it would be stored as:

02 01

If the LittleEndian property is set incorrectly, then you would read the value as 513.

You can use the LittleEndian property to write your code to be OS-independent. Set the LittleEndian property to True if the file format is intrinsically little endian (i.e. GIF files), otherwise leave it as False.

Sample Code

This example reads each pair of bytes from a file and writes them in reverse order to a new file. The user chooses the source file using the Open-file dialog box and saves the new file using the Save as dialog box.

Var readFile As FolderItem = GetOpenFolderItem("text")
If readFile <> Nil And readFile.Exists Then
Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
ReadStream.LittleEndian = True
Var WriteFile As FolderItem = GetSaveFolderItem("", "")
If writeFile <> Nil Then
Dim writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
writeStream.LittleEndian = True
Do Until ReadStream.EOF
writeStream.WriteInt32(ReadStream.ReadInt32)
Loop
writeStream.Close
End If
readStream.Close
End If

This code displays the Save As dialog box and writes the contents of the TextArea to a text file.

Var f As FolderItem
Var stream As BinaryStream
f = GetSaveFolderItem(FileTypes1.Text, "Untitled.txt")
If f <> Nil Then
stream = BinaryStream.Create(f, True) // Overwrite if exists
stream.Write(TextArea1.Text)
stream.Close
End If

Check the individual properties and methods for more examples.

See Also

FolderItem, IOException, MemoryBlock, TextInputStream, TextOutputStream classes; TargetBigEndian, TargetLittleEndian constants.