Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Fix for #693 input stream resource leak #694

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions yagi/src/main/java/org/raml/v2/api/loader/CacheResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.raml.v2.api.loader;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand Down Expand Up @@ -62,24 +61,34 @@ public InputStream fetchResource(String resourceName, ResourceUriCallback callba
return toInputStreamOrNull(resourceByteArray);
}

InputStream resource;
URI uriCallback = null;
if (resourceLoader instanceof ResourceLoaderExtended)
InputStream resource = null;
try
{
resource = ((ResourceLoaderExtended) resourceLoader).fetchResource(resourceName, callback);
uriCallback = ((ResourceLoaderExtended) resourceLoader).getUriCallBackParam();
URI uriCallback = null;
if (resourceLoader instanceof ResourceLoaderExtended)
{
resource = ((ResourceLoaderExtended) resourceLoader).fetchResource(resourceName, callback);
uriCallback = ((ResourceLoaderExtended) resourceLoader).getUriCallBackParam();
}
else
{
resource = resourceLoader.fetchResource(resourceName);
}

// we want to cache results even if they are null
final byte[] resourceByteArray = resource == null ? null : IOUtils.toByteArray(resource);
resources.put(resourceName, Pair.of(resourceByteArray, uriCallback));

return toInputStreamOrNull(resourceByteArray);
}
else
finally
{
resource = resourceLoader.fetchResource(resourceName);
if (resource != null)
{
resource.close();
}
}

// we want to cache results even if they are null
final byte[] resourceByteArray = resource == null ? null : IOUtils.toByteArray(resource);
resources.put(resourceName, Pair.of(resourceByteArray, uriCallback));

return toInputStreamOrNull(resourceByteArray);

}
catch (final IOException e)
{
Expand Down