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 232 * read data into (which should be at least count bytes long). 233 * cancellable = optional #GCancellable object, %NULL to ignore. 234 * 235 * Returns: Number of bytes read, or -1 on error, or 0 on end of file. 236 * 237 * Throws: GException on failure. 238 */ 239 public ptrdiff_t read(ubyte[] buffer, Cancellable cancellable) 240 { 241 GError* err = null; 242 243 auto p = g_input_stream_read(gInputStream, buffer.ptr, 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 return p; 251 } 252 253 /** 254 * Tries to read @count bytes from the stream into the buffer starting at 255 * @buffer. Will block during this read. 256 * 257 * This function is similar to g_input_stream_read(), except it tries to 258 * read as many bytes as requested, only stopping on an error or end of stream. 259 * 260 * On a successful read of @count bytes, or if we reached the end of the 261 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes 262 * read into @buffer. 263 * 264 * If there is an error during the operation %FALSE is returned and @error 265 * is set to indicate the error status. 266 * 267 * As a special exception to the normal conventions for functions that 268 * use #GError, if this function returns %FALSE (and sets @error) then 269 * @bytes_read will be set to the number of bytes that were successfully 270 * read before the error was encountered. This functionality is only 271 * available from C. If you need it from another language then you must 272 * write your own loop around g_input_stream_read(). 273 * 274 * Params: 275 * buffer = a buffer to 276 * read data into (which should be at least count bytes long). 277 * bytesRead = location to store the number of bytes that was read from the stream 278 * cancellable = optional #GCancellable object, %NULL to ignore. 279 * 280 * Returns: %TRUE on success, %FALSE if there was an error 281 * 282 * Throws: GException on failure. 283 */ 284 public bool readAll(ubyte[] buffer, out size_t bytesRead, Cancellable cancellable) 285 { 286 GError* err = null; 287 288 auto p = g_input_stream_read_all(gInputStream, buffer.ptr, cast(size_t)buffer.length, &bytesRead, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 289 290 if (err !is null) 291 { 292 throw new GException( new ErrorG(err) ); 293 } 294 295 return p; 296 } 297 298 /** 299 * Request an asynchronous read of @count bytes from the stream into the 300 * buffer starting at @buffer. 301 * 302 * This is the asynchronous equivalent of g_input_stream_read_all(). 303 * 304 * Call g_input_stream_read_all_finish() to collect the result. 305 * 306 * Any outstanding I/O request with higher priority (lower numerical 307 * value) will be executed before an outstanding request with lower 308 * priority. Default priority is %G_PRIORITY_DEFAULT. 309 * 310 * Params: 311 * buffer = a buffer to 312 * read data into (which should be at least count bytes long) 313 * ioPriority = the [I/O priority][io-priority] of the request 314 * cancellable = optional #GCancellable object, %NULL to ignore 315 * callback = callback to call when the request is satisfied 316 * userData = the data to pass to callback function 317 * 318 * Since: 2.44 319 */ 320 public void readAllAsync(ubyte[] buffer, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 321 { 322 g_input_stream_read_all_async(gInputStream, buffer.ptr, cast(size_t)buffer.length, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 323 } 324 325 /** 326 * Finishes an asynchronous stream read operation started with 327 * g_input_stream_read_all_async(). 328 * 329 * As a special exception to the normal conventions for functions that 330 * use #GError, if this function returns %FALSE (and sets @error) then 331 * @bytes_read will be set to the number of bytes that were successfully 332 * read before the error was encountered. This functionality is only 333 * available from C. If you need it from another language then you must 334 * write your own loop around g_input_stream_read_async(). 335 * 336 * Params: 337 * result = a #GAsyncResult 338 * bytesRead = location to store the number of bytes that was read from the stream 339 * 340 * Returns: %TRUE on success, %FALSE if there was an error 341 * 342 * Since: 2.44 343 * 344 * Throws: GException on failure. 345 */ 346 public bool readAllFinish(AsyncResultIF result, out size_t bytesRead) 347 { 348 GError* err = null; 349 350 auto p = g_input_stream_read_all_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &bytesRead, &err) != 0; 351 352 if (err !is null) 353 { 354 throw new GException( new ErrorG(err) ); 355 } 356 357 return p; 358 } 359 360 /** 361 * Request an asynchronous read of @count bytes from the stream into the buffer 362 * starting at @buffer. When the operation is finished @callback will be called. 363 * You can then call g_input_stream_read_finish() to get the result of the 364 * operation. 365 * 366 * During an async request no other sync and async calls are allowed on @stream, and will 367 * result in %G_IO_ERROR_PENDING errors. 368 * 369 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error. 370 * 371 * On success, the number of bytes read into the buffer will be passed to the 372 * callback. It is not an error if this is not the same as the requested size, as it 373 * can happen e.g. near the end of a file, but generally we try to read 374 * as many bytes as requested. Zero is returned on end of file 375 * (or if @count is zero), but never otherwise. 376 * 377 * Any outstanding i/o request with higher priority (lower numerical value) will 378 * be executed before an outstanding request with lower priority. Default 379 * priority is %G_PRIORITY_DEFAULT. 380 * 381 * The asynchronous methods have a default fallback that uses threads to implement 382 * asynchronicity, so they are optional for inheriting classes. However, if you 383 * override one you must override all. 384 * 385 * Params: 386 * buffer = a buffer to 387 * read data into (which should be at least count bytes long). 388 * ioPriority = the [I/O priority][io-priority] 389 * of the request. 390 * cancellable = optional #GCancellable object, %NULL to ignore. 391 * callback = callback to call when the request is satisfied 392 * userData = the data to pass to callback function 393 */ 394 public void readAsync(ubyte[] buffer, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 395 { 396 g_input_stream_read_async(gInputStream, buffer.ptr, cast(size_t)buffer.length, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 397 } 398 399 /** 400 * Like g_input_stream_read(), this tries to read @count bytes from 401 * the stream in a blocking fashion. However, rather than reading into 402 * a user-supplied buffer, this will create a new #GBytes containing 403 * the data that was read. This may be easier to use from language 404 * bindings. 405 * 406 * If count is zero, returns a zero-length #GBytes and does nothing. A 407 * value of @count larger than %G_MAXSSIZE will cause a 408 * %G_IO_ERROR_INVALID_ARGUMENT error. 409 * 410 * On success, a new #GBytes is returned. It is not an error if the 411 * size of this object is not the same as the requested size, as it 412 * can happen e.g. near the end of a file. A zero-length #GBytes is 413 * returned on end of file (or if @count is zero), but never 414 * otherwise. 415 * 416 * If @cancellable is not %NULL, then the operation can be cancelled by 417 * triggering the cancellable object from another thread. If the operation 418 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an 419 * operation was partially finished when the operation was cancelled the 420 * partial result will be returned, without an error. 421 * 422 * On error %NULL is returned and @error is set accordingly. 423 * 424 * Params: 425 * count = maximum number of bytes that will be read from the stream. Common 426 * values include 4096 and 8192. 427 * cancellable = optional #GCancellable object, %NULL to ignore. 428 * 429 * Returns: a new #GBytes, or %NULL on error 430 * 431 * Since: 2.34 432 * 433 * Throws: GException on failure. 434 */ 435 public Bytes readBytes(size_t count, Cancellable cancellable) 436 { 437 GError* err = null; 438 439 auto p = g_input_stream_read_bytes(gInputStream, count, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 440 441 if (err !is null) 442 { 443 throw new GException( new ErrorG(err) ); 444 } 445 446 if(p is null) 447 { 448 return null; 449 } 450 451 return new Bytes(cast(GBytes*) p, true); 452 } 453 454 /** 455 * Request an asynchronous read of @count bytes from the stream into a 456 * new #GBytes. When the operation is finished @callback will be 457 * called. You can then call g_input_stream_read_bytes_finish() to get the 458 * result of the operation. 459 * 460 * During an async request no other sync and async calls are allowed 461 * on @stream, and will result in %G_IO_ERROR_PENDING errors. 462 * 463 * A value of @count larger than %G_MAXSSIZE will cause a 464 * %G_IO_ERROR_INVALID_ARGUMENT error. 465 * 466 * On success, the new #GBytes will be passed to the callback. It is 467 * not an error if this is smaller than the requested size, as it can 468 * happen e.g. near the end of a file, but generally we try to read as 469 * many bytes as requested. Zero is returned on end of file (or if 470 * @count is zero), but never otherwise. 471 * 472 * Any outstanding I/O request with higher priority (lower numerical 473 * value) will be executed before an outstanding request with lower 474 * priority. Default priority is %G_PRIORITY_DEFAULT. 475 * 476 * Params: 477 * count = the number of bytes that will be read from the stream 478 * ioPriority = the [I/O priority][io-priority] of the request 479 * cancellable = optional #GCancellable object, %NULL to ignore. 480 * callback = callback to call when the request is satisfied 481 * userData = the data to pass to callback function 482 * 483 * Since: 2.34 484 */ 485 public void readBytesAsync(size_t count, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 486 { 487 g_input_stream_read_bytes_async(gInputStream, count, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 488 } 489 490 /** 491 * Finishes an asynchronous stream read-into-#GBytes operation. 492 * 493 * Params: 494 * result = a #GAsyncResult. 495 * 496 * Returns: the newly-allocated #GBytes, or %NULL on error 497 * 498 * Since: 2.34 499 * 500 * Throws: GException on failure. 501 */ 502 public Bytes readBytesFinish(AsyncResultIF result) 503 { 504 GError* err = null; 505 506 auto p = g_input_stream_read_bytes_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err); 507 508 if (err !is null) 509 { 510 throw new GException( new ErrorG(err) ); 511 } 512 513 if(p is null) 514 { 515 return null; 516 } 517 518 return new Bytes(cast(GBytes*) p, true); 519 } 520 521 /** 522 * Finishes an asynchronous stream read operation. 523 * 524 * Params: 525 * result = a #GAsyncResult. 526 * 527 * Returns: number of bytes read in, or -1 on error, or 0 on end of file. 528 * 529 * Throws: GException on failure. 530 */ 531 public ptrdiff_t readFinish(AsyncResultIF result) 532 { 533 GError* err = null; 534 535 auto p = g_input_stream_read_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err); 536 537 if (err !is null) 538 { 539 throw new GException( new ErrorG(err) ); 540 } 541 542 return p; 543 } 544 545 /** 546 * Sets @stream to have actions pending. If the pending flag is 547 * already set or @stream is closed, it will return %FALSE and set 548 * @error. 549 * 550 * Returns: %TRUE if pending was previously unset and is now set. 551 * 552 * Throws: GException on failure. 553 */ 554 public bool setPending() 555 { 556 GError* err = null; 557 558 auto p = g_input_stream_set_pending(gInputStream, &err) != 0; 559 560 if (err !is null) 561 { 562 throw new GException( new ErrorG(err) ); 563 } 564 565 return p; 566 } 567 568 /** 569 * Tries to skip @count bytes from the stream. Will block during the operation. 570 * 571 * This is identical to g_input_stream_read(), from a behaviour standpoint, 572 * but the bytes that are skipped are not returned to the user. Some 573 * streams have an implementation that is more efficient than reading the data. 574 * 575 * This function is optional for inherited classes, as the default implementation 576 * emulates it using read. 577 * 578 * If @cancellable is not %NULL, then the operation can be cancelled by 579 * triggering the cancellable object from another thread. If the operation 580 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an 581 * operation was partially finished when the operation was cancelled the 582 * partial result will be returned, without an error. 583 * 584 * Params: 585 * count = the number of bytes that will be skipped from the stream 586 * cancellable = optional #GCancellable object, %NULL to ignore. 587 * 588 * Returns: Number of bytes skipped, or -1 on error 589 * 590 * Throws: GException on failure. 591 */ 592 public ptrdiff_t skip(size_t count, Cancellable cancellable) 593 { 594 GError* err = null; 595 596 auto p = g_input_stream_skip(gInputStream, count, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 597 598 if (err !is null) 599 { 600 throw new GException( new ErrorG(err) ); 601 } 602 603 return p; 604 } 605 606 /** 607 * Request an asynchronous skip of @count bytes from the stream. 608 * When the operation is finished @callback will be called. 609 * You can then call g_input_stream_skip_finish() to get the result 610 * of the operation. 611 * 612 * During an async request no other sync and async calls are allowed, 613 * and will result in %G_IO_ERROR_PENDING errors. 614 * 615 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error. 616 * 617 * On success, the number of bytes skipped will be passed to the callback. 618 * It is not an error if this is not the same as the requested size, as it 619 * can happen e.g. near the end of a file, but generally we try to skip 620 * as many bytes as requested. Zero is returned on end of file 621 * (or if @count is zero), but never otherwise. 622 * 623 * Any outstanding i/o request with higher priority (lower numerical value) 624 * will be executed before an outstanding request with lower priority. 625 * Default priority is %G_PRIORITY_DEFAULT. 626 * 627 * The asynchronous methods have a default fallback that uses threads to 628 * implement asynchronicity, so they are optional for inheriting classes. 629 * However, if you override one, you must override all. 630 * 631 * Params: 632 * count = the number of bytes that will be skipped from the stream 633 * ioPriority = the [I/O priority][io-priority] of the request 634 * cancellable = optional #GCancellable object, %NULL to ignore. 635 * callback = callback to call when the request is satisfied 636 * userData = the data to pass to callback function 637 */ 638 public void skipAsync(size_t count, int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 639 { 640 g_input_stream_skip_async(gInputStream, count, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 641 } 642 643 /** 644 * Finishes a stream skip operation. 645 * 646 * Params: 647 * result = a #GAsyncResult. 648 * 649 * Returns: the size of the bytes skipped, or %-1 on error. 650 * 651 * Throws: GException on failure. 652 */ 653 public ptrdiff_t skipFinish(AsyncResultIF result) 654 { 655 GError* err = null; 656 657 auto p = g_input_stream_skip_finish(gInputStream, (result is null) ? null : result.getAsyncResultStruct(), &err); 658 659 if (err !is null) 660 { 661 throw new GException( new ErrorG(err) ); 662 } 663 664 return p; 665 } 666 }