[#BloombergLP-bdls-FdStreamBuf]
= xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bdls.adoc[bdls]::FdStreamBuf
:relfileprefix: ../../
:mrdocs:

This class, derived from the C&plus;&plus; standard library class `bsl&colon;&colon;streambuf`, is a mechanism that can be associated with an opened file descriptor, and, except for changing the locale, enables the caller to invoke all the standard `bsl&colon;&colon;streambuf` operations on that file descriptor&period; Note that objects of this class are always in exactly one of the 5 modes outlined in the enum `FdStreamBuf&colon;&colon;FdStreamBufMode`&period;

== Synopsis

Declared in `&lt;bdls&lowbar;fdstreambuf&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class FdStreamBuf
    : public std::streambuf
----

== Base Classes

[cols="1,4"]
|===
| Name| Description
| `std::streambuf`
| 
|===

== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bdls/FdStreamBuf/2constructor.adoc[`FdStreamBuf`]         [.small]#[constructor]#
| Create a `FdStreamBuf` associated with the specified `fileDescriptor` that refers to an already opened file or device, and specify `writableFlag` which, if `true`, indicates that `fileDescriptor` is writable, otherwise it is not&period; The optionally specified `willCloseOnResetFlag`, if `true`, indicates that `fileDescriptor` is to be closed the next time this object is reset, cleared or destroyed, or if `false` the file descriptor is to be left open&period; Optionally specify a `binaryModeFlag` which is ignored on Unix&semi; if `false` on Windows, it indicates that &grave;
&grave;s are to be translated to and from &grave;&bsol;r
` sequences on the device&period; Optionally specify a `basicAllocator&grave; used to supply memory&period; If `basicAllocator` is 0, the currently installed default allocator is used&period; The supplied file descriptor, if valid, should remain open until the next call to either `reset` or the destructor&period; Note that if `FilesystemUtil&colon;&colon;k&lowbar;INVALID&lowbar;FD` is passed to `fileDescriptor`, no file descriptor is to be associated with this object&period; Also note that the state of the `fileDescriptor` is unchanged by this call (i&period;e&period;, there is no implicit seek)&period;
| xref:BloombergLP/bdls/FdStreamBuf/2destructor.adoc[`&#126;FdStreamBuf`] [.small]#[destructor]# [.small]#[virtual]#
| Destroy this object and, if `willCloseOnReset` is `true`, close the file descriptor associated with this object, if any&period;
| xref:BloombergLP/bdls/FdStreamBuf/clear.adoc[`clear`] 
| Release any file descriptor that may be associated with this file handler&period; If `isOpened` and `willCloseOnReset` are both `true`, the file descriptor will be closed, otherwise it will not&period; Return 0 on success, and a non&hyphen;zero value if the close fails&period; This method succeeds with no effect if `isOpened` was false&period; Note that `fileDescriptor` is `FilesystemUtil&colon;&colon;k&lowbar;INVALID&lowbar;FD` after this call&period;
| xref:BloombergLP/bdls/FdStreamBuf/fileDescriptor.adoc[`fileDescriptor`] 
| Return the file descriptor associated with this object, or `FilesystemUtil&colon;&colon;k&lowbar;INVALID&lowbar;FD` if this object is not currently associated with a file descriptor&period;
| xref:BloombergLP/bdls/FdStreamBuf/isOpened.adoc[`isOpened`] 
| Return `true` if this object is currently associated with a file descriptor, and `false` otherwise&period;
| xref:BloombergLP/bdls/FdStreamBuf/release.adoc[`release`] 
| Disassociate this file handler from any file descriptor with which it may be associated without closing that file descriptor&period; This method succeeds with no effect is `isOpened` was false&period; Note that `fileDescriptor` is `FilesystemUtil&colon;&colon;k&lowbar;INVALID&lowbar;FD` after this call&period;
| xref:BloombergLP/bdls/FdStreamBuf/reset.adoc[`reset`] 
| Associate this object with the specified `fileDescriptor`, and record the state of the specified `writableFlag` which, if `true`, indicates that `fileDescriptor` is writable, otherwise it is not&period; If, in the call to the constructor or `reset` prior to this call, `willCloseOnReset` was true, close any file descriptor previously associated with this object, otherwise leave it open but disassociate this object from it&period; The Optionally specified `willCloseOnResetFlag` which will set `willCloseOnReset`, which, if `true`, indicates that the specified file descriptor is to be closed when this object is later cleared, reset, or destroyed, otherwise no action will be taken on `fileDescriptor` at that time&period; The supplied file descriptor, if valid, should remain open until the next call to either `reset` or the destructor, regardless of the value of `willCloseOnResetFlag`&period; Optionally specify a `binaryModeFlag`, which is ignored on Unix&semi; if `false` on Windows, it indicates that &grave;
&grave;s internally are to be translated to and from &grave;&bsol;r
&grave; sequences on the device&semi; on Unix or if `binaryModeFlag` is `true` no such translation is to occur&period; Return 0 on success, and a non&hyphen;zero value otherwise&period; Note that if `FilesystemUtil&colon;&colon;k&lowbar;INVALID&lowbar;FD` is passed as `fileDescriptor`, no file descriptor is to be associated with this object&period; Also note that the state of the `fileDescriptor` is unchanged by this call, there is no implicit seek&period;
| xref:BloombergLP/bdls/FdStreamBuf/willCloseOnReset.adoc[`willCloseOnReset`] 
| Return `true` if this object will close the associated file descriptor the next time it is reset, cleared, or destroyed, and `false` otherwise&period;
|===


== Protected Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:BloombergLP/bdls/FdStreamBuf/imbue.adoc[`imbue`]  [.small]#[virtual]#
| Set the locale for this object&period; This method has no effect&period; The behavior is undefined unless the specified `locale` is the same as `bsl&colon;&colon;locale()`&period;
| xref:BloombergLP/bdls/FdStreamBuf/overflow.adoc[`overflow`]  [.small]#[virtual]#
| If in output mode, write the contents of the buffer to output&period; Return `traits&lowbar;type&colon;&colon;eof()` on failure, and any other value on success&period; Optionally specify a character `c` to be appended to the buffer prior to the flush&period; If no character is specified, no character is appended to the buffer&period; If not in output mode, switch to output mode&period; Note that the write will translate &grave; `s to `r
&grave;s&period;
| xref:BloombergLP/bdls/FdStreamBuf/pbackfail.adoc[`pbackfail`]  [.small]#[virtual]#
| If the optionally specified `c` is not given, move the current input position back one character and return the character at that position&period; Otherwise specify a value for `c` other than `traits&lowbar;type&colon;&colon;eof`&period; If `c` is equal to the previous character in the read buffer, the behavior is the same as if `eof()` was passed&period; If `c` is not eof and is not equal to the previous character in the putback buffer push the character `c` is back into the input buffer, if possible&period; Return the backed up character on success and `traits&lowbar;type&colon;&colon;eof()` otherwise&period; If the input buffer is readonly, or `gptr()` is already at the beginning of the input buffer, this object enters `INPUT&lowbar;PUTBACK&lowbar;MODE` and `c` is stuffed back into the putback buffer&period; Note that only `PBACK&lowbar;BUF&lowbar;SIZE` characters can be backed up into the putback buffer, if this limit is exceeded, `traits&lowbar;type&colon;&colon;eof()` will be returned&period; Also note that this method is called by public methods `sputbackc` or `sungetc` in the base class, and only when simply decrementing the current position in the input buffer won&apos;t satisfy the request, either because `c` doesn&apos;t match the previously input character, or because the input position is already at the beginning of the input buffer&period;
| xref:BloombergLP/bdls/FdStreamBuf/seekoff.adoc[`seekoff`]  [.small]#[virtual]#
| Set the file pointer associated with the file descriptor according to the specified `offset` and `whence`&colon;
| xref:BloombergLP/bdls/FdStreamBuf/seekpos.adoc[`seekpos`]  [.small]#[virtual]#
| Seek to the specified `offset` relative to the beginning of the file&period; Return the resulting absolute position in the file relative to the beginning&period; Optionally specify `mode` which is ignored&period; Also note that seeks are always in terms of bytes on the device, meaning that on a Windows text file, seeking past a &grave;&bsol;r &grave; sequence on the disk will count as two bytes, though if it is read in it will be a single &grave; &grave; byte&period;
| xref:BloombergLP/bdls/FdStreamBuf/setbuf.adoc[`setbuf`]  [.small]#[virtual]#
| Use the specified `buffer` of the specified `numBytes` capacity as the input&sol;output buffer for this `streambuf`&period; If `buffer &equals;&equals; 0`, the buffer is dynamically allocated with a default size&period; If both `buffer` and `numBytes` are zero a 1&hyphen;byte buffer is dynamically allocated&period; The behavior is undefined if any I&sol;O has preceded this call, and unless the buffer is uninitialized before this call&period;
| xref:BloombergLP/bdls/FdStreamBuf/showmanyc.adoc[`showmanyc`]  [.small]#[virtual]#
| If this object is in putback mode, return the number of characters remaining to be read in the putback buffer, and otherwise the number of characters remaining in the file to be read&period; Return a non&hyphen;negative number of characters on success and a negative value otherwise&period; The behavior is undefined unless this object is in input mode and the file descriptor is associated with a regular file&period;
| xref:BloombergLP/bdls/FdStreamBuf/sync.adoc[`sync`]  [.small]#[virtual]#
| If in output mode, flush the buffer to the associated file descriptor&semi; otherwise do nothing&period; Return 0 on success, &hyphen;1 otherwise&period;
| xref:BloombergLP/bdls/FdStreamBuf/underflow.adoc[`underflow`]  [.small]#[virtual]#
| Replenish the input buffer with data obtained from the file descriptor, and return the next character of input (or eof if no input is available)&period; Note that in windows text mode, &grave;&bsol;r &grave; sequences on the device will be translated to &grave; &grave;s&period;
| xref:BloombergLP/bdls/FdStreamBuf/xsgetn.adoc[`xsgetn`]  [.small]#[virtual]#
| Read up to the specified `numBytes` characters from the file descriptor into the specified `buffer` and return the number of characters successfully read&period; The behavior is undefined unless `buffer` is at least `numBytes` bytes long&period; Note that on a Windows text file, a &grave;&bsol;r ` in the file will be read as `
&grave; (counting as a single character)&period;
| xref:BloombergLP/bdls/FdStreamBuf/xsputn.adoc[`xsputn`]  [.small]#[virtual]#
| Write up to the specified `numBytes` characters from the specified `buffer` and return the number of characters successfully written&period; Note that this method does not necessarily modify the file&colon; this method may simply write the characters to a buffer to be flushed to the file at a later time&period; Also note that on a Windows text file, a &grave; &grave; will be written to the file as &grave;&bsol;r &grave; (counted as a single character)&period;
|===



[.small]#Created with https://www.mrdocs.com[MrDocs]#
