@@ -1514,14 +1514,9 @@ public abstract class CtClass { | |||
*/ | |||
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(); | |||
} | |||
@@ -1551,13 +1546,9 @@ public abstract class CtClass { | |||
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) { |
@@ -1617,13 +1617,9 @@ class CtClassType extends CtClass { | |||
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() |
@@ -107,10 +107,10 @@ public class URLClassPath implements ClassPath { | |||
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()) { | |||
if (is != null) { | |||
return con.getURL(); | |||
} | |||
} | |||
} | |||
catch (IOException e) {} | |||
@@ -135,26 +135,23 @@ public class URLClassPath implements ClassPath { | |||
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; | |||
} |
@@ -155,46 +155,37 @@ public class AppletServer extends Webserver { | |||
} | |||
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) | |||
@@ -229,27 +220,25 @@ public class AppletServer extends Webserver { | |||
} | |||
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(); | |||
} | |||
} | |||
@@ -162,24 +162,27 @@ public class ObjectImporter implements java.io.Serializable { | |||
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); | |||
@@ -231,33 +234,32 @@ public class ObjectImporter implements java.io.Serializable { | |||
* | |||
* 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; |
@@ -170,24 +170,22 @@ public class Viewer extends ClassLoader { | |||
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; | |||
} | |||
@@ -200,24 +200,21 @@ public class Webserver { | |||
} | |||
final void process(Socket clnt) throws IOException { | |||
InputStream in = new BufferedInputStream(clnt.getInputStream()); | |||
String cmd = readLine(in); | |||
logging(clnt.getInetAddress().getHostName(), | |||
new Date().toString(), cmd); | |||
while (skipLine(in) > 0){ | |||
} | |||
OutputStream out = new BufferedOutputStream(clnt.getOutputStream()); | |||
try { | |||
doReply(in, out, cmd); | |||
} | |||
catch (BadHttpRequest e) { | |||
replyError(out, e); | |||
try (InputStream in = new BufferedInputStream(clnt.getInputStream())) { | |||
String cmd = readLine(in); | |||
logging(clnt.getInetAddress().getHostName(), | |||
new Date().toString(), cmd); | |||
while (skipLine(in) > 0) {} | |||
try (OutputStream out | |||
= new BufferedOutputStream(clnt.getOutputStream())) { | |||
try { | |||
doReply(in, out, cmd); | |||
} catch (BadHttpRequest e) { | |||
replyError(out, e); | |||
} | |||
out.flush(); | |||
} | |||
} | |||
out.flush(); | |||
in.close(); | |||
out.close(); | |||
clnt.close(); | |||
} | |||
@@ -285,16 +282,16 @@ public class Webserver { | |||
File file = new File(filename); | |||
if (file.canRead()) { | |||
sendHeader(out, file.length(), fileType); | |||
FileInputStream fin = new FileInputStream(file); | |||
byte[] filebuffer = new byte[4096]; | |||
for (;;) { | |||
len = fin.read(filebuffer); | |||
if (len <= 0) | |||
break; | |||
out.write(filebuffer, 0, len); | |||
try (FileInputStream fin = new FileInputStream(file)) { | |||
byte[] filebuffer = new byte[4096]; | |||
for (;;) { | |||
len = fin.read(filebuffer); | |||
if (len <= 0) { | |||
break; | |||
} | |||
out.write(filebuffer, 0, len); | |||
} | |||
} | |||
fin.close(); | |||
return; | |||
} | |||
@@ -302,23 +299,24 @@ public class Webserver { | |||
// then Class.getResourceAsStream() is tried. | |||
if (fileType == typeClass) { | |||
InputStream fin | |||
= getClass().getResourceAsStream("/" + urlName); | |||
if (fin != null) { | |||
ByteArrayOutputStream barray = new ByteArrayOutputStream(); | |||
byte[] filebuffer = new byte[4096]; | |||
for (;;) { | |||
len = fin.read(filebuffer); | |||
if (len <= 0) | |||
break; | |||
barray.write(filebuffer, 0, len); | |||
try (InputStream fin | |||
= getClass().getResourceAsStream("/" + urlName)) { | |||
if (fin != null) { | |||
ByteArrayOutputStream barray = new ByteArrayOutputStream(); | |||
byte[] filebuffer = new byte[4096]; | |||
for (;;) { | |||
len = fin.read(filebuffer); | |||
if (len <= 0) { | |||
break; | |||
} | |||
barray.write(filebuffer, 0, len); | |||
} | |||
byte[] classfile = barray.toByteArray(); | |||
sendHeader(out, classfile.length, typeClass); | |||
out.write(classfile); | |||
return; | |||
} | |||
byte[] classfile = barray.toByteArray(); | |||
sendHeader(out, classfile.length, typeClass); | |||
out.write(classfile); | |||
fin.close(); | |||
return; | |||
} | |||
} | |||
@@ -202,21 +202,13 @@ public class HotSwapAgent { | |||
attrs.put(new Attributes.Name("Can-Retransform-Classes"), "true"); | |||
attrs.put(new Attributes.Name("Can-Redefine-Classes"), "true"); | |||
JarOutputStream jos = null; | |||
try { | |||
jos = new JarOutputStream(new FileOutputStream(jar), manifest); | |||
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jar), manifest)) { | |||
String cname = HotSwapAgent.class.getName(); | |||
JarEntry e = new JarEntry(cname.replace('.', '/') + ".class"); | |||
jos.putNextEntry(e); | |||
ClassPool pool = ClassPool.getDefault(); | |||
CtClass clazz = pool.get(cname); | |||
jos.putNextEntry(new JarEntry(cname.replace('.', '/') + ".class")); | |||
CtClass clazz = ClassPool.getDefault().get(cname); | |||
jos.write(clazz.toBytecode()); | |||
jos.closeEntry(); | |||
} | |||
finally { | |||
if (jos != null) | |||
jos.close(); | |||
} | |||
return jar; | |||
} |
@@ -179,14 +179,9 @@ public class FactoryHelper { | |||
private static byte[] toBytecode(ClassFile cf) throws IOException { | |||
ByteArrayOutputStream barray = new ByteArrayOutputStream(); | |||
DataOutputStream out = new DataOutputStream(barray); | |||
try { | |||
try (DataOutputStream out = new DataOutputStream(barray)) { | |||
cf.write(out); | |||
} | |||
finally { | |||
out.close(); | |||
} | |||
return barray.toByteArray(); | |||
} | |||
@@ -215,16 +210,12 @@ public class FactoryHelper { | |||
new File(dir).mkdirs(); | |||
} | |||
DataOutputStream out = new DataOutputStream(new BufferedOutputStream( | |||
new FileOutputStream(filename))); | |||
try { | |||
try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream( | |||
new FileOutputStream(filename)))) { | |||
cf.write(out); | |||
} | |||
catch (IOException e) { | |||
throw e; | |||
} | |||
finally { | |||
out.close(); | |||
} | |||
} | |||
} |