public void TestException() { Assert.Throws <ArgumentNullException>(() => StreamExtension.AppendBytes(null, SnmpType.Counter32, null, null)); Assert.Throws <ArgumentNullException>(() => new MemoryStream().AppendBytes(SnmpType.Counter32, null, null)); Assert.Throws <ArgumentNullException>(() => StreamExtension.IgnoreBytes(null, 0)); Assert.Throws <ArgumentNullException>(() => StreamExtension.ReadPayloadLength(null)); }
/// <summary> /// Defines the entry point of the application. /// </summary> private static void Main() { #region Memory leak example 1: Streams are not being close var source = ConfigurationManager.AppSettings["sourceFilePath"]; var destination = ConfigurationManager.AppSettings["destinationFilePath"]; Console.WriteLine($"Copying completed. Total bytes: {StreamExtension.ByByteCopy(source, destination)}\n"); #endregion #region Memory leak example 2: Subscribing to events, and aren't being unsubscribe from event, before calling .Dispose() Clock countdown = new Clock(10); var falconHeavy = new Rocket(countdown); falconHeavy.Subscribe(); countdown.StartCountdown(); //falconHeavy.Dispose(); #endregion #region Memory leak example 3: Allocating unmanaged resources and not freeing them after var pointer = WorkWithBitmap.GetHbitmapDemo("image.jpg"); //WorkWithBitmap.DeleteObject(pointer); #endregion Console.ReadLine(); }
public static byte[] SynthesizeToByteArray(string text) { TextToSpeechService _textToSpeech = new TextToSpeechService(); _textToSpeech.SetCredential(_username, _password); var sample = _textToSpeech.Synthesize(text, Voice.ES_SOFIA, AudioType.WAV); byte[] result = StreamExtension.ReadAllBytes(sample); return(result); }
public override ScriptData Read(string name, Stream stream) { using (var file = new BinaryReader(stream, Encodings.cp932, true)) { uint signature = file.ReadUInt32(); if (signature != file.BaseStream.Length) { return(null); } uint header_size = file.ReadUInt32(); if (header_size > 0x24 || header_size < 0x14) { return(null); } uint code_size = file.ReadUInt32(); uint text_index_size = file.ReadUInt32(); uint text_size = file.ReadUInt32(); byte[] header_data = file.ReadBytes((int)header_size - 0x14); byte[] code = file.ReadBytes((int)code_size); uint[] index = new uint[text_index_size / 4]; for (int i = 0; i < index.Length; ++i) { index[i] = file.ReadUInt32(); if (index[i] >= text_size) { return(null); } } long text_offset = header_size + code_size + text_index_size; var script = new GscScriptData(); script.Header = header_data; script.Code = code; for (int i = 0; i < index.Length; ++i) { file.BaseStream.Position = text_offset + index[i]; string text = StreamExtension.ReadCString(file.BaseStream); script.TextLines.Add(new ScriptLine { Id = (uint)i, Text = text }); } long footer_pos = text_offset + text_size; file.BaseStream.Position = footer_pos; script.Footer = new byte[file.BaseStream.Length - footer_pos]; file.BaseStream.Read(script.Footer, 0, script.Footer.Length); return(script); } }
public static SqlInt64 LastIndexOfRange(SqlBytes input, SqlBytes value, SqlInt64 index, SqlInt64 count) { if (input.IsNull || value.IsNull) { return(SqlInt64.Null); } if (!index.IsNull && (index.Value < 0L || index > input.Length)) { throw new ArgumentOutOfRangeException("index"); } if (!count.IsNull && (count.Value < 0L || count > input.Length - (index.IsNull ? 0L : index.Value))) { throw new ArgumentOutOfRangeException("count"); } if (input.Length == 0L || value.Length > input.Length) { return(new SqlInt64(-1L)); } long indexValue = index.IsNull ? (count.IsNull ? input.Length : count.Value) - 1L : index.Value; long countValue = count.IsNull ? (index.IsNull ? input.Length : index.Value + 1L) : count.Value; switch (input.Storage) { case StorageState.Buffer: return(new SqlInt64(input.Buffer.SequenceFindLast((int)indexValue, (int)countValue, (int)value.Length, false, (byte v, int i) => v == value.Buffer[i]))); case StorageState.Stream: Stream stream = input.Stream; stream.Position = indexValue; long position = StreamExtension.FindLast(input.Stream, countValue, (int)value.Length, (byte v, int i, int j) => value.Buffer[(int)value.Length - 1 - i] == (j >= 0 ? value.Buffer[(int)value.Length - 1 - j] : v)); if (position >= 0L) { position = indexValue - position; } return(new SqlInt64(position)); default: throw new NotSupportedException("Unsupported input storage type."); } }
///<summary> ///Starts the listener for the server. ///</summary> public static void Main(string[] args) { string foutname; if (args.Length < 1) foutname = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\log"; else { foutname = args[0]; } BufferedStream outbuffer = new BufferedStream(new FileStream(foutname, FileMode.Append)); TcpListener server = null; try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected! From {0}", client.Client.RemoteEndPoint); data = null; // Get a stream object for reading and writing StreamExtension stream = new StreamExtension(client.GetStream()); int i; CI kp; while (stream.IsConnected()) { try { kp = CI.Parser.ParseDelimitedFrom(stream); Console.WriteLine("Received: {0}", kp); kp.WriteDelimitedTo(outbuffer); outbuffer.Flush(); } catch (IOException) { break; } } // Shutdown and end connection outbuffer.Flush(); client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); }