# PooledBufferStream Methods

[Type overview](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/)

## CopyTo(Writer&lt;TBufferWriter&gt;) {#copyto-1-ref-orleans-serialization-buffers-writer-tbufferwriter-d1296e37}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/copyto-1-ref-orleans-serialization-buffers-writer-tbufferwriter-d1296e37/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L87-L94)

```csharp
public void CopyTo<TBufferWriter>(ref Writer<TBufferWriter> writer)
```

Copies the contents of this writer to another writer.

### Parameters

- `writer` (`Writer<TBufferWriter>`)

## Flush {#flush-7e367890}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/flush-7e367890/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L369)

```csharp
public override void Flush()
```

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

### Exceptions

- `System.IO.IOException`: An I/O error occurs.

## Read(byte[], int, int) {#read-byte-array-1-int-int-17f3a7d9}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/read-byte-array-1-int-int-17f3a7d9/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L195-L227)

```csharp
public override int Read(byte[] buffer, int offset, int count)
```

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

### Parameters

- `buffer` (`byte[]`): An array of bytes. When this method returns, the buffer contains the specified byte array with the values between `offset` and (`offset` + `count` - 1) replaced by the bytes read from the current source.
- `offset` (`int`): The zero-based byte offset in `buffer` at which to begin storing the data read from the current stream.
- `count` (`int`): The maximum number of bytes to be read from the current stream.

### Returns

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if `count` is 0 or the end of the stream has been reached.

### Exceptions

- `System.ArgumentException`: The sum of `offset` and `count` is larger than the buffer length.
- `System.ArgumentNullException`: `buffer` is `null`.
- `System.ArgumentOutOfRangeException`: `offset` or `count` is negative.
- `System.IO.IOException`: An I/O error occurs.
- `System.NotSupportedException`: The stream does not support reading.
- `System.ObjectDisposedException`: Methods were called after the stream was closed.

## Rent {#rent-4ac8f6be}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/rent-4ac8f6be/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L43)

```csharp
public static PooledBufferStream Rent()
```

Gets an object from the pool if one is available, otherwise creates one.

### Returns

A [PooledBufferStream](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/).

## RentReadOnlySequence {#rentreadonlysequence-c753f7f5}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/rentreadonlysequence-c753f7f5/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L114-L145)

```csharp
public ReadOnlySequence<byte> RentReadOnlySequence()
```

Returns a new `System.Buffers.ReadOnlySequence-1` which must be used and returned before resetting this instance via the [PooledBufferStream](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/) method.

## Reset {#reset-6ccac0a9}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/reset-6ccac0a9/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L98-L107)

```csharp
public void Reset()
```

## Return(PooledBufferStream) {#return-orleans-serialization-buffers-adaptors-pooledbufferstream-6c3c9a07}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/return-orleans-serialization-buffers-adaptors-pooledbufferstream-6c3c9a07/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L51-L59)

```csharp
public static void Return(PooledBufferStream stream)
```

Return an object to the pool.

### Parameters

- `stream` (`PooledBufferStream`)

## ReturnReadOnlySequence(ReadOnlySequence&lt;byte&gt;) {#returnreadonlysequence-in-system-buffers-readonlysequence-byte-61d47d54}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/returnreadonlysequence-in-system-buffers-readonlysequence-byte-61d47d54/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L153-L164)

```csharp
public void ReturnReadOnlySequence(in ReadOnlySequence<byte> sequence)
```

Returns a `System.Buffers.ReadOnlySequence-1` previously rented by [PooledBufferStream](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/);

### Parameters

- `sequence` (`ReadOnlySequence<byte>`)

## Seek(long, SeekOrigin) {#seek-long-system-io-seekorigin-e76f0d3d}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/seek-long-system-io-seekorigin-e76f0d3d/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L176-L189)

```csharp
public override long Seek(long offset, SeekOrigin origin)
```

When overridden in a derived class, sets the position within the current stream.

### Parameters

- `offset` (`long`): A byte offset relative to the `origin` parameter.
- `origin` (`SeekOrigin`): A value of type `System.IO.SeekOrigin` indicating the reference point used to obtain the new position.

### Returns

The new position within the current stream.

### Exceptions

- `System.IO.IOException`: An I/O error occurs.
- `System.NotSupportedException`: The stream does not support seeking, such as if the stream is constructed from a pipe or console output.
- `System.ObjectDisposedException`: Methods were called after the stream was closed.

## SetLength(long) {#setlength-long-23f04503}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/setlength-long-23f04503/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L232-L281)

```csharp
public override void SetLength(long value)
```

When overridden in a derived class, sets the length of the current stream.

### Parameters

- `value` (`long`): The desired length of the current stream in bytes.

### Exceptions

- `System.IO.IOException`: An I/O error occurs.
- `System.NotSupportedException`: The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.
- `System.ObjectDisposedException`: Methods were called after the stream was closed.

## ToArray {#toarray-b3a14978}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/toarray-b3a14978/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L70-L81)

```csharp
public byte[] ToArray()
```

Returns the data which has been written as an array.

### Returns

The data which has been written.

## Write(byte[], int, int) {#write-byte-array-1-int-int-e20a4a3f}

[Dedicated page](/orleans/docs/api/csharp/microsoft.orleans.serialization/orleans.serialization.buffers.adaptors.pooledbufferstream/methods/write-byte-array-1-int-int-e20a4a3f/)

[Source](https://github.com/dotnet/orleans/blob/e4e752cedecc2976811db49802c7a999b1f1c704/src/Orleans.Serialization/Buffers/Adaptors/PooledBufferStream.cs#L294-L327)

```csharp
public override void Write(byte[] buffer, int offset, int count)
```

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

### Parameters

- `buffer` (`byte[]`): An array of bytes. This method copies `count` bytes from `buffer` to the current stream.
- `offset` (`int`): The zero-based byte offset in `buffer` at which to begin copying bytes to the current stream.
- `count` (`int`): The number of bytes to be written to the current stream.

### Exceptions

- `System.ArgumentException`: The sum of `offset` and `count` is greater than the buffer length.
- `System.ArgumentNullException`: `buffer` is `null`.
- `System.ArgumentOutOfRangeException`: `offset` or `count` is negative.
- `System.IO.IOException`: An I/O error occurred, such as the specified file cannot be found.
- `System.NotSupportedException`: The stream does not support writing.
- `System.ObjectDisposedException`: `System.IO.Stream.Write(System.Byte[],System.Int32,System.Int32)` was called after the stream was closed.
