1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . IO ;
4
- using System . Linq ;
5
- using System . Text ;
6
3
using System . Threading . Tasks ;
7
4
8
5
namespace Elasticsearch . Net
@@ -11,16 +8,21 @@ public class ResponseBuilder<TReturn>
11
8
where TReturn : class
12
9
{
13
10
private const int BufferSize = 81920 ;
11
+ private static readonly VoidResponse Void = new VoidResponse ( ) ;
12
+ private static readonly IDisposable EmptyDisposable = new MemoryStream ( ) ;
13
+
14
+ private readonly RequestData _requestData ;
15
+ private readonly bool _disableDirectStreaming ;
14
16
15
17
public Exception Exception { get ; set ; }
16
18
public int ? StatusCode { get ; set ; }
17
19
public Stream Stream { get ; set ; }
18
20
19
- private readonly RequestData _requestData ;
20
-
21
21
public ResponseBuilder ( RequestData requestData )
22
22
{
23
23
_requestData = requestData ;
24
+ _disableDirectStreaming =
25
+ this . _requestData . PostData ? . DisableDirectStreaming ?? this . _requestData . ConnectionSettings . DisableDirectStreaming ;
24
26
}
25
27
26
28
public ElasticsearchResponse < TReturn > ToResponse ( )
@@ -53,21 +55,19 @@ private ElasticsearchResponse<TReturn> Initialize(int? statusCode, Exception exc
53
55
return response ;
54
56
}
55
57
56
- private static IDisposable EmptyDisposable = new MemoryStream ( ) ;
57
-
58
58
private void SetBody ( ElasticsearchResponse < TReturn > response , Stream stream )
59
- {
60
- byte [ ] bytes = null ;
61
- if ( NeedsToEagerReadStream ( ) )
62
- {
63
- var inMemoryStream = this . _requestData . MemoryStreamFactory . Create ( ) ;
64
- stream . CopyTo ( inMemoryStream , BufferSize ) ;
65
- bytes = this . SwapStreams ( ref stream , ref inMemoryStream ) ;
59
+ {
60
+ byte [ ] bytes = null ;
61
+ if ( NeedsToEagerReadStream ( ) )
62
+ {
63
+ var inMemoryStream = this . _requestData . MemoryStreamFactory . Create ( ) ;
64
+ stream . CopyTo ( inMemoryStream , BufferSize ) ;
65
+ bytes = this . SwapStreams ( ref stream , ref inMemoryStream ) ;
66
66
}
67
67
68
68
var needsDispose = typeof ( TReturn ) != typeof ( Stream ) ;
69
- using ( needsDispose ? stream : EmptyDisposable )
70
- {
69
+ using ( needsDispose ? stream : EmptyDisposable )
70
+ {
71
71
if ( response . Success )
72
72
{
73
73
if ( ! SetSpecialTypes ( stream , response , bytes ) )
@@ -81,25 +81,25 @@ private void SetBody(ElasticsearchResponse<TReturn> response, Stream stream)
81
81
ServerError serverError ;
82
82
if ( ServerError . TryCreate ( stream , out serverError ) )
83
83
response . ServerError = serverError ;
84
- if ( this . _requestData . ConnectionSettings . DisableDirectStreaming )
84
+ if ( _disableDirectStreaming )
85
85
response . ResponseBodyInBytes = bytes ;
86
86
}
87
87
}
88
88
}
89
-
89
+
90
90
private async Task SetBodyAsync ( ElasticsearchResponse < TReturn > response , Stream stream )
91
- {
92
- byte [ ] bytes = null ;
93
- if ( NeedsToEagerReadStream ( ) )
94
- {
95
- var inMemoryStream = this . _requestData . MemoryStreamFactory . Create ( ) ;
96
- await stream . CopyToAsync ( inMemoryStream , BufferSize , this . _requestData . CancellationToken ) . ConfigureAwait ( false ) ;
97
- bytes = this . SwapStreams ( ref stream , ref inMemoryStream ) ;
91
+ {
92
+ byte [ ] bytes = null ;
93
+ if ( NeedsToEagerReadStream ( ) )
94
+ {
95
+ var inMemoryStream = this . _requestData . MemoryStreamFactory . Create ( ) ;
96
+ await stream . CopyToAsync ( inMemoryStream , BufferSize , this . _requestData . CancellationToken ) . ConfigureAwait ( false ) ;
97
+ bytes = this . SwapStreams ( ref stream , ref inMemoryStream ) ;
98
98
}
99
-
100
- var needsDispose = typeof ( TReturn ) != typeof ( Stream ) ;
101
- using ( needsDispose ? stream : EmptyDisposable )
102
- {
99
+
100
+ var needsDispose = typeof ( TReturn ) != typeof ( Stream ) ;
101
+ using ( needsDispose ? stream : EmptyDisposable )
102
+ {
103
103
if ( response . Success )
104
104
{
105
105
if ( ! SetSpecialTypes ( stream , response , bytes ) )
@@ -108,10 +108,10 @@ private async Task SetBodyAsync(ElasticsearchResponse<TReturn> response, Stream
108
108
else response . Body = await this . _requestData . ConnectionSettings . Serializer . DeserializeAsync < TReturn > ( stream , this . _requestData . CancellationToken ) . ConfigureAwait ( false ) ;
109
109
}
110
110
}
111
- else if ( response . HttpStatusCode != null )
111
+ else if ( response . HttpStatusCode != null )
112
112
{
113
113
response . ServerError = await ServerError . TryCreateAsync ( stream , this . _requestData . CancellationToken ) . ConfigureAwait ( false ) ;
114
- if ( this . _requestData . ConnectionSettings . DisableDirectStreaming )
114
+ if ( _disableDirectStreaming )
115
115
response . ResponseBodyInBytes = bytes ;
116
116
}
117
117
}
@@ -127,7 +127,7 @@ private void Finalize(ElasticsearchResponse<TReturn> response)
127
127
}
128
128
129
129
private bool NeedsToEagerReadStream ( ) =>
130
- this . _requestData . ConnectionSettings . DisableDirectStreaming || typeof ( TReturn ) == typeof ( string ) || typeof ( TReturn ) == typeof ( byte [ ] ) ;
130
+ _disableDirectStreaming || typeof ( TReturn ) == typeof ( string ) || typeof ( TReturn ) == typeof ( byte [ ] ) ;
131
131
132
132
private byte [ ] SwapStreams ( ref Stream responseStream , ref MemoryStream ms )
133
133
{
@@ -142,16 +142,16 @@ private byte[] SwapStreams(ref Stream responseStream, ref MemoryStream ms)
142
142
private bool SetSpecialTypes ( Stream responseStream , ElasticsearchResponse < TReturn > cs , byte [ ] bytes )
143
143
{
144
144
var setSpecial = true ;
145
- if ( this . _requestData . ConnectionSettings . DisableDirectStreaming )
145
+ if ( _disableDirectStreaming )
146
146
cs . ResponseBodyInBytes = bytes ;
147
- var returnType = typeof ( TReturn ) ;
147
+ var returnType = typeof ( TReturn ) ;
148
148
if ( returnType == typeof ( string ) )
149
149
this . SetStringResult ( cs as ElasticsearchResponse < string > , bytes ) ;
150
150
else if ( returnType == typeof ( byte [ ] ) )
151
151
this . SetByteResult ( cs as ElasticsearchResponse < byte [ ] > , bytes ) ;
152
152
else if ( returnType == typeof ( VoidResponse ) )
153
153
this . SetVoidResult ( cs as ElasticsearchResponse < VoidResponse > , responseStream ) ;
154
- else if ( returnType == typeof ( Stream ) )
154
+ else if ( returnType == typeof ( Stream ) )
155
155
this . SetStreamResult ( cs as ElasticsearchResponse < Stream > , responseStream ) ;
156
156
else
157
157
setSpecial = false ;
@@ -164,12 +164,10 @@ private bool SetSpecialTypes(Stream responseStream, ElasticsearchResponse<TRetur
164
164
165
165
private void SetStreamResult ( ElasticsearchResponse < Stream > result , Stream response ) => result . Body = response ;
166
166
167
- private static VoidResponse _void = new VoidResponse ( ) ;
168
-
169
167
private void SetVoidResult ( ElasticsearchResponse < VoidResponse > result , Stream response )
170
168
{
171
169
response . Dispose ( ) ;
172
- result . Body = _void ;
170
+ result . Body = Void ;
173
171
}
174
172
}
175
173
}
0 commit comments