Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,12 @@ void mkdirTest() throws IOException, AlluxioException {
private void fileReadTest(String fileName, int size) throws IOException {
File testFile = new File(PathUtils.concatPath(mLocalAlluxioCluster.getAlluxioHome(), fileName));
FileInputStream fis = new FileInputStream(testFile);
byte[] read = new byte[size];
fis.read(read);
fis.close();
assertTrue(BufferUtils.equalIncreasingByteArray(size, read));
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use try-with-resources. This is more elegant.
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

byte[] read = new byte[size];
fis.read(read);
assertTrue(BufferUtils.equalIncreasingByteArray(size, read));
} finally {
fis.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ private void setupJVMGateway(String gatewayHost, int gatewayPort) throws IOExcep
private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws IOException {
InputStream input =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, closing the input stream is missing; this can be done directly with try-with-resource.

getClass().getClassLoader().getResourceAsStream("python/zeppelin_ipython.py");
if (input == null) {
throw new IOException("Cannot find resource: python/zeppelin_ipython.py");
}
List<String> lines = IOUtils.readLines(input, StandardCharsets.UTF_8);
ExecuteResponse response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder()
.setCode(StringUtils.join(lines, System.lineSeparator())
Expand All @@ -145,6 +148,9 @@ private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws I

input =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, closing the input stream is missing; this can be done directly with try-with-resource.

getClass().getClassLoader().getResourceAsStream("python/zeppelin_context.py");
if (input == null) {
throw new IOException("Cannot find resource: python/zeppelin_context.py");
}
lines = IOUtils.readLines(input, StandardCharsets.UTF_8);
response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder()
.setCode(StringUtils.join(lines, System.lineSeparator())).build());
Expand All @@ -161,6 +167,9 @@ private void initPythonInterpreter(String gatewayHost, int gatewayPort) throws I

if (additionalPythonInitFile != null) {
input = getClass().getClassLoader().getResourceAsStream(additionalPythonInitFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, closing the input stream is missing; this can be done directly with try-with-resource.

if (input == null) {
throw new IOException("Cannot find resource: " + additionalPythonInitFile);
}
lines = IOUtils.readLines(input, StandardCharsets.UTF_8);
response = jupyterKernelClient.block_execute(ExecuteRequest.newBuilder()
.setCode(StringUtils.join(lines, System.lineSeparator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -196,15 +197,21 @@ protected boolean useIPython() {
private void copyResourceToPythonWorkDir(String srcResourceName,
String dstFileName) throws IOException {
FileOutputStream out = null;
InputStream in = null;
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use try-with-resources, also here

out = new FileOutputStream(pythonWorkDir.getAbsoluteFile() + "/" + dstFileName);
IOUtils.copy(
getClass().getClassLoader().getResourceAsStream(srcResourceName),
out);
in = getClass().getClassLoader().getResourceAsStream(srcResourceName);
if (in == null) {
throw new IOException("Cannot find resource: " + srcResourceName);
}
IOUtils.copy(in, out);
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions rlang/src/main/java/org/apache/zeppelin/r/IRInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ protected void initIRKernel() throws IOException, InterpreterException {
String timeout = getProperty("spark.r.backendConnectionTimeout", "6000");
InputStream input =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same please use try-with-resource do close the stream.

getClass().getClassLoader().getResourceAsStream("R/zeppelin_isparkr.R");
if (input == null) {
throw new IOException("Cannot find resource: R/zeppelin_isparkr.R");
}
String code = IOUtils.toString(input, StandardCharsets.UTF_8)
.replace("${Port}", sparkRBackend.port() + "")
.replace("${version}", sparkVersion() + "")
Expand Down
3 changes: 3 additions & 0 deletions rlang/src/main/java/org/apache/zeppelin/r/ZeppelinR.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public void open() throws IOException, InterpreterException {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same use try-with-resource for the input and FileOutputStream

out = new FileOutputStream(scriptFile);
in = getClass().getClassLoader().getResourceAsStream("R/zeppelin_sparkr.R");
if (in == null) {
throw new InterpreterException("Cannot find resource: R/zeppelin_sparkr.R");
}
IOUtils.copy(in, out);
} catch (IOException e) {
throw new InterpreterException(e);
Expand Down
Loading