BinaryStream
From Real Software Documentation
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. On Windows and Macintosh only, BinaryStream objects can work with files larger than 2 gigabytes.
| Properties | ||||||
|
| Shared Methods | ||
|
| Constructors | |||
|
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
What is the LittleEndian property? The Windows and Linux operating systems store binary values in the reverse order from the Mac OS PowerPC machines. If you were using the ReadShort or ReadLong methods to read data from a file that was in Little Endian format, you would get incorrect data. Real Studio reads data in Big Endian format. Most Macintosh files are in Big Endian format. If you are reading a file that is in Little Endian format, you will need to set the Little Endian property to True before you begin reading the file. This applies to writing data with WriteShort and WriteLong.
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 Mac OS), the value 258 would be stored as:
while in Little Endian, it would be stored as:
If the LittleEndian property is set incorrectly, then you would read the value as 513.
Because Real Studio has the LittleEndian property, you can 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.
Examples
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.
If readFile <> Nil Then
Dim ReadStream as BinaryStream = BinaryStream.Open(readFile, False)
ReadStream.littleEndian = true
dim writeFile as FolderItem = GetSaveFolderItem("","")
If writeFile <> Nil Then
dim writeStream as BinaryStream =BinaryStream.Create(writeFile, true)
writeStream.LittleEndian = true
Do Until ReadStream.EOF
writeStream.WriteShort ReadStream.ReadShort
Loop
writeStream = nil
End If
readStream = nil
End If
This example displays the Save As dialog box and writes the contents of the TextArea to a text file.
Dim stream as BinaryStream
f=GetSaveFolderItem(FileTypes1.Text,"Untitled.txt")
If f<> Nil Then
stream=BinaryStream.Create(f,True)
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.
