Skip to content

Commit 9df8458

Browse files
committed
SXP: Free resources after reading them
DEVSIX-2314 Autoported commit. Original commit hash: [71180b08e] Manual files: styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/resolver/resource/ResourceResolverTest.java
1 parent a241655 commit 9df8458

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

itext.tests/itext.styledxmlparser.tests/itext/styledxmlparser/resolver/resource/ResourceResolverTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ResourceResolverTest : ExtendedITextTest {
5454
.CurrentContext.TestDirectory) + "/resources/itext/styledxmlparser/resolver/retrieveStreamTest/";
5555

5656
[NUnit.Framework.Test]
57-
[LogMessage(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, Count = 2)]
57+
[LogMessage(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, Count = 1)]
5858
public virtual void malformedResourceNameTest() {
5959
String fileName = "resourceResolverTest .png";
6060
ResourceResolver resourceResolver = new ResourceResolver(baseUri);

itext/itext.styledxmlparser/itext/styledxmlparser/resolver/resource/ResourceResolver.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,14 @@ public virtual Stream RetrieveStyleSheet(String uri) {
175175
[Obsolete]
176176
public virtual byte[] RetrieveStream(String src) {
177177
try {
178-
return StreamUtil.InputStreamToArray(RetrieveResourceAsInputStream(src));
178+
using (Stream stream = RetrieveResourceAsInputStream(src)) {
179+
if (stream != null) {
180+
return StreamUtil.InputStreamToArray(stream);
181+
}
182+
else {
183+
return null;
184+
}
185+
}
179186
}
180187
catch (Exception e) {
181188
ILog logger = LogManager.GetLogger(typeof(iText.StyledXmlParser.Resolver.Resource.ResourceResolver));
@@ -194,19 +201,20 @@ public virtual byte[] RetrieveStream(String src) {
194201
/// <param name="src">either link to file or base64 encoded stream.</param>
195202
/// <returns>byte[] on success, otherwise null.</returns>
196203
public virtual byte[] RetrieveBytesFromResource(String src) {
197-
Stream stream = RetrieveResourceAsInputStream(src);
198-
if (stream != null) {
199-
try {
200-
return StreamUtil.InputStreamToArray(stream);
201-
}
202-
catch (System.IO.IOException ioe) {
203-
ILog logger = LogManager.GetLogger(typeof(iText.StyledXmlParser.Resolver.Resource.ResourceResolver));
204-
logger.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
205-
, uriResolver.GetBaseUri(), src), ioe);
206-
return null;
204+
try {
205+
using (Stream stream = RetrieveResourceAsInputStream(src)) {
206+
if (stream != null) {
207+
return StreamUtil.InputStreamToArray(stream);
208+
}
209+
else {
210+
return null;
211+
}
207212
}
208213
}
209-
else {
214+
catch (System.IO.IOException ioe) {
215+
ILog logger = LogManager.GetLogger(typeof(iText.StyledXmlParser.Resolver.Resource.ResourceResolver));
216+
logger.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
217+
, uriResolver.GetBaseUri(), src), ioe);
210218
return null;
211219
}
212220
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12da6291f4df3a21df2574d7028b68353bc5a912
1+
71180b08e7d566226757888776b008557cd50013

0 commit comments

Comments
 (0)