| igorlira | posted: Thu Oct 29, 2009 8:56 pm | |
Member since: 2009-10-25 Posts: 3
| MemoryStream eats a big system memory, to evit this, you can use this custom stream made by me instead of MemoryStream. class CustomStream : Stream { string _T; long _pos; System.Text.Encoding _encoder = Encoding.Default;
public CustomStream() : this("") { } public byte[] GetBytes() { return _encoder.GetBytes(_T); } public CustomStream(string BaseText) { _T = BaseText; _pos = 0; }
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return true; } }
public override bool CanWrite { get { return true; } }
public override void Flush() { _T = ""; _pos = 0; }
public override long Length { get { return _T.Length; } }
public override long Position { get { return _pos; } set { _pos = value; } }
public override int Read(byte[] buffer, int offset, int count) { int ret = 0; for (int i = 0; i < count; i++) { if (_T.Length <= _pos) { break; } buffer[i] = (byte)_T[(int)_pos]; _pos++; ret++; } return ret; }
public override long Seek(long offset, SeekOrigin origin) { if (origin != SeekOrigin.Begin) { throw new NotImplementedException(); } _pos = offset; return _pos; }
public override void SetLength(long value) { if (_T.Length > value) { string nstr = ""; for (int i = 0; i < value; i++) { nstr += _T[i]; } _T = nstr; } else if (_T.Length < value) { for (int i = 0; i < value - _T.Length; i++) { _T += "\0"; } } }
public override void Write(byte[] buffer, int offset, int count) { _T += _encoder.GetString(buffer, offset, count); } } Some fixes in edit since: 30/10 12:43 (GMT-3) |
| Inge Jones | posted: Fri Oct 30, 2009 8:55 am | |
Member since: 2005-03-06 Posts: 1897
| Thank you! I'll get Peter to look at that :) |
viewthread, 0, 0, Memory-saving-CustomStream-instead-of-MemoryStream