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