OutputStream


Object Hierarchy:

GLib.OutputStream GLib.OutputStream GLib.OutputStream GLib.Object GLib.Object GLib.Object->GLib.OutputStream

Description:

[ CCode ( type_id = "g_output_stream_get_type ()" ) ]
public abstract class OutputStream : Object

`GOutputStream` is a base class for implementing streaming output.

It has functions to write to a stream ([method@Gio.OutputStream.write]), to close a stream ([method@Gio.OutputStream.close]) and to flush pending writes ([method@Gio.OutputStream.flush]).

To copy the content of an input stream to an output stream without manually handling the reads and writes, use [method@Gio.OutputStream.splice] .

See the documentation for [class@Gio.IOStream] for details of thread safety of streaming APIs.

All of these functions have async variants too.

All classes derived from `GOutputStream` *should* implement synchronous writing, splicing, flushing and closing streams, but *may* implement asynchronous versions.

Example: IOStream:

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");
IOStream ios = file.create_readwrite (FileCreateFlags.PRIVATE);


//
// Write data:
//

size_t bytes_written;
OutputStream os = ios.output_stream;
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);
ios = null;


// Open the file for reading:
InputStream @is = file.read ();


//
// Read n bytes:
//

// 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.IOStream.vala

Required by:

Namespace: GLib
Package: gio-2.0

Content:

Creation methods:

Methods:

Inherited Members: