我有一个执行 PDF 文件操作的第三方组件。每当我需要执行操作时,我都会从文档存储(数据库、SharePoint、文件系统等)中检索 PDF 文档。为了使事情有点一致,我将 PDF 文档作为byte[]
.
这个第 3 方组件需要一个MemoryStream[]
( MemoryStream
array) 作为我需要使用的主要方法之一的参数。
我正在尝试将此功能包装在我自己的组件中,以便我可以将此功能用于我的应用程序中的许多区域。我基本上提出了以下几点:
public class PdfDocumentManipulator : IDisposable
{
List<MemoryStream> pdfDocumentStreams = new List<MemoryStream>();
public void AddFileToManipulate(byte[] pdfDocument)
{
using (MemoryStream stream = new MemoryStream(pdfDocument))
{
pdfDocumentStreams.Add(stream);
}
}
public byte[] ManipulatePdfDocuments()
{
byte[] outputBytes = null;
using (MemoryStream outputStream = new MemoryStream())
{
ThirdPartyComponent component = new ThirdPartyComponent();
component.Manipuate(this.pdfDocumentStreams.ToArray(), outputStream);
//move to begining
outputStream.Seek(0, SeekOrigin.Begin);
//convert the memory stream to a byte array
outputBytes = outputStream.ToArray();
}
return outputBytes;
}
#region IDisposable Members
public void Dispose()
{
for (int i = this.pdfDocumentStreams.Count - 1; i >= 0; i--)
{
MemoryStream stream = this.pdfDocumentStreams[i];
this.pdfDocumentStreams.RemoveAt(i);
stream.Dispose();
}
}
#endregion
}
我的“包装器”的调用代码如下所示:
byte[] manipulatedResult = null;
using (PdfDocumentManipulator manipulator = new PdfDocumentManipulator())
{
manipulator.AddFileToManipulate(file1bytes);
manipulator.AddFileToManipulate(file2bytes);
manipulatedResult = manipulator.Manipulate();
}
关于上面的几个问题:
- 方法中的
using
子句是AddFileToManipulate()
多余的和不必要的吗? Dispose()
我在我的对象的方法中清理东西好吗?- 这是“可接受的”用法
MemoryStream
吗?我预计内存中不会同时有很多文件......总共可能有 1-10 个 PDF 页,每页大约 200KB。设计为在支持 ASP.NET 站点的服务器上运行的应用程序。 - 有什么意见/建议吗?
感谢代码审查 :)