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