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