-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpJsonContent.cs
161 lines (134 loc) · 5.23 KB
/
HttpJsonContent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Storage.Streams;
using Windows.Web.Http;
using Windows.Web.Http.Headers;
namespace RoomController
{
class HttpJsonContent : IHttpContent
{
IJsonValue jsonValue;
HttpContentHeaderCollection headers;
public HttpContentHeaderCollection Headers
{
get
{
return headers;
}
}
public HttpJsonContent(IJsonValue jsonValue)
{
if (jsonValue == null)
{
throw new ArgumentException("jsonValue cannot be null.");
}
this.jsonValue = jsonValue;
headers = new HttpContentHeaderCollection();
headers.ContentType = new HttpMediaTypeHeaderValue("application/json");
headers.ContentType.CharSet = "UTF-8";
}
public IAsyncOperationWithProgress<ulong, ulong> BufferAllAsync()
{
return AsyncInfo.Run<ulong, ulong>((cancellationToken, progress) =>
{
return Task<ulong>.Run(() =>
{
ulong length = GetLength();
// Report progress.
progress.Report(length);
// Just return the size in bytes.
return length;
});
});
}
public IAsyncOperationWithProgress<IBuffer, ulong> ReadAsBufferAsync()
{
return AsyncInfo.Run<IBuffer, ulong>((cancellationToken, progress) =>
{
return Task<IBuffer>.Run(() =>
{
DataWriter writer = new DataWriter();
writer.WriteString(jsonValue.Stringify());
// Make sure that the DataWriter destructor does not free the buffer.
IBuffer buffer = writer.DetachBuffer();
// Report progress.
progress.Report(buffer.Length);
return buffer;
});
});
}
public IAsyncOperationWithProgress<IInputStream, ulong> ReadAsInputStreamAsync()
{
return AsyncInfo.Run<IInputStream, ulong>(async (cancellationToken, progress) =>
{
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream);
writer.WriteString(jsonValue.Stringify());
uint bytesStored = await writer.StoreAsync().AsTask(cancellationToken);
// Make sure that the DataWriter destructor does not close the stream.
writer.DetachStream();
// Report progress.
progress.Report(randomAccessStream.Size);
return randomAccessStream.GetInputStreamAt(0);
});
}
public IAsyncOperationWithProgress<string, ulong> ReadAsStringAsync()
{
return AsyncInfo.Run<string, ulong>((cancellationToken, progress) =>
{
return Task<string>.Run(() =>
{
string jsonString = jsonValue.Stringify();
// Report progress (length of string).
progress.Report((ulong)jsonString.Length);
return jsonString;
});
});
}
public bool TryComputeLength(out ulong length)
{
length = GetLength();
return true;
}
public IAsyncOperationWithProgress<ulong, ulong> WriteToStreamAsync(IOutputStream outputStream)
{
return AsyncInfo.Run<ulong, ulong>(async (cancellationToken, progress) =>
{
DataWriter writer = new DataWriter(outputStream);
writer.WriteString(jsonValue.Stringify());
uint bytesWritten = await writer.StoreAsync().AsTask(cancellationToken);
// Make sure that DataWriter destructor does not close the stream.
writer.DetachStream();
// Report progress.
progress.Report(bytesWritten);
return bytesWritten;
});
}
public void Dispose()
{
}
private ulong GetLength()
{
DataWriter writer = new DataWriter();
writer.WriteString(jsonValue.Stringify());
IBuffer buffer = writer.DetachBuffer();
return buffer.Length;
}
}
}