Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
3d057200
Commit
3d057200
authored
Sep 03, 2018
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
do a much better job of ripping the inner buffer from MemoryStream
parent
c767a3f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
2 deletions
+41
-2
RedisValue.cs
src/StackExchange.Redis/RedisValue.cs
+23
-1
Values.cs
tests/StackExchange.Redis.Tests/Values.cs
+18
-1
No files found.
src/StackExchange.Redis/RedisValue.cs
View file @
3d057200
using
System
;
using
System.Buffers
;
using
System.IO
;
using
System.Reflection
;
using
System.Runtime.InteropServices
;
using
System.Text
;
...
...
@@ -806,7 +807,7 @@ public static RedisValue CreateFrom(MemoryStream stream)
{
if
(
stream
==
null
)
return
Null
;
if
(
stream
.
Length
==
0
)
return
Array
.
Empty
<
byte
>();
if
(
stream
.
TryGetBuffer
(
out
var
segment
))
if
(
stream
.
TryGetBuffer
(
out
var
segment
)
||
ReflectionTryGetBuffer
(
stream
,
out
segment
)
)
{
return
new
Memory
<
byte
>(
segment
.
Array
,
segment
.
Offset
,
segment
.
Count
);
}
...
...
@@ -817,6 +818,27 @@ public static RedisValue CreateFrom(MemoryStream stream)
}
}
private
static
readonly
FieldInfo
s_origin
=
typeof
(
MemoryStream
).
GetField
(
"_origin"
,
BindingFlags
.
NonPublic
|
BindingFlags
.
Instance
),
s_buffer
=
typeof
(
MemoryStream
).
GetField
(
"_buffer"
,
BindingFlags
.
NonPublic
|
BindingFlags
.
Instance
);
private
static
bool
ReflectionTryGetBuffer
(
MemoryStream
ms
,
out
ArraySegment
<
byte
>
buffer
)
{
if
(
s_origin
!=
null
&&
s_buffer
!=
null
)
{
try
{
int
offset
=
(
int
)
s_origin
.
GetValue
(
ms
);
byte
[]
arr
=
(
byte
[])
s_buffer
.
GetValue
(
ms
);
buffer
=
new
ArraySegment
<
byte
>(
arr
,
offset
,
checked
((
int
)
ms
.
Length
));
return
true
;
}
catch
{
}
}
buffer
=
default
;
return
false
;
}
/// <summary>
/// Indicates whether the current value has the supplied value as a prefix.
/// </summary>
...
...
tests/StackExchange.Redis.Tests/Values.cs
View file @
3d057200
using
System.
Linq
;
using
System.
IO
;
using
System.Text
;
using
Xunit
;
using
Xunit.Abstractions
;
...
...
@@ -30,5 +30,22 @@ public void NullValueChecks()
Assert
.
False
(
emptyArr
.
HasValue
);
Assert
.
True
(
emptyArr
.
IsNullOrEmpty
);
}
[
Fact
]
public
void
FromStream
()
{
var
arr
=
Encoding
.
UTF8
.
GetBytes
(
"hello world"
);
var
ms
=
new
MemoryStream
(
arr
);
var
val
=
RedisValue
.
CreateFrom
(
ms
);
Assert
.
Equal
(
"hello world"
,
(
string
)
val
);
ms
=
new
MemoryStream
(
arr
,
1
,
6
,
false
,
false
);
val
=
RedisValue
.
CreateFrom
(
ms
);
Assert
.
Equal
(
"ello w"
,
(
string
)
val
);
ms
=
new
MemoryStream
(
arr
,
2
,
6
,
false
,
true
);
val
=
RedisValue
.
CreateFrom
(
ms
);
Assert
.
Equal
(
"llo wo"
,
(
string
)
val
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment