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
|
namespace DotNetOpenAuth.ApplicationBlock {
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using DotNetOpenAuth.Messaging;
public static class Util {
/// <summary>
/// Pseudo-random data generator.
/// </summary>
internal static readonly Random NonCryptoRandomDataGenerator = new Random();
/// <summary>
/// Enumerates through the individual set bits in a flag enum.
/// </summary>
/// <param name="flags">The flags enum value.</param>
/// <returns>An enumeration of just the <i>set</i> bits in the flags enum.</returns>
internal static IEnumerable<long> GetIndividualFlags(Enum flags) {
long flagsLong = Convert.ToInt64(flags);
for (int i = 0; i < sizeof(long) * 8; i++) { // long is the type behind the largest enum
// Select an individual application from the scopes.
long individualFlagPosition = (long)Math.Pow(2, i);
long individualFlag = flagsLong & individualFlagPosition;
if (individualFlag == individualFlagPosition) {
yield return individualFlag;
}
}
}
internal static Uri GetCallbackUrlFromContext() {
Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_");
return callback;
}
/// <summary>
/// Copies the contents of one stream to another.
/// </summary>
/// <param name="copyFrom">The stream to copy from, at the position where copying should begin.</param>
/// <param name="copyTo">The stream to copy to, at the position where bytes should be written.</param>
/// <returns>The total number of bytes copied.</returns>
/// <remarks>
/// Copying begins at the streams' current positions.
/// The positions are NOT reset after copying is complete.
/// </remarks>
internal static int CopyTo(this Stream copyFrom, Stream copyTo) {
return CopyTo(copyFrom, copyTo, int.MaxValue);
}
/// <summary>
/// Copies the contents of one stream to another.
/// </summary>
/// <param name="copyFrom">The stream to copy from, at the position where copying should begin.</param>
/// <param name="copyTo">The stream to copy to, at the position where bytes should be written.</param>
/// <param name="maximumBytesToCopy">The maximum bytes to copy.</param>
/// <returns>The total number of bytes copied.</returns>
/// <remarks>
/// Copying begins at the streams' current positions.
/// The positions are NOT reset after copying is complete.
/// </remarks>
internal static int CopyTo(this Stream copyFrom, Stream copyTo, int maximumBytesToCopy) {
if (copyFrom == null) {
throw new ArgumentNullException("copyFrom");
}
if (copyTo == null) {
throw new ArgumentNullException("copyTo");
}
byte[] buffer = new byte[1024];
int readBytes;
int totalCopiedBytes = 0;
while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(1024, maximumBytesToCopy))) > 0) {
int writeBytes = Math.Min(maximumBytesToCopy, readBytes);
copyTo.Write(buffer, 0, writeBytes);
totalCopiedBytes += writeBytes;
maximumBytesToCopy -= writeBytes;
}
return totalCopiedBytes;
}
}
}
|