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