Azure / Azure/DotNetty

Converting IByteBuffer to byte[] throws IndexOutOfRangeException with UnpooledByteBufferAllocator

Open
#415 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.3k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

We use the following code to convert an IByteBuffer to a byte[] -
```
public byte[] ToByteArray(IByteBuffer byteBuffer)
{
if (!byteBuffer.IsReadable())
{
return new byte[0];
}
using (var stream = new ReadOnlyByteBufferStream(byteBuffer, false))
{
var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
byte[] bytes = memoryStream.ToArray();
return bytes;
}
}
```

But this throws an IndexOutOfRangeException with the following callstack -

at DotNetty.Buffers.UnpooledUnsafeDirectByteBuffer.Addr(Int32 index) in E:\workspace\DotNetty\src\DotNetty.Buffers\UnpooledUnsafeDirectByteBuffer.cs:line 339
at DotNetty.Buffers.UnpooledUnsafeDirectByteBuffer.GetBytes(Int32 index, Byte[] dst, Int32 dstIndex, Int32 length) in E:\workspace\DotNetty\src\DotNetty.Buffers\UnpooledUnsafeDirectByteBuffer.cs:line 211
at DotNetty.Buffers.AbstractByteBuffer.ReadBytes(Byte[] destination, Int32 dstIndex, Int32 length) in E:\workspace\DotNetty\src\DotNetty.Buffers\AbstractByteBuffer.cs:line 998
at DotNetty.Buffers.ReadOnlyByteBufferStream.Read(Byte[] output, Int32 offset, Int32 count) in E:\workspace\DotNetty\src\DotNetty.Buffers\ReadOnlyByteBufferStream.cs:line 46
at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)
at DotnettyTest1.ByteBufferConverter.ToByteArray(IByteBuffer byteBuffer) in C:\Users\vpuranik.REDMOND\Documents\Visual Studio 2017\Projects\DotnettyTest1\DotnettyTest1\ByteBufferConverter.cs:line 34
at DotnettyTest1.Program.ByteBufferTest1() in C:\Users\vpuranik.REDMOND\Documents\Visual Studio 2017\Projects\DotnettyTest1\DotnettyTest1\Program.cs:line 26
at DotnettyTest1.Program.Main(String[] args) in C:\Users\vpuranik.REDMOND\Documents\Visual Studio 2017\Projects\DotnettyTest1\DotnettyTest1\Program.cs:line 11

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.