Csharp/C Sharp by API/System.IO/MemoryStream
Содержание
MemoryStream.Capacity
using System;
using System.IO;
public class MainClass
{
public static int Main(string[] args)
{
MemoryStream myMemStream = new MemoryStream();
myMemStream.Capacity = 256;
for(int i = 0; i < 256; i++)
myMemStream.WriteByte((byte)i);
myMemStream.Position = 0;
for(int i = 0; i < 256; i++)
Console.Write(myMemStream.ReadByte());
Console.WriteLine();
FileStream dumpFile = new FileStream("Dump.dat", FileMode.Create, FileAccess.ReadWrite);
myMemStream.WriteTo(dumpFile);
byte[] bytesinMemory = myMemStream.ToArray();
myMemStream.Close();
for(int i = 0; i < bytesinMemory.Length; i++)
Console.Write(bytesinMemory[i]);
return 0;
}
}
MemoryStream.GetBuffer()
using System;
using System.Windows.Forms;
using System.IO;
class MainClass
{
public static void SaveMemoryStream(MemoryStream ms, string FileName)
{
FileStream outStream = File.OpenWrite(FileName);
ms.WriteTo(outStream);
outStream.Flush();
outStream.Close();
}
public static void Main()
{
FileStream inStream = File.OpenRead("c:\\text.txt");
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(inStream.Length);
inStream.Read(storeStream.GetBuffer(), 0, (int)inStream.Length);
storeStream.Flush();
inStream.Close();
SaveMemoryStream(storeStream, "C:\\text.txt" + ".bak");
storeStream.Close();
}
}
MemoryStream.Seek
using System;
using System.IO;
public class MemStrDemo {
public static void Main() {
byte[] storage = new byte[255];
// Create a memory-based stream.
MemoryStream memstrm = new MemoryStream(storage);
// Wrap memstrm in a reader and a writer.
StreamWriter memwtr = new StreamWriter(memstrm);
StreamReader memrdr = new StreamReader(memstrm);
// Write to storage, through memwtr.
for(int i=0; i < 10; i++)
memwtr.WriteLine("byte [" + i + "]: " + i);
// put a period at the end
memwtr.Write(".");
memwtr.Flush();
Console.WriteLine("Reading from storage directly: ");
// Display contents of storage directly.
foreach(char ch in storage) {
if (ch == ".") break;
Console.Write(ch);
}
Console.WriteLine("\nReading through memrdr: ");
// Read from memstrm using the stream reader.
memstrm.Seek(0, SeekOrigin.Begin); // reset file pointer
string str = memrdr.ReadLine();
while(str != null) {
str = memrdr.ReadLine();
if(str.rupareTo(".") == 0) break;
Console.WriteLine(str);
}
}
}
MemoryStream.SetLength
using System;
using System.Windows.Forms;
using System.IO;
public class Example15_13
{
// SaveMemoryStream saves the MemoryStream as a file
public static void SaveMemoryStream(
MemoryStream ms, string FileName)
{
FileStream outStream = File.OpenWrite(FileName);
ms.WriteTo(outStream);
outStream.Flush();
outStream.Close();
}
[STAThread]
public static void Main()
{
// use an open file dialog to get a filename
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title="Select file to back up";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
// Read the file into a MemoryStream
FileStream inStream = File.OpenRead(dlgOpen.FileName);
MemoryStream storeStream = new MemoryStream();
// copy all data from in to store
storeStream.SetLength(inStream.Length);
inStream.Read(storeStream.GetBuffer(), 0, (int)inStream.Length);
// clean up
storeStream.Flush();
inStream.Close();
// pass the store to a method to write it out
SaveMemoryStream(storeStream, dlgOpen.FileName + ".bak");
storeStream.Close();
}
}
}
MemoryStream.ToArray()
using System;
using System.IO;
public class MainClass
{
public static int Main(string[] args)
{
MemoryStream myMemStream = new MemoryStream();
myMemStream.Capacity = 256;
for(int i = 0; i < 256; i++)
myMemStream.WriteByte((byte)i);
myMemStream.Position = 0;
for(int i = 0; i < 256; i++)
Console.Write(myMemStream.ReadByte());
Console.WriteLine();
FileStream dumpFile = new FileStream("Dump.dat", FileMode.Create, FileAccess.ReadWrite);
myMemStream.WriteTo(dumpFile);
byte[] bytesinMemory = myMemStream.ToArray();
myMemStream.Close();
for(int i = 0; i < bytesinMemory.Length; i++)
Console.Write(bytesinMemory[i]);
return 0;
}
}
MemoryStream.Write
using System;
using System.IO;
public class MemStrDemo {
public static void Main() {
byte[] storage = new byte[255];
// Create a memory-based stream.
MemoryStream memstrm = new MemoryStream(storage);
// Wrap memstrm in a reader and a writer.
StreamWriter memwtr = new StreamWriter(memstrm);
StreamReader memrdr = new StreamReader(memstrm);
// Write to storage, through memwtr.
for(int i=0; i < 10; i++)
memwtr.WriteLine("byte [" + i + "]: " + i);
// put a period at the end
memwtr.Write(".");
memwtr.Flush();
Console.WriteLine("Reading from storage directly: ");
// Display contents of storage directly.
foreach(char ch in storage) {
if (ch == ".") break;
Console.Write(ch);
}
Console.WriteLine("\nReading through memrdr: ");
// Read from memstrm using the stream reader.
memstrm.Seek(0, SeekOrigin.Begin); // reset file pointer
string str = memrdr.ReadLine();
while(str != null) {
str = memrdr.ReadLine();
if(str.rupareTo(".") == 0) break;
Console.WriteLine(str);
}
}
}
MemoryStream.WriteLine
using System;
using System.IO;
public class MemStrDemo {
public static void Main() {
byte[] storage = new byte[255];
// Create a memory-based stream.
MemoryStream memstrm = new MemoryStream(storage);
// Wrap memstrm in a reader and a writer.
StreamWriter memwtr = new StreamWriter(memstrm);
StreamReader memrdr = new StreamReader(memstrm);
// Write to storage, through memwtr.
for(int i=0; i < 10; i++)
memwtr.WriteLine("byte [" + i + "]: " + i);
// put a period at the end
memwtr.Write(".");
memwtr.Flush();
Console.WriteLine("Reading from storage directly: ");
// Display contents of storage directly.
foreach(char ch in storage) {
if (ch == ".") break;
Console.Write(ch);
}
Console.WriteLine("\nReading through memrdr: ");
// Read from memstrm using the stream reader.
memstrm.Seek(0, SeekOrigin.Begin); // reset file pointer
string str = memrdr.ReadLine();
while(str != null) {
str = memrdr.ReadLine();
if(str.rupareTo(".") == 0) break;
Console.WriteLine(str);
}
}
}
new MemoryStream()
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public class Analyzer {
public static void Main() {
Image sample = new Bitmap("a.jpg");
MemoryStream buf = new MemoryStream();
sample.Save(buf, ImageFormat.Bmp);
byte[] currentImage = buf.GetBuffer();
int[] stats = new int[3];
for (int i = 0; i < currentImage.Length; ){
for (int j = 0; j < 3; j++) {
stats[j] += currentImage[i];
++i;
}
}
Console.WriteLine("Blue: " + stats[0]);
Console.WriteLine("Green: " + stats[1]);
Console.WriteLine("Red: " + stats[2]);
if ((stats[0] > stats[1]) && (stats[0] > stats[2]))
Console.WriteLine("This is a cold picture.");
if ((stats[1] > stats[0]) && (stats[1] > stats[2]))
Console.WriteLine("This is a summer picture.");
if ((stats[2] > stats[0]) && (stats[2] > stats[1]))
Console.WriteLine("This is a fiery picture.");
}
}