BaseStream을 닫지 않고 StreamWriter를 닫는 방법이 있습니까?
내 근본 문제는 때이다 using호출 Dispose에 StreamWriter, 그것은 또한 처분 BaseStream(같은 문제로를 Close).
이에 대한 해결 방법이 있지만 보시다시피 스트림 복사가 포함됩니다. 스트림을 복사하지 않고이 작업을 수행 할 수있는 방법이 있습니까?
이것의 목적은 문자열의 내용 (원래 데이터베이스에서 읽음)을 스트림으로 가져 와서 제 3 자 구성 요소에서 스트림을 읽을 수 있도록하는 것입니다.
참고 : 타사 구성 요소를 변경할 수 없습니다.
public System.IO.Stream CreateStream(string value)
{
var baseStream = new System.IO.MemoryStream();
var baseCopy = new System.IO.MemoryStream();
using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
{
writer.Write(value);
writer.Flush();
baseStream.WriteTo(baseCopy);
}
baseCopy.Seek(0, System.IO.SeekOrigin.Begin);
return baseCopy;
}
사용
public void Noddy()
{
System.IO.Stream myStream = CreateStream("The contents of this string are unimportant");
My3rdPartyComponent.ReadFromStream(myStream);
}
이상적으로 불리는 가상의 방법을 찾고 있어요 BreakAssociationWithBaseStream, 예를 들어,
public System.IO.Stream CreateStream_Alternate(string value)
{
var baseStream = new System.IO.MemoryStream();
using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
{
writer.Write(value);
writer.Flush();
writer.BreakAssociationWithBaseStream();
}
return baseStream;
}
.NET Framework 4.5 이상을 사용하는 경우 작성기가 닫힐 때 기본 스트림을 열어 두도록 요청할 수 있는 StreamWriter 오버로드가 있습니다 .
4.5 이전 버전의 .NET Framework StreamWriter 에서는 스트림을 소유 한다고 가정 합니다. 옵션 :
- 처분하지 마십시오
StreamWriter; 그냥 플러시하십시오. Close/에 대한 호출을 무시Dispose하지만 다른 모든 것을 프록시 하는 스트림 래퍼를 만듭니다 . 거기에서 가져오고 싶다면 MiscUtil 에서 구현 했습니다.
.NET 4.5에는 새로운 방법이 있습니다!
http://msdn.microsoft.com/EN-US/library/gg712853(v=VS.110,d=hv.2).aspx
public StreamWriter(
Stream stream,
Encoding encoding,
int bufferSize,
bool leaveOpen
)
Simply don't call Dispose on the StreamWriter. The reason this class is disposable is not because it holds unmanaged resource but to allow the disposal of the stream which itself could hold unmanaged resources. If the life of the underlying stream is handled elsewhere, no need to dispose the writer.
Memory stream has a ToArray property that can be used even when stream is closed. To Array writes the stream contents to a byte array, regardless of the Position property. You can create a new stream based on the stream you wrote in.
public System.IO.Stream CreateStream(string value)
{
var baseStream = new System.IO.MemoryStream();
var baseCopy = new System.IO.MemoryStream();
using (var writer = new System.IO.StreamWriter(baseStream, System.Text.Encoding.UTF8))
{
writer.Write(value);
writer.Flush();
baseStream.WriteTo(baseCopy);
}
var returnStream = new System.IO.MemoryStream( baseCopy.ToArray());
return returnStream;
}
You need to create a descendant of the StreamWriter and override its dispose method, by always passing false to the disposing parameter, it will force the stream writer NOT to close, the StreamWriter just calls dispose in the close method, so there is no need to override it (of course you can add all the constructors if you want, i just have one):
public class NoCloseStreamWriter : StreamWriter
{
public NoCloseStreamWriter(Stream stream, Encoding encoding)
: base(stream, encoding)
{
}
protected override void Dispose(bool disposing)
{
base.Dispose(false);
}
}
'Programing' 카테고리의 다른 글
| 스토리 보드를 사용하지 않고 새 Swift 프로젝트를 생성하려면 어떻게합니까? (0) | 2020.08.12 |
|---|---|
| WPF에서 WndProc 메시지를 처리하는 방법은 무엇입니까? (0) | 2020.08.12 |
| 버전 제어에 Xcode 작업 공간 체계 추가 (0) | 2020.08.12 |
| 제거 / 무시하는 방법 : 터치 장치에서 CSS 스타일을 호버 (0) | 2020.08.12 |
| 파일 이름으로 사용하기 위해 Java에서 문자열을 안전하게 인코딩하는 방법은 무엇입니까? (0) | 2020.08.12 |