Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3a1529b6c3
Fetching contributors…

Cannot retrieve contributors at this time

77 lines (70 sloc) 2.936 kb
namespace DotNetOpenAuth.ApplicationBlock {
using System;
using System.Collections.Generic;
using System.IO;
using DotNetOpenAuth.Messaging;
internal static class Util {
/// <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;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.