/// <summary> /// Returns the content of a stream as byte array. /// </summary> /// <param name="stream">The stream.</param> /// <param name="bufferSize">The custom buffer size to use.</param> /// <returns><paramref name="stream" /> as byte array.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="stream" /> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bufferSize" /> is invalid. /// </exception> public static byte[] ToByteArray(this Stream stream, int? bufferSize = null) { if (stream == null) { throw new ArgumentNullException("stream"); } if (bufferSize < 1) { throw new ArgumentOutOfRangeException("stream", bufferSize, "Must be 1 at least!"); } if (stream is MemoryStream) { return ((MemoryStream)stream).ToArray(); } using (var temp = new MemoryStream()) { if (bufferSize.HasValue) { stream.CopyTo(temp, bufferSize.Value); } else { stream.CopyTo(temp); } return temp.ToArray(); } }
internal static void ShouldBeCopiedOK(this IPersistentBufferProvider @this,string iPath) { bool res = @this.CopyTo(iPath); res.Should().BeTrue(); File.Exists(iPath).Should().BeTrue(); IPersistentBufferProvider bc = InternalBufferFactory.GetBufferProviderFromFile(iPath); bc.Should().NotBeNull(); @this.Compare(bc).Should().BeTrue(); @this.CopyTo("").Should().BeFalse(); }
/// <summary> /// 数组转int, /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static Int32 BytesToInt(this Byte[] bytes) { byte[] intBytes = new byte[4]; bytes.CopyTo(intBytes, intBytes.Length - bytes.Length); if (BitConverter.IsLittleEndian) Array.Reverse(intBytes); return BitConverter.ToInt32(intBytes, 0); }
public static void UnpackToDirectory(this ZipInputStream zip, string path, IFileSystem fs) { ZipEntry theEntry; while ((theEntry = zip.GetNextEntry()) != null) { string directoryName = fs.Path.GetDirectoryName(theEntry.Name); string fileName = fs.Path.GetFileName(theEntry.Name); //Create directory for this entry if (!string.IsNullOrEmpty(directoryName)) fs.Directory.CreateDirectory(fs.Path.Combine(path, directoryName)); //If there is no filename this was a directory entry, skip to the next entry if (fileName == String.Empty) continue; //Copy the file from zip into filesystem using (Stream streamWriter = fs.File.Create(fs.Path.Combine(path, theEntry.Name))) { zip.CopyTo(streamWriter); streamWriter.Flush(); } } Thread.Sleep(100); }
public static MemoryStream GetMemoryStream(this Stream stream, int index) { MemoryStream retVal = null; if (stream != null) { MemoryStream wrkStream = null; try { wrkStream = new MemoryStream(); stream.CopyTo(wrkStream, index, stream.Length - index); retVal = wrkStream; retVal.Position = 0; wrkStream = null; } finally { if (wrkStream != null) { wrkStream.Dispose(); } } } return retVal; }
/// <summary> /// Creates a clone and performs a shallow copy of properties from the source object to the destination /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <returns></returns> public static object Clone(this object source, List<String> ExcludeProperties) { Type t = source.GetType(); var clone = Activator.CreateInstance(t); source.CopyTo(clone, ExcludeProperties); return clone; }
/// <summary> /// Returns a <see cref="Stream" /> as a <see cref="MemoryStream" />. /// </summary> /// <param name="stream">The stream to cast / convert.</param> /// <param name="moveToBeginning">Move cursor of result stream to beginning or not.</param> /// <returns>The result stream.</returns> /// <remarks> /// <see langword="null" /> is returned if <paramref name="stream" /> is also <see langword="null" />. /// </remarks> /// <remarks> /// If <paramref name="stream" /> is already a memory stream it is simply casted. /// </remarks> public static MemoryStream AsMemoryStream(this Stream stream, bool moveToBeginning = true) { var result = stream as MemoryStream; if (result == null && stream != null) { result = new MemoryStream(); try { stream.CopyTo(result); } catch { result.Dispose(); throw; } } if (result != null) { if (moveToBeginning) { result.Position = 0; } } return result; }
public static String GetString(this IList<char> chars) { char[] array = new char[chars.Count]; chars.CopyTo(array, 0); chars.Clear(); return new String(array); }
public static byte[] ToBytes(this BitArray array) { var messageBytes = new byte[array.Count]; array.CopyTo(messageBytes, 0); return messageBytes; }
public static IRandomAccessStream AsRandomAccessStream( this Stream stream ) { var randomAccessStream = new InMemoryRandomAccessStream(); var s = randomAccessStream.AsStreamForWrite(); stream.CopyTo(s); return randomAccessStream; }
public static void WriteToFile(this Stream stream, string filePath) { using (var sw = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { stream.CopyTo(sw); } }
public static int ToInteger(this BitArray bits) { var number = new int[1]; bits.CopyTo(number, 0); var numberValue = number[0]; return numberValue; }
public static byte[] AsBytes(this Stream stream) { using (MemoryStream ms = new MemoryStream ()) { stream.CopyTo (ms); return ms.ToArray (); } }
/// <summary> /// Copies the stream. /// </summary> /// <param name="sourceStream">The source stream.</param> /// <param name="destinationStream">The destination stream.</param> public static void CopyStream(this Stream sourceStream, Stream destinationStream) { if (sourceStream != null && destinationStream != null) { sourceStream.CopyTo(destinationStream); } }
/// <summary> /// 将一个BitArray拼接在当前对象之后 /// </summary> /// <param name="current"></param> /// <param name="after"></param> /// <returns></returns> public static BitArray Append(this BitArray current, BitArray after) { var bools = new bool[current.Length + after.Length]; current.CopyTo(bools, 0); after.CopyTo(bools, current.Length); return new BitArray(bools); }
/// <summary> /// 将一个BitArray拼接在当前对象之前 /// </summary> /// <param name="current"></param> /// <param name="before"></param> /// <returns></returns> public static BitArray Prepend(this BitArray current, BitArray before) { var bools = new bool[current.Length + before.Length]; before.CopyTo(bools, 0); current.CopyTo(bools, before.Length); return new BitArray(bools); }
/// <summary> /// Returns stream’s contents as an array of bytes. /// </summary> public static byte[] ReadToEnd(this Stream stream) { Contract.Requires<ArgumentNullException>(stream != null); long? originalPosition = null; if (stream.CanSeek) { originalPosition = stream.Position; stream.Position = 0; } try { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); return ms.ToArray(); } } finally { if (stream.CanSeek && originalPosition.HasValue && originalPosition.Value >= 0) { stream.Position = originalPosition.Value; } } }
public static void CopyTo(this FileInfo source, DirectoryInfo destination, Boolean overwrite) { if(source == null) throw new ArgumentNullException("source"); if(destination.Exists == false) destination.Create(); source.CopyTo(Path.Combine(destination.FullName, source.Name), overwrite); }
public static byte[] GetAllBytes(this Stream stream) { using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); return memoryStream.ToArray(); } }
public static byte ToByte(this BitArray bits) { if (bits.Count != 8) throw new Exception("A byte has 8 bits, wtf you only passed in " + bits.Count); byte[] bytes = new byte[1]; bits.CopyTo(bytes, 0); return bytes[0]; }
/// <summary> /// Convert BitArray to binary digit Int32 array. /// </summary> /// <param name="source">The source BitArray.</param> /// <returns>Binary digit Int32 array.</returns> public static int[] ToBitIntArray(this BitArray source) { int[] result = new int[source.Length]; source.CopyTo(result, 0); return result; }
/// <summary> /// Convert BitArray to bool array. /// </summary> /// <param name="source">The source BitArray.</param> /// <returns>bool array.</returns> public static bool[] ToBoolArray(this BitArray source) { bool[] result = new bool[source.Length]; source.CopyTo(result, 0); return result; }
/// <summary> /// Convert BitArray to byte array. /// </summary> /// <param name="source">The source BitArray.</param> /// <returns>Byte array.</returns> public static byte[] ToByteArray(this BitArray source) { byte[] result = new byte[((source.Length - 1) / 8) + 1]; source.CopyTo(result, 0); return result; }
public static byte[] ToByteArray(this BitArray value) { var length = (int)System.Math.Ceiling(value.Count / 8d); var list = new byte[length]; value.CopyTo(list, 0); return list; }
public static MemoryStream ToMemoryStream(this Stream stream) { var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); return memoryStream; }
// Statyczna metoda zamienia ciąg wejściowy Stream na tablice bajtów a więc odczytuje // cały plik i zapisuje w tablicy. Ułatwia to dalszą obróbkę zdjęcia, ponieważ // nie pracujemy już na strumieniu danych, ale na całym już wczytanym zdjęciu. public static byte[] ReadFully(this Stream input) { using (MemoryStream ms = new MemoryStream()) { input.CopyTo(ms); return ms.ToArray(); } }
public static byte[] ReadToEnd(this Stream stream) { using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); return memoryStream.ToArray(); } }
/// <summary> /// Extends CopyTo so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// stringbuilder.CopyTo(sourceIndex, destination); /// </example> /// </summary> public static void CopyTo(this StringBuilder stringbuilder, Int32 sourceIndex, Char[] destination) { if(stringbuilder == null) throw new ArgumentNullException("stringbuilder"); if(destination == null) throw new ArgumentNullException("destination"); stringbuilder.CopyTo(sourceIndex, destination, 0, destination.Length); }
public static byte[] ToByteArray(this Stream input) { using (var memoryStream = new MemoryStream()) { input.CopyTo(memoryStream); return memoryStream.ToArray(); } }
/// <summary> /// Copies any stream into a local MemoryStream /// </summary> /// <param name="stream">The source stream.</param> /// <returns>The copied memory stream.</returns> public static MemoryStream CopyToMemory(this Stream stream) { var memoryStream = new MemoryStream((int)stream.Length); stream.CopyTo(memoryStream); return memoryStream; }