Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert various try blocks to try-with-resources #385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
13 changes: 2 additions & 11 deletions src/main/javassist/CtClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -1514,14 +1514,9 @@ public void rebuildClassFile() {}
*/
public byte[] toBytecode() throws IOException, CannotCompileException {
ByteArrayOutputStream barray = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(barray);
try {
try (DataOutputStream out = new DataOutputStream(barray)) {
toBytecode(out);
}
finally {
out.close();
}

return barray.toByteArray();
}

Expand Down Expand Up @@ -1551,13 +1546,9 @@ public void writeFile()
public void writeFile(String directoryName)
throws CannotCompileException, IOException
{
DataOutputStream out = makeFileOutput(directoryName);
try {
try (DataOutputStream out = makeFileOutput(directoryName)) {
toBytecode(out);
}
finally {
out.close();
}
}

protected DataOutputStream makeFileOutput(String directoryName) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/javassist/CtClassType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1617,13 +1617,9 @@ public void toBytecode(DataOutputStream out)

private void dumpClassFile(ClassFile cf) throws IOException
{
DataOutputStream dump = makeFileOutput(debugDump);
try {
try (DataOutputStream dump = makeFileOutput(debugDump)) {
cf.write(dump);
}
finally {
dump.close();
}
}

/* See also checkModified()
Expand Down
23 changes: 10 additions & 13 deletions src/main/javassist/URLClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ private URLConnection openClassfile0(String classname) throws IOException {
public URL find(String classname) {
try {
URLConnection con = openClassfile0(classname);
InputStream is = con.getInputStream();
if (is != null) {
is.close();
return con.getURL();
try (InputStream is = con.getInputStream()) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this correct?

Copy link
Contributor Author

@tim-hoffman tim-hoffman Jul 19, 2021

Choose a reason for hiding this comment

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

The try-with-resources will automatically close the stream just prior to the return. Thus, to my knowledge, the only differences between these versions are (1) closing after the getURL call vs before which should be fine from what I can tell and (2) the new version ensure the stream is closed no matter what where the old version could allow a scenario where the stream is not closed if the current thread is interrupted by an asynchronous exception before the close method is called.

if (is != null) {
return con.getURL();
}
}
}
catch (IOException e) {}
Expand All @@ -135,26 +135,23 @@ public static byte[] fetchClass(String host, int port,
URLConnection con = fetchClass0(host, port,
directory + classname.replace('.', '/') + ".class");
int size = con.getContentLength();
InputStream s = con.getInputStream();
try {
if (size <= 0)
try (InputStream s = con.getInputStream()) {
if (size <= 0) {
b = ClassPoolTail.readStream(s);
else {
} else {
b = new byte[size];
int len = 0;
do {
int n = s.read(b, len, size - len);
if (n < 0)
if (n < 0) {
throw new IOException("the stream was closed: "
+ classname);
+ classname);
}

len += n;
} while (len < size);
}
}
finally {
s.close();
}

return b;
}
Expand Down
101 changes: 45 additions & 56 deletions src/main/javassist/tools/rmi/AppletServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,37 @@ else if (cmd.startsWith("POST /lookup "))
}

private void processRMI(InputStream ins, OutputStream outs)
throws IOException
{
ObjectInputStream in = new ObjectInputStream(ins);

int objectId = in.readInt();
int methodId = in.readInt();
Exception err = null;
Object rvalue = null;
try {
ExportedObject eo = exportedObjects.get(objectId);
Object[] args = readParameters(in);
rvalue = convertRvalue(eo.methods[methodId].invoke(eo.object,
args));
}
catch(Exception e) {
err = e;
logging2(e.toString());
}

outs.write(okHeader);
ObjectOutputStream out = new ObjectOutputStream(outs);
if (err != null) {
out.writeBoolean(false);
out.writeUTF(err.toString());
}
else
throws IOException {
try (ObjectInputStream in = new ObjectInputStream(ins)) {
int objectId = in.readInt();
int methodId = in.readInt();
Exception err = null;
Object rvalue = null;
try {
out.writeBoolean(true);
out.writeObject(rvalue);
}
catch (NotSerializableException e) {
ExportedObject eo = exportedObjects.get(objectId);
Object[] args = readParameters(in);
rvalue = convertRvalue(eo.methods[methodId].invoke(eo.object,
args));
} catch (Exception e) {
err = e;
logging2(e.toString());
}
catch (InvalidClassException e) {
logging2(e.toString());
outs.write(okHeader);
try (ObjectOutputStream out = new ObjectOutputStream(outs)) {
if (err != null) {
out.writeBoolean(false);
out.writeUTF(err.toString());
} else {
try {
out.writeBoolean(true);
out.writeObject(rvalue);
} catch (NotSerializableException | InvalidClassException e) {
logging2(e.toString());
}
}
out.flush();
}

out.flush();
out.close();
in.close();
}
}

private Object[] readParameters(ObjectInputStream in)
Expand Down Expand Up @@ -229,27 +220,25 @@ private Object convertRvalue(Object rvalue)
}

private void lookupName(String cmd, InputStream ins, OutputStream outs)
throws IOException
{
ObjectInputStream in = new ObjectInputStream(ins);
String name = DataInputStream.readUTF(in);
ExportedObject found = exportedNames.get(name);
outs.write(okHeader);
ObjectOutputStream out = new ObjectOutputStream(outs);
if (found == null) {
logging2(name + "not found.");
out.writeInt(-1); // error code
out.writeUTF("error");
}
else {
logging2(name);
out.writeInt(found.identifier);
out.writeUTF(found.object.getClass().getName());
throws IOException {
try (ObjectInputStream in = new ObjectInputStream(ins)) {
String name = DataInputStream.readUTF(in);
ExportedObject found = exportedNames.get(name);
outs.write(okHeader);
try (ObjectOutputStream out = new ObjectOutputStream(outs)) {
if (found == null) {
logging2(name + "not found.");
out.writeInt(-1); // error code
out.writeUTF("error");
} else {
logging2(name);
out.writeInt(found.identifier);
out.writeUTF(found.object.getClass().getName());
}

out.flush();
}
}

out.flush();
out.close();
in.close();
}
}

Expand Down
84 changes: 43 additions & 41 deletions src/main/javassist/tools/rmi/ObjectImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,27 @@ public void setHttpProxy(String host, int port) {
public Object lookupObject(String name) throws ObjectNotFoundException
{
try {
Socket sock = new Socket(servername, port);
OutputStream out = sock.getOutputStream();
out.write(lookupCommand);
out.write(endofline);
out.write(endofline);
int n;
String classname;
try (Socket sock = new Socket(servername, port)) {
OutputStream out = sock.getOutputStream();
out.write(lookupCommand);
out.write(endofline);
out.write(endofline);

ObjectOutputStream dout = new ObjectOutputStream(out);
dout.writeUTF(name);
dout.flush();
ObjectOutputStream dout = new ObjectOutputStream(out);
dout.writeUTF(name);
dout.flush();

InputStream in = new BufferedInputStream(sock.getInputStream());
skipHeader(in);
ObjectInputStream din = new ObjectInputStream(in);
int n = din.readInt();
String classname = din.readUTF();
din.close();
dout.close();
sock.close();
InputStream in = new BufferedInputStream(sock.getInputStream());
skipHeader(in);
ObjectInputStream din = new ObjectInputStream(in);
n = din.readInt();
classname = din.readUTF();

din.close();
dout.close();
}

if (n >= 0)
return createProxy(n, classname);
Expand Down Expand Up @@ -231,33 +234,32 @@ public Object call(int objectid, int methodid, Object[] args)
*
* lookupObject() has the same problem.
*/
Socket sock = new Socket(servername, port);
OutputStream out = new BufferedOutputStream(
sock.getOutputStream());
out.write(rmiCommand);
out.write(endofline);
out.write(endofline);
try (Socket sock = new Socket(servername, port)) {
OutputStream out = new BufferedOutputStream(sock.getOutputStream());
out.write(rmiCommand);
out.write(endofline);
out.write(endofline);

ObjectOutputStream dout = new ObjectOutputStream(out);
dout.writeInt(objectid);
dout.writeInt(methodid);
writeParameters(dout, args);
dout.flush();
ObjectOutputStream dout = new ObjectOutputStream(out);
dout.writeInt(objectid);
dout.writeInt(methodid);
writeParameters(dout, args);
dout.flush();

InputStream ins = new BufferedInputStream(sock.getInputStream());
skipHeader(ins);
ObjectInputStream din = new ObjectInputStream(ins);
result = din.readBoolean();
rvalue = null;
errmsg = null;
if (result)
rvalue = din.readObject();
else
errmsg = din.readUTF();

din.close();
dout.close();
sock.close();
InputStream ins = new BufferedInputStream(sock.getInputStream());
skipHeader(ins);
ObjectInputStream din = new ObjectInputStream(ins);
result = din.readBoolean();
rvalue = null;
errmsg = null;
if (result) {
rvalue = din.readObject();
} else {
errmsg = din.readUTF();
}
din.close();
dout.close();
}

if (rvalue instanceof RemoteRef) {
RemoteRef ref = (RemoteRef)rvalue;
Expand Down
32 changes: 15 additions & 17 deletions src/main/javassist/tools/web/Viewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,22 @@ protected byte[] fetchClass(String classname) throws Exception
URLConnection con = url.openConnection();
con.connect();
int size = con.getContentLength();
InputStream s = con.getInputStream();
if (size <= 0)
b = readStream(s);
else {
b = new byte[size];
int len = 0;
do {
int n = s.read(b, len, size - len);
if (n < 0) {
s.close();
throw new IOException("the stream was closed: "
+ classname);
}
len += n;
} while (len < size);
try (InputStream s = con.getInputStream()) {
if (size <= 0) {
b = readStream(s);
} else {
b = new byte[size];
int len = 0;
do {
int n = s.read(b, len, size - len);
if (n < 0) {
throw new IOException("the stream was closed: "
+ classname);
}
len += n;
} while (len < size);
}
}

s.close();
return b;
}

Expand Down
Loading