1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.IOStream;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.InputStream;
30 private import gio.OutputStream;
31 private import gio.c.functions;
32 public  import gio.c.types;
33 private import glib.ErrorG;
34 private import glib.GException;
35 private import gobject.ObjectG;
36 public  import gtkc.giotypes;
37 
38 
39 /**
40  * GIOStream represents an object that has both read and write streams.
41  * Generally the two streams act as separate input and output streams,
42  * but they share some common resources and state. For instance, for
43  * seekable streams, both streams may use the same position.
44  * 
45  * Examples of #GIOStream objects are #GSocketConnection, which represents
46  * a two-way network connection; and #GFileIOStream, which represents a
47  * file handle opened in read-write mode.
48  * 
49  * To do the actual reading and writing you need to get the substreams
50  * with g_io_stream_get_input_stream() and g_io_stream_get_output_stream().
51  * 
52  * The #GIOStream object owns the input and the output streams, not the other
53  * way around, so keeping the substreams alive will not keep the #GIOStream
54  * object alive. If the #GIOStream object is freed it will be closed, thus
55  * closing the substreams, so even if the substreams stay alive they will
56  * always return %G_IO_ERROR_CLOSED for all operations.
57  * 
58  * To close a stream use g_io_stream_close() which will close the common
59  * stream object and also the individual substreams. You can also close
60  * the substreams themselves. In most cases this only marks the
61  * substream as closed, so further I/O on it fails but common state in the
62  * #GIOStream may still be open. However, some streams may support
63  * "half-closed" states where one direction of the stream is actually shut down.
64  * 
65  * Operations on #GIOStreams cannot be started while another operation on the
66  * #GIOStream or its substreams is in progress. Specifically, an application can
67  * read from the #GInputStream and write to the #GOutputStream simultaneously
68  * (either in separate threads, or as asynchronous operations in the same
69  * thread), but an application cannot start any #GIOStream operation while there
70  * is a #GIOStream, #GInputStream or #GOutputStream operation in progress, and
71  * an application can’t start any #GInputStream or #GOutputStream operation
72  * while there is a #GIOStream operation in progress.
73  * 
74  * This is a product of individual stream operations being associated with a
75  * given #GMainContext (the thread-default context at the time the operation was
76  * started), rather than entire streams being associated with a single
77  * #GMainContext.
78  * 
79  * GIO may run operations on #GIOStreams from other (worker) threads, and this
80  * may be exposed to application code in the behaviour of wrapper streams, such
81  * as #GBufferedInputStream or #GTlsConnection. With such wrapper APIs,
82  * application code may only run operations on the base (wrapped) stream when
83  * the wrapper stream is idle. Note that the semantics of such operations may
84  * not be well-defined due to the state the wrapper stream leaves the base
85  * stream in (though they are guaranteed not to crash).
86  */
87 public class IOStream : ObjectG
88 {
89 	/** the main Gtk struct */
90 	protected GIOStream* gIOStream;
91 
92 	/** Get the main Gtk struct */
93 	public GIOStream* getIOStreamStruct(bool transferOwnership = false)
94 	{
95 		if (transferOwnership)
96 			ownedRef = false;
97 		return gIOStream;
98 	}
99 
100 	/** the main Gtk struct as a void* */
101 	protected override void* getStruct()
102 	{
103 		return cast(void*)gIOStream;
104 	}
105 
106 	protected override void setStruct(GObject* obj)
107 	{
108 		gIOStream = cast(GIOStream*)obj;
109 		super.setStruct(obj);
110 	}
111 
112 	/**
113 	 * Sets our main struct and passes it to the parent class.
114 	 */
115 	public this (GIOStream* gIOStream, bool ownedRef = false)
116 	{
117 		this.gIOStream = gIOStream;
118 		super(cast(GObject*)gIOStream, ownedRef);
119 	}
120 
121 
122 	/** */
123 	public static GType getType()
124 	{
125 		return g_io_stream_get_type();
126 	}
127 
128 	/**
129 	 * Finishes an asynchronous io stream splice operation.
130 	 *
131 	 * Params:
132 	 *     result = a #GAsyncResult.
133 	 *
134 	 * Returns: %TRUE on success, %FALSE otherwise.
135 	 *
136 	 * Since: 2.28
137 	 *
138 	 * Throws: GException on failure.
139 	 */
140 	public static bool spliceFinish(AsyncResultIF result)
141 	{
142 		GError* err = null;
143 
144 		auto p = g_io_stream_splice_finish((result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
145 
146 		if (err !is null)
147 		{
148 			throw new GException( new ErrorG(err) );
149 		}
150 
151 		return p;
152 	}
153 
154 	/**
155 	 * Clears the pending flag on @stream.
156 	 *
157 	 * Since: 2.22
158 	 */
159 	public void clearPending()
160 	{
161 		g_io_stream_clear_pending(gIOStream);
162 	}
163 
164 	/**
165 	 * Closes the stream, releasing resources related to it. This will also
166 	 * close the individual input and output streams, if they are not already
167 	 * closed.
168 	 *
169 	 * Once the stream is closed, all other operations will return
170 	 * %G_IO_ERROR_CLOSED. Closing a stream multiple times will not
171 	 * return an error.
172 	 *
173 	 * Closing a stream will automatically flush any outstanding buffers
174 	 * in the stream.
175 	 *
176 	 * Streams will be automatically closed when the last reference
177 	 * is dropped, but you might want to call this function to make sure
178 	 * resources are released as early as possible.
179 	 *
180 	 * Some streams might keep the backing store of the stream (e.g. a file
181 	 * descriptor) open after the stream is closed. See the documentation for
182 	 * the individual stream for details.
183 	 *
184 	 * On failure the first error that happened will be reported, but the
185 	 * close operation will finish as much as possible. A stream that failed
186 	 * to close will still return %G_IO_ERROR_CLOSED for all operations.
187 	 * Still, it is important to check and report the error to the user,
188 	 * otherwise there might be a loss of data as all data might not be written.
189 	 *
190 	 * If @cancellable is not NULL, then the operation can be cancelled by
191 	 * triggering the cancellable object from another thread. If the operation
192 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
193 	 * Cancelling a close will still leave the stream closed, but some streams
194 	 * can use a faster close that doesn't block to e.g. check errors.
195 	 *
196 	 * The default implementation of this method just calls close on the
197 	 * individual input/output streams.
198 	 *
199 	 * Params:
200 	 *     cancellable = optional #GCancellable object, %NULL to ignore
201 	 *
202 	 * Returns: %TRUE on success, %FALSE on failure
203 	 *
204 	 * Since: 2.22
205 	 *
206 	 * Throws: GException on failure.
207 	 */
208 	public bool close(Cancellable cancellable)
209 	{
210 		GError* err = null;
211 
212 		auto p = g_io_stream_close(gIOStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
213 
214 		if (err !is null)
215 		{
216 			throw new GException( new ErrorG(err) );
217 		}
218 
219 		return p;
220 	}
221 
222 	/**
223 	 * Requests an asynchronous close of the stream, releasing resources
224 	 * related to it. When the operation is finished @callback will be
225 	 * called. You can then call g_io_stream_close_finish() to get
226 	 * the result of the operation.
227 	 *
228 	 * For behaviour details see g_io_stream_close().
229 	 *
230 	 * The asynchronous methods have a default fallback that uses threads
231 	 * to implement asynchronicity, so they are optional for inheriting
232 	 * classes. However, if you override one you must override all.
233 	 *
234 	 * Params:
235 	 *     ioPriority = the io priority of the request
236 	 *     cancellable = optional cancellable object
237 	 *     callback = callback to call when the request is satisfied
238 	 *     userData = the data to pass to callback function
239 	 *
240 	 * Since: 2.22
241 	 */
242 	public void closeAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
243 	{
244 		g_io_stream_close_async(gIOStream, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
245 	}
246 
247 	/**
248 	 * Closes a stream.
249 	 *
250 	 * Params:
251 	 *     result = a #GAsyncResult
252 	 *
253 	 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
254 	 *
255 	 * Since: 2.22
256 	 *
257 	 * Throws: GException on failure.
258 	 */
259 	public bool closeFinish(AsyncResultIF result)
260 	{
261 		GError* err = null;
262 
263 		auto p = g_io_stream_close_finish(gIOStream, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
264 
265 		if (err !is null)
266 		{
267 			throw new GException( new ErrorG(err) );
268 		}
269 
270 		return p;
271 	}
272 
273 	/**
274 	 * Gets the input stream for this object. This is used
275 	 * for reading.
276 	 *
277 	 * Returns: a #GInputStream, owned by the #GIOStream.
278 	 *     Do not free.
279 	 *
280 	 * Since: 2.22
281 	 */
282 	public InputStream getInputStream()
283 	{
284 		auto p = g_io_stream_get_input_stream(gIOStream);
285 
286 		if(p is null)
287 		{
288 			return null;
289 		}
290 
291 		return ObjectG.getDObject!(InputStream)(cast(GInputStream*) p);
292 	}
293 
294 	/**
295 	 * Gets the output stream for this object. This is used for
296 	 * writing.
297 	 *
298 	 * Returns: a #GOutputStream, owned by the #GIOStream.
299 	 *     Do not free.
300 	 *
301 	 * Since: 2.22
302 	 */
303 	public OutputStream getOutputStream()
304 	{
305 		auto p = g_io_stream_get_output_stream(gIOStream);
306 
307 		if(p is null)
308 		{
309 			return null;
310 		}
311 
312 		return ObjectG.getDObject!(OutputStream)(cast(GOutputStream*) p);
313 	}
314 
315 	/**
316 	 * Checks if a stream has pending actions.
317 	 *
318 	 * Returns: %TRUE if @stream has pending actions.
319 	 *
320 	 * Since: 2.22
321 	 */
322 	public bool hasPending()
323 	{
324 		return g_io_stream_has_pending(gIOStream) != 0;
325 	}
326 
327 	/**
328 	 * Checks if a stream is closed.
329 	 *
330 	 * Returns: %TRUE if the stream is closed.
331 	 *
332 	 * Since: 2.22
333 	 */
334 	public bool isClosed()
335 	{
336 		return g_io_stream_is_closed(gIOStream) != 0;
337 	}
338 
339 	/**
340 	 * Sets @stream to have actions pending. If the pending flag is
341 	 * already set or @stream is closed, it will return %FALSE and set
342 	 * @error.
343 	 *
344 	 * Returns: %TRUE if pending was previously unset and is now set.
345 	 *
346 	 * Since: 2.22
347 	 *
348 	 * Throws: GException on failure.
349 	 */
350 	public bool setPending()
351 	{
352 		GError* err = null;
353 
354 		auto p = g_io_stream_set_pending(gIOStream, &err) != 0;
355 
356 		if (err !is null)
357 		{
358 			throw new GException( new ErrorG(err) );
359 		}
360 
361 		return p;
362 	}
363 
364 	/**
365 	 * Asyncronously splice the output stream of @stream1 to the input stream of
366 	 * @stream2, and splice the output stream of @stream2 to the input stream of
367 	 * @stream1.
368 	 *
369 	 * When the operation is finished @callback will be called.
370 	 * You can then call g_io_stream_splice_finish() to get the
371 	 * result of the operation.
372 	 *
373 	 * Params:
374 	 *     stream2 = a #GIOStream.
375 	 *     flags = a set of #GIOStreamSpliceFlags.
376 	 *     ioPriority = the io priority of the request.
377 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
378 	 *     callback = a #GAsyncReadyCallback.
379 	 *     userData = user data passed to @callback.
380 	 *
381 	 * Since: 2.28
382 	 */
383 	public void spliceAsync(IOStream stream2, GIOStreamSpliceFlags flags, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
384 	{
385 		g_io_stream_splice_async(gIOStream, (stream2 is null) ? null : stream2.getIOStreamStruct(), flags, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
386 	}
387 }