forked from ArtifexSoftware/Ghostscript.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGhostscriptPipedOutput.cs
More file actions
193 lines (156 loc) · 5.79 KB
/
GhostscriptPipedOutput.cs
File metadata and controls
193 lines (156 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// GhostscriptPipedOutput.cs
// This file is part of Ghostscript.NET library
//
// Author: Josip Habjan ([email protected], http://www.linkedin.com/in/habjan)
// Copyright (c) 2013-2016 by Josip Habjan. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
namespace Ghostscript.NET
{
/// <summary>
/// Represents a Ghostscript piped output.
/// </summary>
public class GhostscriptPipedOutput : IDisposable
{
#region Private variables
private bool _disposed = false;
private AnonymousPipeServerStream _pipe;
private Thread _thread = null;
private MemoryStream _data = new MemoryStream();
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the Ghostscript.NET.GhostscriptPipedOutput class.
/// </summary>
public GhostscriptPipedOutput()
{
_pipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);
_thread = new Thread(new System.Threading.ParameterizedThreadStart(ReadGhostscriptPipeOutput));
_thread.Start();
}
#endregion
#region Destructor
~GhostscriptPipedOutput()
{
Dispose(false);
}
#endregion
#region Dispose
#region Dispose
/// <summary>
/// Releases all resources used by the Ghostscript.NET.GhostscriptPipedOutput instance.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region Dispose - disposing
/// <summary>
/// Releases all resources used by the Ghostscript.NET.GhostscriptPipedOutput instance.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_pipe != null)
{
// _pipe.DisposeLocalCopyOfClientHandle();
// for some reason at this point the handle is invalid for real.
// DisposeLocalCopyOfClientHandle should be called instead, but it
// throws an exception saying that the handle is invalid pointing to
// CloseHandle method in the dissasembled code.
// this is a workaround, if we don't set the handle as invalid, when
// garbage collector tries to dispose this handle, exception is thrown
_pipe.ClientSafePipeHandle.SetHandleAsInvalid();
_pipe.Dispose(); _pipe = null;
}
if (_thread != null)
{
// check if the thread is still running
if (_thread.ThreadState == ThreadState.Running)
{
// abort the thread
_thread.Abort();
}
_thread = null;
}
}
_disposed = true;
}
}
#endregion
#endregion
#region ClientHandle
/// <summary>
/// Gets pipes client handle as string.
/// </summary>
public string ClientHandle
{
get
{
return _pipe.GetClientHandleAsString();
}
}
#endregion
#region ReadGhostscriptPipeOutput
/// <summary>
/// Reads Ghostscript output.
/// </summary>
public void ReadGhostscriptPipeOutput(object state)
{
// create BinaryReader instance through which we will read out Ghostscript output
using(BinaryReader reader = new BinaryReader(_pipe))
{
// allocate memory space for the incoming output data
byte[] buffer = new byte[_pipe.InBufferSize];
int readCount = 0;
// read untill we have something to read
while ((readCount = reader.Read(buffer, 0, buffer.Length)) > 0)
{
// write the output to our local memory stream
_data.Write(buffer, 0, readCount);
}
}
}
#endregion
#region Data
/// <summary>
/// Gets the Ghostscript output.
/// </summary>
public byte[] Data
{
get
{
_thread.Join();
return _data.ToArray();
}
}
#endregion
}
}