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