internal static byte[] StreamToBuffer (Stream assemblyStream) { byte [] buffer; using (assemblyStream) { // avoid extra step for MemoryStream (but not any stream that inherits from it) if (assemblyStream.GetType () == MemoryStreamType) return (assemblyStream as MemoryStream).ToArray (); // it's normally bad to depend on Stream.Length since some stream (e.g. NetworkStream) // don't implement them. However it is safe in this case (i.e. SL2 depends on Length too) buffer = new byte [assemblyStream.Length]; int length = buffer.Length; int offset = 0; while (length > 0) { int read = assemblyStream.Read (buffer, offset, length); if (read == 0) break; length -= read; offset += read; } } return buffer; }
internal static bool IsFileStream(Stream stream) { if (FileStreamType.Value == null) { return false; } var type = stream.GetType(); return type == FileStreamType.Value || type.GetTypeInfo().IsSubclassOf(FileStreamType.Value); }
public static void CreateDiskImage(System.IO.Stream s, FATFormatInfo fi) { if (s.GetType() == typeof(System.IO.FileStream)) { CreateDiskImage((System.IO.FileStream)s, fi); return; } s.SetLength(0); FileCompression.WriteZeros(s, 0, (long)fi.TotalSectors * 512); FormatDiskImage(s, fi, false); }
/// <summary> /// Returns the stream as an array. /// </summary> public byte[] ToArray() { if (Stream.GetType() == typeof(FileStream)) { Stream.Position = 0; byte[] buffer = new byte[Stream.Length]; Stream.Read(buffer, 0, buffer.Length); return(buffer); } return(((MemoryStream)Stream).ToArray()); }
public AviWriter(System.IO.Stream outputAvi, string fourCC, int width, int height, float fps) { // RIFFファイルは、RIFFヘッダーとその後ろに続く 0個以上のリストとチャンクで構成されている。 // RIFFヘッダーは、'RIFF'のFOURCC、4バイトのデータサイズ、データを識別するFOURCC、データから構成されている。 // リストは、'LIST'のFOURCC、4バイトのデータサイズ、データを識別するFOURCC、データから構成されている。 // チャンクは、データを識別するFOURCC、4バイトのデータサイズ、データから構成されている。 // チャンクデータを識別するFOURCCは、2桁のストリーム番号とその後に続く2文字コード(dc=ビデオ,wb=音声,tx=字幕など)で構成されている。 // AVIファイルは、'AVI 'のFOURCCと、2つの必須のLISTチャンク('hdrl''movi')、オプションのインデックスチャンクから構成されるRIFFファイルである。 var riffFile = new RiffFile(outputAvi, "AVI "); // hdrlリストを仮のフレーム数で作成 var hdrlList = riffFile.CreateList("hdrl"); WriteHdrlList(hdrlList, fourCC, width, height, fps, 0); hdrlList.Close(); // moviリストを作成し、OnAddImageごとにデータチャンクを追加 var idx1List = new List <Idx1Entry>(); var moviList = riffFile.CreateList("movi"); this.OnAddImage += (data, keyFrame) => { var idx1 = WriteMoviList(moviList, "00dc", data); idx1.KeyFrame = keyFrame; idx1List.Add(idx1); }; // ファイルをクローズ this.OnClose += () => { // moviリストを閉じる moviList.Close(); // idx1チャンクを作成 WriteIdx1Chunk(riffFile, idx1List); // hdrlListを正しいフレーム数で上書き var offset = hdrlList.Offset; riffFile.BaseStream.Seek(offset, System.IO.SeekOrigin.Begin); // hdrlリストの先頭まで戻る riffFile.BaseStream.Seek(12, System.IO.SeekOrigin.Current); // hdrlリストのヘッダ分飛ばす WriteHdrlList(riffFile, fourCC, width, height, fps, idx1List.Count); // hdrlリストのデータを正しいフレーム数で上書き riffFile.BaseStream.Seek(0, System.IO.SeekOrigin.End); // 元の場所に戻る // ファイルをクローズ riffFile.Close(); if (outputAvi.GetType() == typeof(MemoryStream)) { finishedData = ((MemoryStream)outputAvi).ToArray(); } outputAvi.Dispose(); }; }
public Stream() { var pitch = 12; var height = 2; var size = pitch * height; SurfaceStream = FormatterServices.GetUninitializedObject(typeof(SurfaceStream)) as SurfaceStream; var fieldPtr = SurfaceStream.GetType().GetField("_ptr", BindingFlags.Instance | BindingFlags.NonPublic); var fieldLength = SurfaceStream.GetType().GetField("_length", BindingFlags.Instance | BindingFlags.NonPublic); var fieldSurface = SurfaceStream.GetType().GetField("_surface", BindingFlags.Instance | BindingFlags.NonPublic); var handle = new ObjectHandle(); var data = Marshal.AllocHGlobal(size); for (var i = 0; i < size; ++i) { Marshal.WriteByte(data, i, 0); } fieldPtr.SetValue(SurfaceStream, data); fieldLength.SetValue(SurfaceStream, size); fieldSurface.SetValue(SurfaceStream, handle); }
public static void WriteZeros(System.IO.Stream s, long begin, long end) { if (s.GetType() == typeof(System.IO.FileStream)) { WriteZeros((System.IO.FileStream)s, begin, end); } else { for (long t = begin; t < end; t += 512) { s.Write(sector, 0, (int)Math.Min(512, end - t)); } s.Position = end; } }
private static bool OverridesBeginMethod(Stream stream, string methodName) { // Get all of the methods on the underlying stream var methods = stream.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance); // If any of the methods have the desired name and are defined on the base Stream // Type, then the method was not overridden. If none of them were defined on the // base Stream, then it must have been overridden. foreach (var method in methods) { if (method.DeclaringType == typeof(Stream) && method.Name == methodName) { return(false); } } return(true); }
public static MemoryStream ToMemoryStream(this System.IO.Stream stream) { if (stream.GetType() == typeof(MemoryStream)) { return((MemoryStream)stream); } MemoryStream memoryStream = new MemoryStream(); byte[] buffer = new byte[4096]; int numBytesRead; while ((numBytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { memoryStream.Write(buffer, 0, numBytesRead); } return(memoryStream); }
public override System.IO.Stream ChainStream(System.IO.Stream stream) { Stream ret; if (FirstPhase && stream.GetType().Name == "SoapExtensionStream") { ret = base.ChainStream(stream); } else if (!FirstPhase && oldStream == null) { ret = base.ChainStream(stream); } else { oldStream = stream; newStream = new MemoryStream(); ret = newStream; } FirstPhase = false; return(ret); }
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) { if (input == null) { throw new ArgumentNullException(nameof(input)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (!input.CanRead) { throw new ArgumentException(SR.Argument_StreamNotReadable); } _stream = input; _decoder = encoding.GetDecoder(); _maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize); int minBufferSize = encoding.GetMaxByteCount(1); // max bytes per one char if (minBufferSize < 16) { minBufferSize = 16; } _buffer = new byte[minBufferSize]; // _charBuffer and _charBytes will be left null. // For Encodings that always use 2 bytes per char (or more), // special case them here to make Read() & Peek() faster. _2BytesPerChar = encoding is UnicodeEncoding; // check if BinaryReader is based on MemoryStream, and keep this for it's life // we cannot use "as" operator, since derived classes are not allowed _isMemoryStream = (_stream.GetType() == typeof(MemoryStream)); _leaveOpen = leaveOpen; Debug.Assert(_decoder != null, "[BinaryReader.ctor]_decoder!=null"); }
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) { if (input == null) { throw new ArgumentNullException("input"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (!input.CanRead) { throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotReadable")); } Contract.EndContractBlock(); m_stream = input; m_decoder = encoding.GetDecoder(); m_maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize); int minBufferSize = encoding.GetMaxByteCount(1); // max bytes per one char if (minBufferSize < 16) { minBufferSize = 16; } m_buffer = new byte[minBufferSize]; // m_charBuffer and m_charBytes will be left null. // For Encodings that always use 2 bytes per char (or more), // special case them here to make Read() & Peek() faster. m_2BytesPerChar = encoding is UnicodeEncoding; // check if BinaryReader is based on MemoryStream, and keep this for it's life // we cannot use "as" operator, since derived classes are not allowed m_isMemoryStream = (m_stream.GetType() == typeof(MemoryStream)); m_leaveOpen = leaveOpen; Contract.Assert(m_decoder != null, "[BinaryReader.ctor]m_decoder!=null"); }
// GetSubStream returns a 'copy' of the InputStream with Position set to 0. static Stream GetSubStream (Stream stream) { #if !TARGET_JVM if (stream is IntPtrStream) return new IntPtrStream (stream); #endif if (stream is MemoryStream) { MemoryStream other = (MemoryStream) stream; return new MemoryStream (other.GetBuffer (), 0, (int) other.Length, false, true); } if (stream is TempFileStream) { ((TempFileStream) stream).SavePosition (); return stream; } throw new NotSupportedException ("The stream is " + stream.GetType ()); }