namespace DotNetOpenAuth.ApplicationBlock { using System; using System.Collections.Generic; using System.IO; using DotNetOpenAuth.Messaging; internal static class Util { /// /// Enumerates through the individual set bits in a flag enum. /// /// The flags enum value. /// An enumeration of just the set bits in the flags enum. internal static IEnumerable 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; } /// /// Copies the contents of one stream to another. /// /// The stream to copy from, at the position where copying should begin. /// The stream to copy to, at the position where bytes should be written. /// The total number of bytes copied. /// /// Copying begins at the streams' current positions. /// The positions are NOT reset after copying is complete. /// internal static int CopyTo(this Stream copyFrom, Stream copyTo) { return CopyTo(copyFrom, copyTo, int.MaxValue); } /// /// Copies the contents of one stream to another. /// /// The stream to copy from, at the position where copying should begin. /// The stream to copy to, at the position where bytes should be written. /// The maximum bytes to copy. /// The total number of bytes copied. /// /// Copying begins at the streams' current positions. /// The positions are NOT reset after copying is complete. /// 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; } } }