Class

# BinaryStream

<div class="rst-class">

forsearch

</div>

Stream

<div class="rst-class">

forsearch

</div>

File

<div class="rst-class">

forsearch

</div>

Files

<div class="rst-class">

forsearch

</div>

Reading

<div class="rst-class">

forsearch

</div>

Writing

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

<span class="title-ref">BinaryStream</span> objects are used to read and write data to and from a binary file. The benefit of using <span class="title-ref">BinaryStreams</span> 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

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                      | Type                                              | Read-Only | Shared |
|-------------------------------------------|---------------------------------------------------|-----------|--------|
| `BytePosition<binarystream.byteposition>` | `UInt64</api/data_types/additional_types/uint64>` |           |        |
| `Length<binarystream.length>`             | `UInt64</api/data_types/additional_types/uint64>` |           |        |
| `LittleEndian<binarystream.littleendian>` | `Boolean</api/data_types/boolean>`                |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                        | Parameters                                                                                                                                   | Returns                                           | Shared |
|---------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|--------|
| `Close<binarystream.close>`                 |                                                                                                                                              |                                                   |        |
| `Constructor<binarystream.constructor0>`    | aString As `String</api/data_types/string>`                                                                                                  |                                                   |        |
| `Constructor<binarystream.constructor1>`    | aMemoryBlock As `MemoryBlock</api/language/memoryblock>`                                                                                     |                                                   |        |
| `Constructor<binarystream.constructor2>`    | handle As `Integer</api/data_types/integer>`, type As `Integer</api/data_types/integer>`                                                     |                                                   |        |
| `Create<binarystream.create>`               | file As `FolderItem</api/files/folderitem>`, overwrite As `Boolean</api/data_types/boolean>` = `False</api/language/false>`                  | <span class="title-ref">BinaryStream</span>       | ✓      |
| `EndOfFile<binarystream.endoffile>`         |                                                                                                                                              | `Boolean</api/data_types/boolean>`                |        |
| `Flush<binarystream.flush>`                 |                                                                                                                                              |                                                   |        |
| `Handle<binarystream.handle>`               | type As `Integer</api/data_types/integer>`                                                                                                   | `Integer</api/data_types/integer>`                |        |
|                                             | type As `IOStreamHandleTypes<runtime.iostreamhandletypes>`                                                                                   | `Ptr</api/data_types/additional_types/ptr>`       |        |
| `Open<binarystream.open>`                   | file As `FolderItem</api/files/folderitem>`, readwrite As `Boolean</api/data_types/boolean>` = `False</api/language/false>`                  | <span class="title-ref">BinaryStream</span>       | ✓      |
| `Read<binarystream.read>`                   | byteCount As `Integer</api/data_types/integer>`, encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>` | `String</api/data_types/string>`                  |        |
| `ReadBoolean<binarystream.readboolean>`     |                                                                                                                                              | `Boolean</api/data_types/boolean>`                |        |
| `ReadCurrency<binarystream.readcurrency>`   |                                                                                                                                              | `Currency</api/data_types/currency>`              |        |
| `ReadDouble<binarystream.readdouble>`       |                                                                                                                                              | `Double</api/data_types/double>`                  |        |
| `ReadError<binarystream.readerror>`         |                                                                                                                                              | `Boolean</api/data_types/boolean>`                |        |
| `ReadInt16<binarystream.readint16>`         |                                                                                                                                              | `Int16</api/data_types/additional_types/int16>`   |        |
| `ReadInt32<binarystream.readint32>`         |                                                                                                                                              | `Int32</api/data_types/additional_types/int32>`   |        |
| `ReadInt64<binarystream.readint64>`         |                                                                                                                                              | `Int64</api/data_types/additional_types/int64>`   |        |
| `ReadInt8<binarystream.readint8>`           |                                                                                                                                              | `Int8</api/data_types/additional_types/int8>`     |        |
| `ReadPString<binarystream.readpstring>`     | encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`                                                  | `String</api/data_types/string>`                  |        |
| `ReadSingle<binarystream.readsingle>`       |                                                                                                                                              | `Double</api/data_types/double>`                  |        |
| `ReadUInt16<binarystream.readuint16>`       |                                                                                                                                              | `UInt16</api/data_types/additional_types/uint16>` |        |
| `ReadUInt32<binarystream.readuint32>`       |                                                                                                                                              | `UInt32</api/data_types/additional_types/uint32>` |        |
| `ReadUInt64<binarystream.readuint64>`       |                                                                                                                                              | `UInt64</api/data_types/additional_types/uint64>` |        |
| `ReadUInt8<binarystream.readuint8>`         |                                                                                                                                              | `UInt8</api/data_types/additional_types/uint8>`   |        |
| `Write<binarystream.write>`                 | value As `String</api/data_types/string>`                                                                                                    |                                                   |        |
| `WriteBoolean<binarystream.writeboolean>`   | value As `Boolean</api/data_types/boolean>`                                                                                                  |                                                   |        |
| `WriteCurrency<binarystream.writecurrency>` | value As `Currency</api/data_types/currency>`                                                                                                |                                                   |        |
| `WriteDouble<binarystream.writedouble>`     | value As `Double</api/data_types/double>`                                                                                                    |                                                   |        |
| `WriteInt16<binarystream.writeint16>`       | value As `Int16</api/data_types/additional_types/int16>`                                                                                     |                                                   |        |
| `WriteInt32<binarystream.writeint32>`       | value As `Int32</api/data_types/additional_types/int32>`                                                                                     |                                                   |        |
| `WriteInt64<binarystream.writeint64>`       | value As `Int64</api/data_types/additional_types/int64>`                                                                                     |                                                   |        |
| `WriteInt8<binarystream.writeint8>`         | value As `Int8</api/data_types/additional_types/int8>`                                                                                       |                                                   |        |
| `WritePString<binarystream.writepstring>`   | value As `String</api/data_types/string>`                                                                                                    |                                                   |        |
| `WriteSingle<binarystream.writesingle>`     | value As `Double</api/data_types/double>`                                                                                                    |                                                   |        |
| `WriteUInt16<binarystream.writeuint16>`     | value As `UInt16</api/data_types/additional_types/uint16>`                                                                                   |                                                   |        |
| `WriteUInt32<binarystream.writeuint32>`     | value As `UInt32</api/data_types/additional_types/uint32>`                                                                                   |                                                   |        |
| `WriteUInt64<binarystream.writeuint64>`     | value As `UInt64</api/data_types/additional_types/uint64>`                                                                                   |                                                   |        |
| `WriteUInt8<binarystream.writeuint8>`       | value As `UInt8</api/data_types/additional_types/uint8>`                                                                                     |                                                   |        |

## 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        |
| HandleTypeWin32Handle | A Windows32 OS handle |

## Property descriptions

<div id="binarystream.byteposition">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.BytePosition

**BytePosition** As `UInt64</api/data_types/additional_types/uint64>`

> Gets or sets the current file position in the <span class="title-ref">BinaryStream</span>. The first position is numbered zero.
>
> This property is automatically incremented by all of the Read and Write methods.
>
> To move the position to the end of the stream, set it to `BinaryStream<binarystream.length>`.
>
> This code changes the position from the default position of zero.
>
> ``` xojo
> readStream.BytePosition = 100
> ```

<div id="binarystream.length">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Length

**Length** As `UInt64</api/data_types/additional_types/uint64>`

> The length of the file in bytes. If you set the Length property to a value smaller than its current value, it will truncate the file.
>
> ``` xojo
> Var i As UInt64
> Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
> i = readStream.Length
> ```

<div id="binarystream.littleendian">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.LittleEndian

**LittleEndian** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>`, the byte order is assumed to be low byte, high byte.
>
> By default, <span class="title-ref">BinaryStream</span> reads data in the BigEndian byte order.
>
> The following code sets the LittleEndian flag to `True</api/language/true>`.
>
> ``` xojo
> Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
> readStream.LittleEndian = True
> ```

## Method descriptions

<div id="binarystream.close">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Close

**Close**

> Closes the stream (and hence the file opened by the stream).
>
> This code backs a <span class="title-ref">BinaryStream</span> with a MemoryBlock that is declared 0-sized:
>
> ``` xojo
> Var mb As New MemoryBlock(0)
> Var bs As New BinaryStream(mb)
> bs.WriteLong(4)
> bs.WriteDouble(3.14)
> bs.Close
> MessageBox(mb.Long(0).ToString)
> ```

<div id="binarystream.constructor0">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Constructor

**Constructor**(aString As `String</api/data_types/string>`)

> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> `Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.
>
> </div>
>
> Creates a <span class="title-ref">BinaryStream</span> from a `String</api/data_types/string>`.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not currently supported for Android.
>
> </div>

<div id="binarystream.constructor1">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Constructor

**Constructor**(aMemoryBlock As `MemoryBlock</api/language/memoryblock>`)

> Creates a <span class="title-ref">BinaryStream</span> from a `MemoryBlock</api/language/memoryblock>`.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not currently supported for Android.
>
> </div>

<div id="binarystream.constructor2">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Constructor

**Constructor**(handle As `Integer</api/data_types/integer>`, type As `Integer</api/data_types/integer>`)

> Creates a <span class="title-ref">BinaryStream</span> from an OS handle.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not currently supported for Android.
>
> </div>
>
> *type* is one of the HandleType class constants and *handle* is the appropriate handle type specified by the *type* parameter.
>
> The <span class="title-ref">BinaryStream</span> class constants given below can be passed as the parameter.
>
> | Constant              | Description            |
> |-----------------------|------------------------|
> |                       |                        |
> | HandleTypeWin32Handle | A Windows32 OS handle. |
> | HandleTypeFilePointer | A file pointer.        |
> | HandleTypeFileNumber  | A file descriptor.     |
>
> For instance, you can use a `Declare</api/language/declare>` to open a file with whatever permissions that you wish, and then pass the Handle to a stream object's constructor. When you do this, the <span class="title-ref">BinaryStream</span> now retains the ownership rights to the file handle, so you should not close the file handle (via a declare for example), since it will close when the <span class="title-ref">BinaryStream</span> is destructed.
>
> ``` xojo
> Var b As New BinaryStream(i, BinaryStream.HandleTypeWin32Handle)
> ```

<div id="binarystream.create">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Create

**Create**(file As `FolderItem</api/files/folderitem>`, overwrite As `Boolean</api/data_types/boolean>` = `False</api/language/false>`) As <span class="title-ref">BinaryStream</span>

> Creates a file and opens a binary stream for reading and exclusive writing to the passed `FolderItem</api/files/folderitem>`.
>
> This method is `shared</api/language/shared>`.
>
> The *overwrite* parameter controls whether the operation can overwrite an existing file. The default is `False</api/language/false>`.
>
> Invoke Create using the syntax for shared methods, i.e. use *BinaryStream.Create(...)*
>
> If the stream can not be opened as requested, an `IOException</api/exceptions/ioexception>` will be raised. For instance, this will happen if the file is already opened for writing, or if the app (its user) has no permission to modify this file or to create a file in the affected directory.
>
> This code 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.
>
> ``` xojo
> Var inputItem As FolderItem = FolderItem.ShowOpenFileDialog("")
> If inputItem <> Nil Then
>   Var inputStream As BinaryStream = BinaryStream.Open(f, False)
>   Var outputItem As FolderItem = FolderItem.ShowSaveFileDialog("","")
>   If outputItem <> Nil Then
>     Try
>       Var outputStream As BinaryStream = BinaryStream.Create(f, True)
>       outputStream.LittleEndian = Not inputStream.LittleEndian
>       Do Until inputStream.EndOfFile
>         If inputStream.Length - inputStream.BytePosition >= 2 Then
>           outputStream.WriteUInt16(inputStream.ReadUInt16)
>         Else
>           outputStream.WriteUInt8(inputStream.ReadByte)
>         End If
>       Loop
>
>     Catch exc As IOException
>       MessageBox("Oops - failed to create the output file.")
>     End Try
>   End If
> End If
> ```

<div id="binarystream.endoffile">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.EndOfFile

**EndOfFile** As `Boolean</api/data_types/boolean>`

> Returns `True</api/language/true>` when there's no more data left to read.
>
> This code reads the rows and columns of data from a tab-delimited text file into a `ListBox</api/user_interface/desktop/desktoplistbox>`:
>
> ``` xojo
> Var f As FolderItem
> Var textInput As TextInputStream
> Var rowFromFile As String
>
> f = FolderItem.ShowOpenFileDialog("text/plain") ' defined as a FileType
> If f <> Nil Then
>   textInput = TextInputStream.Open(f)
>   textInput.Encoding = Encodings.UTF8
>
>   Do
>     rowFromFile = textInput.ReadLine
>     Var values() As String = rowFromFile.ToArray(String.Chr(9))
>     ListBox1.ColumnCount = values.Count
>     ListBox1.AddRow("")
>     Var col As Integer
>     For Each value As String In values
>       ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, col) = value
>       col = col + 1
>     Next
>   Loop Until textInput.EndOfFile
>
>   textInput.Close
> End If
> ```
>
> 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. The EOF property is used to terminate the `Do...Loop</api/language/loops/do...loop>`.
>
> ``` xojo
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text")
> If readFile <> Nil Then
>   Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
>   ReadStream.LittleEndian = True
>   Var writeFile As FolderItem = FolderItem.ShowSaveFileDialog("", "")
>   If writeFile <> Nil Then
>     Var writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
>     writeStream.LittleEndian = True
>     Do Until ReadStream.EndOfFile
>       writeStream.WriteInt8(ReadStream.ReadInt8)
>     Loop
>     writeStream = Nil
>   End If
>   readStream = Nil
> End If
> ```

<div id="binarystream.flush">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Flush

**Flush**

> Immediately sends the contents of internal write buffers to disk or to the output stream.
>
> This function can be useful in point-to-point communication over sockets and similar connections: To optimize for transmission performance, some types of output streams try to collect small pieces of written data into one larger piece for sending instead of sending each piece out individually. By calling Flush, the data collection is stopped and the data is sent without further delay, reducing latency.
>
> When using this on a stream that ends up as a file on disk, it is useful, too: Any short parts of previously written data are written to disk right away, ensuring the data is actually on disk if the application terminates abruptly, e.g. due to a crash.
>
> Avoid calling this method too often. For example, do not call it between successive Write calls because you'll slow down performance without getting much benefit.
>
> A typical use case would look like this:
>
> ``` xojo
> mySocket.Write("you typed: ")
> mySocket.Write(key)
> mySocket.Write(".")
> mySocket.Flush
> ```

<div id="binarystream.handle">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Handle

**Handle**(type As `Integer</api/data_types/integer>`) As `Integer</api/data_types/integer>`

> Returns a handle of the *type* passed.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not supported for Android.
>
> </div>
>
> The <span class="title-ref">BinaryStream</span> class constants given below can be passed as the parameter.
>
> | Name                  | Description            |
> |-----------------------|------------------------|
> | HandleTypeWin32Handle | A Windows32 OS Handle. |
> | HandleTypeFilePointer | A file pointer.        |
> | HandleTypeDescriptor  | A file descriptor.     |
>
> The following code gets a Win32 handle.
>
> ``` xojo
> Var i As Integer
> i = ReadStream.Handle(BinaryStream.HandleTypeWin32Handle)
> ```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

**Handle**(type As `IOStreamHandleTypes<runtime.iostreamhandletypes>`) As `Ptr</api/data_types/additional_types/ptr>`

> Returns a handle of the *type* passed.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not supported for Android.
>
> </div>

<div id="binarystream.open">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Open

**Open**(file As `FolderItem</api/files/folderitem>`, readwrite As `Boolean</api/data_types/boolean>` = `False</api/language/false>`) As <span class="title-ref">BinaryStream</span>

> Opens the passed `FolderItem</api/files/folderitem>` as a binary stream. The *readwrite* parameter controls whether the *file* is open for read/write operations or read-only. When `True</api/language/true>`, you can read and write to the file. When `False</api/language/false>` you can only read from the file. The default is `False</api/language/false>`. If an error occurs, an `IOException</api/exceptions/ioexception>` is raised.
>
> This method is `shared</api/language/shared>`.
>
> Call Open using the syntax for a shared method.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> Android cannot open a file for Read/Write. Calling it will raise an `PlatformNotSupportedException</api/exceptions/platformnotsupportedexception>`.
>
> </div>
>
> This code reads a text file called "Whole File" and displays it in a `TextArea</api/user_interface/desktop/desktoptextarea>`. There is no attempt to parse the contents of the file.
>
> ``` xojo
> Var file As New FolderItem("Whole File", FolderItem.PathModes.Native)
>
> If file <> Nil Then
>   Try
>     Var bs As BinaryStream = BinaryStream.Open(file, False) ' Open as read-only
>     ' read the whole binaryStream
>     TextArea1.Text = bs.Read(bs.Length)
>
>   Catch e As IOException
>     ' something went wrong when opening the file.  This is where you would handle the error
>     ' if appropriate
>   End Try
> End If
> ```
>
> This code 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.
>
> ``` xojo
> Var inputItem As FolderItem = FolderItem.ShowOpenFileDialog("")
> If inputItem <> Nil Then
>   Var inputStream As BinaryStream = BinaryStream.Open(f, False)
>   Var outputItem As FolderItem = FolderItem.ShowSaveFileDialog("","")
>   If outputItem <> Nil Then
>     Try
>       Var outputStream As BinaryStream = BinaryStream.Create(f, True)
>       outputStream.LittleEndian = Not inputStream.LittleEndian
>       Do Until inputStream.EndOfFile
>         If inputStream.Length - inputStream.BytePosition >= 2 Then
>           outputStream.WriteUInt16(inputStream.ReadUInt16)
>         Else
>           outputStream.WriteUInt8(inputStream.ReadByte)
>         End If
>       Loop
>
>     Catch exc As IOException
>       MessageBox("Oops - failed to create the output file.")
>     End Try
>   End If
> End If
> ```

<div id="binarystream.read">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Read

**Read**(byteCount As `Integer</api/data_types/integer>`, encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Reads *byteCount* bytes from the input stream and returns a `String</api/data_types/string>`.
>
> If provided, the optional parameter *encoding* specifies the text encoding to be defined for the `String</api/data_types/string>` to be read.
>
> If *byteCount* is higher than the amount of bytes currently available in the stream, all available bytes will be returned. Therefore, make sure to always consider the case that you get less than you requested. To see if you received all requested bytes, check the returned string's `String<string.bytes>` property (avoid using `Length<binarystream.length>` as it may give a different number if the encoding is not nil).
>
> If not enough memory is available, you get back an empty string.
>
> This example reads the first 1000 bytes from a <span class="title-ref">BinaryStream</span>.
>
> ``` xojo
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
>   ReadStream.LittleEndian = True
>   TextArea1.Text = ReadStream.Read(1000, Encodings.UTF8)
> End If
> ```

<div id="binarystream.readboolean">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadBoolean

**ReadBoolean** As `Boolean</api/data_types/boolean>`

> Reads a `Boolean</api/data_types/boolean>` value from the stream and returns it as a `Boolean</api/data_types/boolean>`.
>
> If you read over the end of the stream you get `False</api/language/false>` returned from this method. No error and no exception. If you need to check for this, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> The following code reads the next value from the <span class="title-ref">BinaryStream</span> as a `Boolean</api/data_types/boolean>`.
>
> ``` xojo
> Var b As Boolean
> Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>
> b = readStream.ReadBoolean
> ```

<div id="binarystream.readcurrency">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadCurrency

**ReadCurrency** As `Currency</api/data_types/currency>`

> Reads a Currency value from the stream and returns it as a `Currency</api/data_types/currency>`.
>
> If you read over the end of the stream you get zero returned from this method. No error and no exception. If you want to check for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var d As Currency
> Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>
> d = readStream.ReadCurrency
> ```

<div id="binarystream.readdouble">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadDouble

**ReadDouble** As `Double</api/data_types/double>`

> Reads a double word from the stream and returns it as a `Double</api/data_types/double>`.
>
> If you read over the end of the stream you get zero returned from this method. No error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var d As Double
> Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>
> d = readStream.ReadDouble
> ```

<div id="binarystream.readerror">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadError

**ReadError** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>` then an error occurred during reading.

<div id="binarystream.readint16">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadInt16

**ReadInt16** As `Int16</api/data_types/additional_types/int16>`

> Reads a two-byte value from the stream and returns it as an `Int16</api/data_types/additional_types/int16>`.
>
> If you read over the end of the stream you get zero returned from this method. No error and no exception. If you need to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var i As Int16
> Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>
> i = readStream.ReadInt16
> ```

<div id="binarystream.readint32">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadInt32

**ReadInt32** As `Int32</api/data_types/additional_types/int32>`

> Reads a four-byte value from the stream and returns it as an `Int32</api/data_types/additional_types/int32>`.
>
> If you read over the end of the stream you get zero returned from this method. No error and no exception. If you need to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var i As Int32
> Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>
> i = readStream.ReadInt32
> ```

<div id="binarystream.readint64">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadInt64

**ReadInt64** As `Int64</api/data_types/additional_types/int64>`

> Reads an eight-byte value from the stream and returns it as an `Int64</api/data_types/additional_types/int64>`.
>
> If you read over the end of the stream you get zero returned from this method. No error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var i As Int64
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   i = readStream.ReadInt64
> End If
> ```

<div id="binarystream.readint8">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadInt8

**ReadInt8** As `Int8</api/data_types/additional_types/int8>`

> Reads a one-byte value from the stream and returns it as an `Int8</api/data_types/additional_types/int8>`.
>
> If you read over the end of the stream you get zero returned from this method. No error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var i As UInt8
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   i = readStream.ReadInt8
> End If
> ```

<div id="binarystream.readpstring">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadPString

**ReadPString**(encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Reads a Pascal string from the stream and returns it as a `String</api/data_types/string>`.
>
> The optional parameter *encoding* enables you to specify the encoding to be defined on the returned string. Use the `Encodings</api/text/encoding_text/encodings>` module to specify the text encoding. If you omit this parameter (or pass `Nil</api/language/nil>`), then the encoding of the returned string will be `Nil</api/language/nil>`.
>
> If you read over the end of the stream you get an empty string returned from this method. No error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var s As String
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
>
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   s = readStream.ReadPString
> End If
> ```
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not currently supported for Android. Calling it will raise an `PlatformNotSupportedException</api/exceptions/platformnotsupportedexception>` as PString is not a supported type on Android.
>
> </div>

<div id="binarystream.readsingle">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadSingle

**ReadSingle** As `Double</api/data_types/double>`

> Reads a word from the stream and returns it as a `Double</api/data_types/double>`.
>
> If you read over the end of the stream you get zero returned from this method. No error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var num As Single
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   num = readStream.Single
> End If
> ```

<div id="binarystream.readuint16">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadUInt16

**ReadUInt16** As `UInt16</api/data_types/additional_types/uint16>`

> Reads a two-byte value from the stream and returns it as a `UInt16</api/data_types/additional_types/uint16>`.
>
> If you read over the end of the stream you get zero returned from this method. There is no error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var i As UInt16
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   i = readStream.ReadUInt16
> End If
> ```

<div id="binarystream.readuint32">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadUInt32

**ReadUInt32** As `UInt32</api/data_types/additional_types/uint32>`

> Reads a four-byte value from the stream and returns it as a `UInt32</api/data_types/additional_types/uint32>`.
>
> If you read over the end of the stream you get zero returned from this method. There is no error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var i As UInt32
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   i = readStream.ReadUInt32
> End If
> ```

<div id="binarystream.readuint64">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadUInt64

**ReadUInt64** As `UInt64</api/data_types/additional_types/uint64>`

> Reads an eight-byte value from the stream and returns it as a `UInt64</api/data_types/additional_types/uint64>`.
>
> If you read over the end of the stream you get zero returned from this method. There is no error and no exception. If you want to test for this error, you can check the `EndOfFile<readable.endoffile>`, `BytePosition<binarystream.byteposition>`, and `Length<binarystream.length>` properties.
>
> ``` xojo
> Var i As UInt64
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   i = readStream.ReadUInt64
> End If
> ```

<div id="binarystream.readuint8">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.ReadUInt8

**ReadUInt8** As `UInt8</api/data_types/additional_types/uint8>`

> Reads a one-byte value from the stream and returns it as a `UInt8</api/data_types/additional_types/uint8>`.
>
> ``` xojo
> Var i As UInt8
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var readStream As BinaryStream = BinaryStream.Open(readFile, False)
>   i = readStream.ReadUInt8
> End If
> ```

<div id="binarystream.write">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.Write

**Write**(value As `String</api/data_types/string>`)

> Writes the passed *value* to the output stream.
>
> Note that in order to make sure that the data actually ends up on disk or gets sent to the socket it is connected to, the stream must either get closed or the `Flush<writeable.flush>` method be called. Otherwise, the data, if small, may end up temporarily in a write buffer before either a certain time has passed or more data is written. This buffering increases performance when writing lots of small pieces of data, but may be causing unwanted delays when another process, e.g. the other end of a socket connection, is waiting for the data. Consider calling the `Flush<writeable.flush>` method to reduce latencies that this buffering may cause in such cases.
>
> If Write fails, an `IOException</api/exceptions/ioexception>` will be raised.
>
> This example displays the Save As dialog box and writes the contents of the TextArea1 to a text file.
>
> ``` xojo
> Var f As FolderItem
> Var stream As BinaryStream
> f = FolderItem.ShowSaveFileDialog(FileTypes1.Text, "Untitled.txt")
> If f<> Nil Then
>   stream = BinaryStream.Create(f, True)
>   stream.Write(TextArea1.Text)
>   stream.Close
> End If
> ```

<div id="binarystream.writeboolean">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteBoolean

**WriteBoolean**(value As `Boolean</api/data_types/boolean>`)

> Writes the passed `boolean</api/data_types/boolean>` to the stream.
>
> ``` xojo
> Var b As Boolean = False
> Var writeStream As BinaryStream
> writeStream.WriteBoolean(b)
> ```

<div id="binarystream.writecurrency">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteCurrency

**WriteCurrency**(value As `Currency</api/data_types/currency>`)

> Writes the passed `Currency</api/data_types/currency>` to the stream.
>
> ``` xojo
> Var c As Currency = 540.34
> Var writeStream As BinaryStream
> writeStream.WriteCurrency(c)
> ```

<div id="binarystream.writedouble">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteDouble

**WriteDouble**(value As `Double</api/data_types/double>`)

> Writes the value passed as a double word to the stream.
>
> ``` xojo
> Var d As Double = 3.1416
> Var writeStream As BinaryStream
> writeStream.WriteDouble(d)
> ```

<div id="binarystream.writeint16">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteInt16

**WriteInt16**(value As `Int16</api/data_types/additional_types/int16>`)

> Writes the passed `Int16</api/data_types/additional_types/int16>` to the stream.
>
> ``` xojo
> Var i As Int16 = 5
> Var writeStream As BinaryStream
> writeStream.WriteInt16(i)
> ```

<div id="binarystream.writeint32">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteInt32

**WriteInt32**(value As `Int32</api/data_types/additional_types/int32>`)

> Writes the passed `Int32</api/data_types/additional_types/int32>` to the steam.
>
> ``` xojo
> Var i As Int32 = 5
> Var writeStream As BinaryStream
> writeStream.WriteInt32(i)
> ```

<div id="binarystream.writeint64">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteInt64

**WriteInt64**(value As `Int64</api/data_types/additional_types/int64>`)

> Writes the passed `Int64</api/data_types/additional_types/int64>` to the stream.
>
> ``` xojo
> Var i As Int64 = 5434555
> Var writeStream As BinaryStream
> writeStream.WriteInt64(i)
> ```

<div id="binarystream.writeint8">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteInt8

**WriteInt8**(value As `Int8</api/data_types/additional_types/int8>`)

> Writes the passed `Int8</api/data_types/additional_types/int8>` to the stream.
>
> ``` xojo
> Var i As Int8 = 8
> Var writeStream As BinaryStream
> writeStream.WriteInt8(i)
> ```

<div id="binarystream.writepstring">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WritePString

**WritePString**(value As `String</api/data_types/string>`)

> Writes the *value* passed as a Pascal string to the stream.
>
> ``` xojo
> Var s As String = "theString"
> Var writeStream As BinaryStream
>
> writeStream.WritePString(s)
> ```
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method is not currently supported for Android. Calling it will raise an `PlatformNotSupportedException</api/exceptions/platformnotsupportedexception>` as PString is not a supported type on Android.
>
> </div>

<div id="binarystream.writesingle">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteSingle

**WriteSingle**(value As `Double</api/data_types/double>`)

> Writes the *value* passed as a single word to the stream.
>
> ``` xojo
> Var d As Single = 5.2437
> Var writeStream As BinaryStream
> writeStream.WriteSingle(d)
> ```

<div id="binarystream.writeuint16">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteUInt16

**WriteUInt16**(value As `UInt16</api/data_types/additional_types/uint16>`)

> Writes the passed `UInt16</api/data_types/additional_types/uint16>` to the stream.
>
> ``` xojo
> Var i As UInt16 =  4
> Var writeStream As BinaryStream
> writeStream.WriteUInt16(i)
> ```

<div id="binarystream.writeuint32">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteUInt32

**WriteUInt32**(value As `UInt32</api/data_types/additional_types/uint32>`)

> Writes the passed `Uint32</api/data_types/additional_types/uint32>` to the steam.
>
> ``` xojo
> Var i As UInt32 = 427
> Var writeStream As BinaryStream
> writeStream.WriteUInt32(i)
> ```

<div id="binarystream.writeuint64">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteUInt64

**WriteUInt64**(value As `UInt64</api/data_types/additional_types/uint64>`)

> Writes the passed `UInt64</api/data_types/additional_types/uint64>` to the stream.
>
> ``` xojo
> Var i As UInt64 = 427
> Var writeStream As BinaryStream
> writeStream.WriteUInt64(i)
> ```

<div id="binarystream.writeuint8">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

BinaryStream.WriteUInt8

**WriteUInt8**(value As `UInt8</api/data_types/additional_types/uint8>`)

> Writes the passed `UInt8</api/data_types/additional_types/uint8>` to the stream.
>
> ``` xojo
> Var i As UInt8 = 4
> Var writeStream As BinaryStream
> writeStream.WriteUInt8(i)
> ```

## Interfaces

This class implements the `Readable</api/files/readable>` and `Writeable</api/files/writeable>` class interfaces.

## Notes

When the <span class="title-ref">BinaryStream</span> 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</api/language/true>` before you begin reading the file. This applies to writing data with WriteInt16 and WriteInt32.

You can use the constants `TargetLittleEndian</api/compiler_directives/targetlittleendian>` and `TargetBigEndian</api/compiler_directives/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:

``` xojo
01 02
```

while in Little Endian, it would be stored as:

``` xojo
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</api/language/true>` if the file format is intrinsically little endian (i.e. GIF files), otherwise leave it as `False</api/language/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.

``` xojo
Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text")
If readFile <> Nil And readFile.Exists Then
  Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
  ReadStream.LittleEndian = True
  Var WriteFile As FolderItem = FolderItem.ShowSaveFileDialog("", "")
  If writeFile <> Nil Then
    Var writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
    writeStream.LittleEndian = False
    Do Until ReadStream.EndOfFile
      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</api/user_interface/desktop/desktoptextarea>` to a text file.

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `FolderItem</api/files/folderitem>`, `IOException</api/exceptions/ioexception>`, `MemoryBlock</api/language/memoryblock>`, `TextInputStream</api/files/textinputstream>`, `TextOutputStream</api/files/textoutputstream>` classes; `TargetBigEndian</api/compiler_directives/targetbigendian>`, `TargetLittleEndian</api/compiler_directives/targetlittleendian>` constants.

</div>
