FileIOStream


Object Hierarchy:

GLib.FileIOStream GLib.FileIOStream GLib.FileIOStream GLib.IOStream GLib.IOStream GLib.IOStream->GLib.FileIOStream GLib.Object GLib.Object GLib.Object->GLib.IOStream GLib.Seekable GLib.Seekable GLib.Seekable->GLib.FileIOStream

Description:

[ CCode ( type_id = "g_file_io_stream_get_type ()" ) ]
[ Version ( since = "2.22" ) ]
public class FileIOStream : IOStream, Seekable

`GFileIOStream` provides I/O streams that both read and write to the same file handle.

`GFileIOStream` implements [iface@Gio.Seekable], which allows the I/O stream to jump to arbitrary positions in the file and to truncate the file, provided the filesystem of the file supports these operations.

To find the position of a file I/O stream, use [method@Gio.Seekable.tell].

To find out if a file I/O stream supports seeking, use [method@Gio.Seekable.can_seek]. To position a file I/O stream, use [ method@Gio.Seekable.seek]. To find out if a file I/O stream supports truncating, use [method@Gio.Seekable.can_truncate]. To truncate a file I/O stream, use [method@Gio.Seekable.truncate].

The default implementation of all the `GFileIOStream` operations and the implementation of [iface@Gio.Seekable] just call into the same operations on the output stream.

Example: FileIOStream:

public static int main (string[] args) {
try {
// Create a file that can only be accessed by the current user:
File file = File.new_for_path ("my-test.bin");
FileIOStream ios = file.create_readwrite (FileCreateFlags.PRIVATE);

//
// Write data:
//

size_t bytes_written;
FileOutputStream os = ios.output_stream as FileOutputStream;
os.write_all ("My 1. line\n".data, out bytes_written);
os.write_all ("My 2. line\n".data, out bytes_written);

DataOutputStream dos = new DataOutputStream (os);
dos.put_string ("My 3. line\n");
dos.put_int16 (10);


//
// Set the file pointer to the beginning of the stream:
//
assert (ios.can_seek ());
ios.seek (0, SeekType.SET);


//
// Read n bytes:
//

FileInputStream @is = ios.input_stream as FileInputStream;

// Output: ``M``
uint8 buffer[1];
size_t size = @is.read (buffer);
stdout.write (buffer, size);

// Output: ``y 1. line``
DataInputStream dis = new DataInputStream (@is);
string str = dis.read_line ();
print ("%s\n", str);

// Output: ``My 2. line``
str = dis.read_line ();
print ("%s\n", str);

// Output: ``My 3. line``
str = dis.read_line ();
print ("%s\n", str);

// Output: ``10``
int16 i = dis.read_int16 ();
print ("%d\n", i);
} catch (Error e) {
print ("Error: %s\n", e.message);
}

return 0;
}

valac --pkg gio-2.0 GLib.FileIOStream.vala


Namespace: GLib
Package: gio-2.0

Content:

Creation methods:

Methods:

Inherited Members:

All known members inherited from interface GLib.Seekable