Skip to content

Commit

Permalink
modify code to close input streams
Browse files Browse the repository at this point in the history
Introduce try-with-resources blocks to release resources after consumptuion.
  • Loading branch information
AmilaSamith committed Jul 9, 2024
1 parent 2ddf1fa commit aab14e3
Showing 1 changed file with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,9 @@ public static void printXsd(org.wso2.micro.core.transports.CarbonHttpRequest req
schema.write(response.getOutputStream());
return;
} else {
InputStream instream = service.getClassLoader()
.getResourceAsStream(DeploymentConstants.META_INF + "/" + schemaName);

if (instream != null) {
response.setStatus(HttpStatus.SC_OK);
response.addHeader(HTTP.CONTENT_TYPE, "text/xml");
OutputStream outstream = response.getOutputStream();
boolean checkLength = true;
int length = Integer.MAX_VALUE;
int nextValue = instream.read();
if (checkLength) {
length--;
}
while (-1 != nextValue && length >= 0) {
outstream.write(nextValue);
nextValue = instream.read();
if (checkLength) {
length--;
}
}
outstream.flush();
return;
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ret = service.printXSD(baos, schemaName);
if (ret > 0) {
baos.flush();
instream = new ByteArrayInputStream(baos.toByteArray());
try (InputStream instream = service.getClassLoader()
.getResourceAsStream(DeploymentConstants.META_INF + "/" + schemaName)) {
if (instream != null) {
response.setStatus(HttpStatus.SC_OK);
response.addHeader(HTTP.CONTENT_TYPE, "text/xml");
OutputStream outstream = response.getOutputStream();
Expand All @@ -157,6 +132,32 @@ public static void printXsd(org.wso2.micro.core.transports.CarbonHttpRequest req
}
outstream.flush();
return;
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ret = service.printXSD(baos, schemaName);
if (ret > 0) {
baos.flush();
try (InputStream inputstream = new ByteArrayInputStream(baos.toByteArray())) {
response.setStatus(HttpStatus.SC_OK);
response.addHeader(HTTP.CONTENT_TYPE, "text/xml");
OutputStream outstream = response.getOutputStream();
boolean checkLength = true;
int length = Integer.MAX_VALUE;
int nextValue = inputstream.read();
if (checkLength) {
length--;
}
while (-1 != nextValue && length >= 0) {
outstream.write(nextValue);
nextValue = inputstream.read();
if (checkLength) {
length--;
}
}
outstream.flush();
return;
}
}
}
}
}
Expand Down Expand Up @@ -185,14 +186,15 @@ public static void printXsd(org.wso2.micro.core.transports.CarbonHttpRequest req
outputStream.flush();
}
} else if (xsds.endsWith(".xsd") && xsds.indexOf("..") == -1){
InputStream in = axisService.getClassLoader()
.getResourceAsStream(DeploymentConstants.META_INF + "/" + xsds);
if (in != null) {
outputStream.write(IOUtils.getStreamAsByteArray(in));
outputStream.flush();
outputStream.close();
} else {
response.setError(HttpServletResponse.SC_NOT_FOUND);
try (InputStream in = axisService.getClassLoader()
.getResourceAsStream(DeploymentConstants.META_INF + "/" + xsds);) {
if (in != null) {
outputStream.write(IOUtils.getStreamAsByteArray(in));
outputStream.flush();
outputStream.close();
} else {
response.setError(HttpServletResponse.SC_NOT_FOUND);
}
}
} else {
String msg = "Invalid schema " + xsds + " requested";
Expand Down

0 comments on commit aab14e3

Please sign in to comment.