VirtualVolume

From Xojo Documentation

Class (inherits from Object)

Enables you to create and maintain a hierarchy of "virtual" files within one physical file. Basic reading and writing text and binary streams are supported.

Properties
Root fa-lock-32.png


Methods
Flush

Notes

The FolderItem class has two methods and one property for working with virtual volumes. Use the CreateAsVirtualVolume and OpenAsVirtualVolume methods to create and open virtual volumes. The VirtualVolume property of the FolderItem class returns the VirtualVolume if the FolderItem is in a virtual volume.

Once you've created a VirtualVolume, you can get its Root and navigate to it using the same FolderItem methods that you would for real files. Use the Child method of the FolderItem class to save and read files in the virtual volume.

Virtual files are cross-platform compatible, and support basic reading and writing as text or binary files. The SaveAsPicture method of the FolderItem class does not work for virtual volumes. Virtual volumes do not support resource forks.

Virtual volumes flush their data to disk whenever a virtual volume is closed. This helps ensure that your data is safely on disk in the event of a crash or loss of power.

Filenames can be up to 223 bytes long. Paths are not supported, but directories are (e.g., create a virtual directory with the CreateAsFolder method of the FolderItem class, and navigate to it with the Child or Item methods of this class). The virtual file system supports four-byte type codes (accessed via f.MacType), but does not support creator codes.

Examples

The following example creates a VirtualVolume, gets its Root property, and writes a text file to the virtual volume.

Dim vv As VirtualVolume
Dim realFile As FolderItem
Dim virtFile As FolderItem
Dim outp As TextOutputStream
realFile = GetFolderItem("VV")
vv = realFile.CreateVirtualVolume
If vv = Nil Then
MsgBox("Unable to create virtual volume")
Else
virtFile = vv.Root.Child("Virtual File.txt")
outp = TextOutputStream.Create(virtFile)
outp.Write("Hello world!")
End If

To access an existing virtual volume, use the OpenAsVirtualVolume method instead of the CreateVirtualVolume method of the FolderItem class.

The following example saves the contents of a TextArea as a file in a VirtualVolume. It is passed the name of the file.

Sub SaveTextFile(s As String)
//creates a VirtualVolume and saves the text in the TextArea as a file

Dim f As FolderItem
Dim b As BinaryStream
Dim v As New VirtualVolume
f = GetSaveFolderItem("", "myVirtualVolume")
If f <> Nil Then
v = f.CreateVirtualVolume
f = v.Root.Child(s)
b = BinaryStream.Create(f, False)
b.Write(TextArea1.Text)
b.Close
End If
End Sub

The following example reads the text file that was saved by the previous example. It is passed the name of the file.

Sub ReadTextFile(s As String)
// reads the text file in the VirtualVolume
Dim f, virtualFile As Folderitem
Dim v As VirtualVolume
f = GetOpenFolderItem("myVirtualVolume")
If f <> Nil Then
v = f.OpenAsVirtualVolume
If v <> Nil Then
virtualFile = v.Root.Child(s)
If virtualFile <> Nil Then
Dim stream As BinaryStream
stream = BinaryStream.Open(virtualFile, False)
Do
TextArea2.AppendText(stream.Read(255))
Loop Until stream.EOF
stream.Close
End If
End If
Else
MsgBox(Str(f.LastErrorCode))
End If
End Sub

See Also

VolumeCount function; FolderItem class.