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.InputStream;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import glib.Bytes;
30 private import glib.ErrorG;
31 private import glib.GException;
32 private import gobject.ObjectG;
33 private import gtkc.gio;
34 public  import gtkc.giotypes;
35 
36 
37 /**
38  * #GInputStream has functions to read from a stream (g_input_stream_read()),
39  * to close a stream (g_input_stream_close()) and to skip some content
40  * (g_input_stream_skip()).
41  * 
42  * To copy the content of an input stream to an output stream without
43  * manually handling the reads and writes, use g_output_stream_splice().
44  * 
45  * All of these functions have async variants too.
46  */
47 public class InputStream : ObjectG
48 {
49 	/** the main Gtk struct */
50 	protected GInputStream* gInputStream;
51 
52 	/** Get the main Gtk struct */
53 	public GInputStream* getInputStreamStruct()
54 	{
55 		return gInputStream;
56 	}
57 
58 	/** the main Gtk struct as a void* */
59 	protected override void* getStruct()
60 	{
61 		return cast(void*)gInputStream;
62 	}
63 
64 	protected override void setStruct(GObject* obj)
65 	{
66 		gInputStream = cast(GInputStream*)obj;
67 		super.setStruct(obj);
68 	}
69 
70 	/**
71 	 * Sets our main struct and passes it to the parent class.
72 	 */
73 	public this (GInputStream* gInputStream, bool ownedRef = false)
74 	{
75 		this.gInputStream = gInputStream;
76 		super(cast(GObject*)gInputStream, ownedRef);
77 	}
78 
79 	/**
80 	 */
81 
82 	public static GType getType()
83 	{
84 		return g_input_stream_get_type();
85 	}
86 
87 	/**
88 	 * Clears the pending flag on @stream.
89 	 */
90 	public void clearPending()
91 	{
92 		g_input_stream_clear_pending(gInputStream);
93 	}
94 
95 	/**
96 	 * Closes the stream, releasing resources related to it.
97 	 *
98 	 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
99 	 * Closing a stream multiple times will not return an error.
100 	 *
101 	 * Streams will be automatically closed when the last reference
102 	 * is dropped, but you might want to call this function to make sure
103 	 * resources are released as early as possible.
104 	 *
105 	 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
106 	 * open after the stream is closed. See the documentation for the individual
107 	 * stream for details.
108 	 *
109 	 * On failure the first error that happened will be reported, but the close
110 	 * operation will finish as much as possible. A stream that failed to
111 	 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
112 	 * is important to check and report the error to the user.
113 	 *
114 	 * If @cancellable is not %NULL, then the operation can be cancelled by
115 	 * triggering the cancellable object from another thread. If the operation
116 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
117 	 * Cancelling a close will still leave the stream closed, but some streams
118 	 * can use a faster close that doesn't block to e.g. check errors.
119 	 *
120 	 * Params:
121 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
122 	 *
123 	 * Return: %TRUE on success, %FALSE on failure
124 	 *
125 	 * Throws: GException on failure.
126 	 */
127 	public bool close(Cancellable cancellable)
128 	{
129 		GError* err = null;
130 		
131 		auto p = g_input_stream_close(gInputStream, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
132 		
133 		if (err !is null)
134 		{
135 			throw new GException( new ErrorG(err) );
136 		}
137 		
138 		return p;
139 	}
140 
141 	/**
142 	 * Requests an asynchronous closes of the stream, releasing resources related to it.
143 	 * When the operation is finished @callback will be called.
144 	 * You can then call g_input_stream_close_finish() to get the result of the
145 	 * operation.
146 	 *
147 	 * For behaviour details see g_input_stream_close().
148 	 *
149 	 * The asyncronous methods have a default fallback that uses threads to implement
150 	 * asynchronicity, so they are optional for inheriting classes. However, if you
151 	 * override one you must override all.
152 	 *
153 	 * Params:
154 	 *     ioPriority = the [I/O priority][io-priority] of the request
155 	 *     cancellable = optional cancellable object
156 	 *     callback = callback to call when the request is satisfied
157 	 *     userData = the data to pass to callback function
158 	 */
159 	public void closeAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
160 	{
161 		g_input_stream_close_async(gInputStream, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
162 	}
163 
164 	/**
165 	 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
166 	 *
167 	 * Params:
168 	 *     result = a #GAsyncResult.
169 	 *
170 	 * Return: %TRUE if the stream was closed successfully.
171 	 *
172 	 * Throws: GException on failure.
173 	 */
174 	public bool closeFinish(AsyncResultIF result)
175 	{
176 		GError* err = null;
177 		
178 		auto p = g_input_stream_close_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0;
179 		
180 		if (err !is null)
181 		{
182 			throw new GException( new ErrorG(err) );
183 		}
184 		
185 		return p;
186 	}
187 
188 	/**
189 	 * Checks if an input stream has pending actions.
190 	 *
191 	 * Return: %TRUE if @stream has pending actions.
192 	 */
193 	public bool hasPending()
194 	{
195 		return g_input_stream_has_pending(gInputStream) != 0;
196 	}
197 
198 	/**
199 	 * Checks if an input stream is closed.
200 	 *
201 	 * Return: %TRUE if the stream is closed.
202 	 */
203 	public bool isClosed()
204 	{
205 		return g_input_stream_is_closed(gInputStream) != 0;
206 	}
207 
208 	/**
209 	 * Tries to read @count bytes from the stream into the buffer starting at
210 	 * @buffer. Will block during this read.
211 	 *
212 	 * If count is zero returns zero and does nothing. A value of @count
213 	 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
214 	 *
215 	 * On success, the number of bytes read into the buffer is returned.
216 	 * It is not an error if this is not the same as the requested size, as it
217 	 * can happen e.g. near the end of a file. Zero is returned on end of file
218 	 * (or if @count is zero),  but never otherwise.
219 	 *
220 	 * The returned @buffer is not a nul-terminated string, it can contain nul bytes
221 	 * at any position, and this function doesn't nul-terminate the @buffer.
222 	 *
223 	 * If @cancellable is not %NULL, then the operation can be cancelled by
224 	 * triggering the cancellable object from another thread. If the operation
225 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
226 	 * operation was partially finished when the operation was cancelled the
227 	 * partial result will be returned, without an error.
228 	 *
229 	 * On error -1 is returned and @error is set accordingly.
230 	 *
231 	 * Params:
232 	 *     buffer = a buffer to
233 	 *         read data into (which should be at least count bytes long).
234 	 *     count = the number of bytes that will be read from the stream
235 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
236 	 *
237 	 * Return: Number of bytes read, or -1 on error, or 0 on end of file.
238 	 *
239 	 * Throws: GException on failure.
240 	 */
241 	public ptrdiff_t read(ubyte[] buffer, Cancellable cancellable)
242 	{
243 		GError* err = null;
244 		
245 		auto p = g_input_stream_read(gInputStream, buffer.ptr, cast(size_t)buffer.length, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
246 		
247 		if (err !is null)
248 		{
249 			throw new GException( new ErrorG(err) );
250 		}
251 		
252 		return p;
253 	}
254 
255 	/**
256 	 * Tries to read @count bytes from the stream into the buffer starting at
257 	 * @buffer. Will block during this read.
258 	 *
259 	 * This function is similar to g_input_stream_read(), except it tries to
260 	 * read as many bytes as requested, only stopping on an error or end of stream.
261 	 *
262 	 * On a successful read of @count bytes, or if we reached the end of the
263 	 * stream,  %TRUE is returned, and @bytes_read is set to the number of bytes
264 	 * read into @buffer.
265 	 *
266 	 * If there is an error during the operation %FALSE is returned and @error
267 	 * is set to indicate the error status, @bytes_read is updated to contain
268 	 * the number of bytes read into @buffer before the error occurred.
269 	 *
270 	 * Params:
271 	 *     buffer = a buffer to
272 	 *         read data into (which should be at least count bytes long).
273 	 *     count = the number of bytes that will be read from the stream
274 	 *     bytesRead = location to store the number of bytes that was read from the stream
275 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
276 	 *
277 	 * Return: %TRUE on success, %FALSE if there was an error
278 	 *
279 	 * Throws: GException on failure.
280 	 */
281 	public bool readAll(ubyte[] buffer, out size_t bytesRead, Cancellable cancellable)
282 	{
283 		GError* err = null;
284 		
285 		auto p = g_input_stream_read_all(gInputStream, buffer.ptr, cast(size_t)buffer.length, &bytesRead, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
286 		
287 		if (err !is null)
288 		{
289 			throw new GException( new ErrorG(err) );
290 		}
291 		
292 		return p;
293 	}
294 
295 	/**
296 	 * Request an asynchronous read of @count bytes from the stream into the buffer
297 	 * starting at @buffer. When the operation is finished @callback will be called.
298 	 * You can then call g_input_stream_read_finish() to get the result of the
299 	 * operation.
300 	 *
301 	 * During an async request no other sync and async calls are allowed on @stream, and will
302 	 * result in %G_IO_ERROR_PENDING errors.
303 	 *
304 	 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
305 	 *
306 	 * On success, the number of bytes read into the buffer will be passed to the
307 	 * callback. It is not an error if this is not the same as the requested size, as it
308 	 * can happen e.g. near the end of a file, but generally we try to read
309 	 * as many bytes as requested. Zero is returned on end of file
310 	 * (or if @count is zero),  but never otherwise.
311 	 *
312 	 * Any outstanding i/o request with higher priority (lower numerical value) will
313 	 * be executed before an outstanding request with lower priority. Default
314 	 * priority is %G_PRIORITY_DEFAULT.
315 	 *
316 	 * The asyncronous methods have a default fallback that uses threads to implement
317 	 * asynchronicity, so they are optional for inheriting classes. However, if you
318 	 * override one you must override all.
319 	 *
320 	 * Params:
321 	 *     buffer = a buffer to
322 	 *         read data into (which should be at least count bytes long).
323 	 *     count = the number of bytes that will be read from the stream
324 	 *     ioPriority = the [I/O priority][io-priority]
325 	 *         of the request.
326 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
327 	 *     callback = callback to call when the request is satisfied
328 	 *     userData = the data to pass to callback function
329 	 */
330 	public void readAsync(ubyte[] buffer, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
331 	{
332 		g_input_stream_read_async(gInputStream, buffer.ptr, cast(size_t)buffer.length, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
333 	}
334 
335 	/**
336 	 * Like g_input_stream_read(), this tries to read @count bytes from
337 	 * the stream in a blocking fashion. However, rather than reading into
338 	 * a user-supplied buffer, this will create a new #GBytes containing
339 	 * the data that was read. This may be easier to use from language
340 	 * bindings.
341 	 *
342 	 * If count is zero, returns a zero-length #GBytes and does nothing. A
343 	 * value of @count larger than %G_MAXSSIZE will cause a
344 	 * %G_IO_ERROR_INVALID_ARGUMENT error.
345 	 *
346 	 * On success, a new #GBytes is returned. It is not an error if the
347 	 * size of this object is not the same as the requested size, as it
348 	 * can happen e.g. near the end of a file. A zero-length #GBytes is
349 	 * returned on end of file (or if @count is zero), but never
350 	 * otherwise.
351 	 *
352 	 * If @cancellable is not %NULL, then the operation can be cancelled by
353 	 * triggering the cancellable object from another thread. If the operation
354 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
355 	 * operation was partially finished when the operation was cancelled the
356 	 * partial result will be returned, without an error.
357 	 *
358 	 * On error %NULL is returned and @error is set accordingly.
359 	 *
360 	 * Params:
361 	 *     count = maximum number of bytes that will be read from the stream. Common
362 	 *         values include 4096 and 8192.
363 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
364 	 *
365 	 * Return: a new #GBytes, or %NULL on error
366 	 *
367 	 * Since: 2.34
368 	 *
369 	 * Throws: GException on failure.
370 	 */
371 	public Bytes readBytes(size_t count, Cancellable cancellable)
372 	{
373 		GError* err = null;
374 		
375 		auto p = g_input_stream_read_bytes(gInputStream, count, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
376 		
377 		if (err !is null)
378 		{
379 			throw new GException( new ErrorG(err) );
380 		}
381 		
382 		if(p is null)
383 		{
384 			return null;
385 		}
386 		
387 		return new Bytes(cast(GBytes*) p);
388 	}
389 
390 	/**
391 	 * Request an asynchronous read of @count bytes from the stream into a
392 	 * new #GBytes. When the operation is finished @callback will be
393 	 * called. You can then call g_input_stream_read_bytes_finish() to get the
394 	 * result of the operation.
395 	 *
396 	 * During an async request no other sync and async calls are allowed
397 	 * on @stream, and will result in %G_IO_ERROR_PENDING errors.
398 	 *
399 	 * A value of @count larger than %G_MAXSSIZE will cause a
400 	 * %G_IO_ERROR_INVALID_ARGUMENT error.
401 	 *
402 	 * On success, the new #GBytes will be passed to the callback. It is
403 	 * not an error if this is smaller than the requested size, as it can
404 	 * happen e.g. near the end of a file, but generally we try to read as
405 	 * many bytes as requested. Zero is returned on end of file (or if
406 	 * @count is zero), but never otherwise.
407 	 *
408 	 * Any outstanding I/O request with higher priority (lower numerical
409 	 * value) will be executed before an outstanding request with lower
410 	 * priority. Default priority is %G_PRIORITY_DEFAULT.
411 	 *
412 	 * Params:
413 	 *     count = the number of bytes that will be read from the stream
414 	 *     ioPriority = the [I/O priority][io-priority] of the request
415 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
416 	 *     callback = callback to call when the request is satisfied
417 	 *     userData = the data to pass to callback function
418 	 *
419 	 * Since: 2.34
420 	 */
421 	public void readBytesAsync(size_t count, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
422 	{
423 		g_input_stream_read_bytes_async(gInputStream, count, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
424 	}
425 
426 	/**
427 	 * Finishes an asynchronous stream read-into-#GBytes operation.
428 	 *
429 	 * Params:
430 	 *     result = a #GAsyncResult.
431 	 *
432 	 * Return: the newly-allocated #GBytes, or %NULL on error
433 	 *
434 	 * Since: 2.34
435 	 *
436 	 * Throws: GException on failure.
437 	 */
438 	public Bytes readBytesFinish(AsyncResultIF result)
439 	{
440 		GError* err = null;
441 		
442 		auto p = g_input_stream_read_bytes_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err);
443 		
444 		if (err !is null)
445 		{
446 			throw new GException( new ErrorG(err) );
447 		}
448 		
449 		if(p is null)
450 		{
451 			return null;
452 		}
453 		
454 		return new Bytes(cast(GBytes*) p);
455 	}
456 
457 	/**
458 	 * Finishes an asynchronous stream read operation.
459 	 *
460 	 * Params:
461 	 *     result = a #GAsyncResult.
462 	 *
463 	 * Return: number of bytes read in, or -1 on error, or 0 on end of file.
464 	 *
465 	 * Throws: GException on failure.
466 	 */
467 	public ptrdiff_t readFinish(AsyncResultIF result)
468 	{
469 		GError* err = null;
470 		
471 		auto p = g_input_stream_read_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err);
472 		
473 		if (err !is null)
474 		{
475 			throw new GException( new ErrorG(err) );
476 		}
477 		
478 		return p;
479 	}
480 
481 	/**
482 	 * Sets @stream to have actions pending. If the pending flag is
483 	 * already set or @stream is closed, it will return %FALSE and set
484 	 * @error.
485 	 *
486 	 * Return: %TRUE if pending was previously unset and is now set.
487 	 *
488 	 * Throws: GException on failure.
489 	 */
490 	public bool setPending()
491 	{
492 		GError* err = null;
493 		
494 		auto p = g_input_stream_set_pending(gInputStream, &err) != 0;
495 		
496 		if (err !is null)
497 		{
498 			throw new GException( new ErrorG(err) );
499 		}
500 		
501 		return p;
502 	}
503 
504 	/**
505 	 * Tries to skip @count bytes from the stream. Will block during the operation.
506 	 *
507 	 * This is identical to g_input_stream_read(), from a behaviour standpoint,
508 	 * but the bytes that are skipped are not returned to the user. Some
509 	 * streams have an implementation that is more efficient than reading the data.
510 	 *
511 	 * This function is optional for inherited classes, as the default implementation
512 	 * emulates it using read.
513 	 *
514 	 * If @cancellable is not %NULL, then the operation can be cancelled by
515 	 * triggering the cancellable object from another thread. If the operation
516 	 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
517 	 * operation was partially finished when the operation was cancelled the
518 	 * partial result will be returned, without an error.
519 	 *
520 	 * Params:
521 	 *     count = the number of bytes that will be skipped from the stream
522 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
523 	 *
524 	 * Return: Number of bytes skipped, or -1 on error
525 	 *
526 	 * Throws: GException on failure.
527 	 */
528 	public ptrdiff_t skip(size_t count, Cancellable cancellable)
529 	{
530 		GError* err = null;
531 		
532 		auto p = g_input_stream_skip(gInputStream, count, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
533 		
534 		if (err !is null)
535 		{
536 			throw new GException( new ErrorG(err) );
537 		}
538 		
539 		return p;
540 	}
541 
542 	/**
543 	 * Request an asynchronous skip of @count bytes from the stream.
544 	 * When the operation is finished @callback will be called.
545 	 * You can then call g_input_stream_skip_finish() to get the result
546 	 * of the operation.
547 	 *
548 	 * During an async request no other sync and async calls are allowed,
549 	 * and will result in %G_IO_ERROR_PENDING errors.
550 	 *
551 	 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
552 	 *
553 	 * On success, the number of bytes skipped will be passed to the callback.
554 	 * It is not an error if this is not the same as the requested size, as it
555 	 * can happen e.g. near the end of a file, but generally we try to skip
556 	 * as many bytes as requested. Zero is returned on end of file
557 	 * (or if @count is zero), but never otherwise.
558 	 *
559 	 * Any outstanding i/o request with higher priority (lower numerical value)
560 	 * will be executed before an outstanding request with lower priority.
561 	 * Default priority is %G_PRIORITY_DEFAULT.
562 	 *
563 	 * The asynchronous methods have a default fallback that uses threads to
564 	 * implement asynchronicity, so they are optional for inheriting classes.
565 	 * However, if you override one, you must override all.
566 	 *
567 	 * Params:
568 	 *     count = the number of bytes that will be skipped from the stream
569 	 *     ioPriority = the [I/O priority][io-priority] of the request
570 	 *     cancellable = optional #GCancellable object, %NULL to ignore.
571 	 *     callback = callback to call when the request is satisfied
572 	 *     userData = the data to pass to callback function
573 	 */
574 	public void skipAsync(size_t count, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
575 	{
576 		g_input_stream_skip_async(gInputStream, count, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
577 	}
578 
579 	/**
580 	 * Finishes a stream skip operation.
581 	 *
582 	 * Params:
583 	 *     result = a #GAsyncResult.
584 	 *
585 	 * Return: the size of the bytes skipped, or %-1 on error.
586 	 *
587 	 * Throws: GException on failure.
588 	 */
589 	public ptrdiff_t skipFinish(AsyncResultIF result)
590 	{
591 		GError* err = null;
592 		
593 		auto p = g_input_stream_skip_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err);
594 		
595 		if (err !is null)
596 		{
597 			throw new GException( new ErrorG(err) );
598 		}
599 		
600 		return p;
601 	}
602 }