提交 671533fd 编写于 作者: S smarks

7022382: convert pack200 library code to use try-with-resources

Reviewed-by: ksrini
上级 7ff04ffb
......@@ -743,24 +743,24 @@ class BandStructure {
private void dumpBand() throws IOException {
assert(optDumpBands);
PrintStream ps = new PrintStream(getDumpStream(this, ".txt"));
String irr = (bandCoding == regularCoding) ? "" : " irregular";
ps.print("# length="+length+
" size="+outputSize()+
irr+" coding="+bandCoding);
if (metaCoding != noMetaCoding) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < metaCoding.length; i++) {
if (i == 1) sb.append(" /");
sb.append(" ").append(metaCoding[i] & 0xFF);
try (PrintStream ps = new PrintStream(getDumpStream(this, ".txt"))) {
String irr = (bandCoding == regularCoding) ? "" : " irregular";
ps.print("# length="+length+
" size="+outputSize()+
irr+" coding="+bandCoding);
if (metaCoding != noMetaCoding) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < metaCoding.length; i++) {
if (i == 1) sb.append(" /");
sb.append(" ").append(metaCoding[i] & 0xFF);
}
ps.print(" //header: "+sb);
}
ps.print(" //header: "+sb);
printArrayTo(ps, values, 0, length);
}
try (OutputStream ds = getDumpStream(this, ".bnd")) {
bandCoding.writeArrayTo(ds, values, 0, length);
}
printArrayTo(ps, values, 0, length);
ps.close();
OutputStream ds = getDumpStream(this, ".bnd");
bandCoding.writeArrayTo(ds, values, 0, length);
ds.close();
}
/** Disburse one value. */
......@@ -829,12 +829,12 @@ class BandStructure {
private void dumpBand() throws IOException {
assert(optDumpBands);
OutputStream ds = getDumpStream(this, ".bnd");
if (bytesForDump != null)
bytesForDump.writeTo(ds);
else
bytes.writeTo(ds);
ds.close();
try (OutputStream ds = getDumpStream(this, ".bnd")) {
if (bytesForDump != null)
bytesForDump.writeTo(ds);
else
bytes.writeTo(ds);
}
}
public void readDataFrom(InputStream in) throws IOException {
......
......@@ -150,12 +150,12 @@ class Driver {
// See if there is any other action to take.
if ("--config-file=".equals(state)) {
String propFile = av.remove(0);
InputStream propIn = new FileInputStream(propFile);
Properties fileProps = new Properties();
fileProps.load(new BufferedInputStream(propIn));
try (InputStream propIn = new FileInputStream(propFile)) {
fileProps.load(propIn);
}
if (engProps.get(verboseProp) != null)
fileProps.list(System.out);
propIn.close();
for (Map.Entry<Object,Object> me : fileProps.entrySet()) {
engProps.put((String) me.getKey(), (String) me.getValue());
}
......@@ -348,10 +348,10 @@ class Driver {
else
fileOut = new FileOutputStream(outfile);
fileOut = new BufferedOutputStream(fileOut);
JarOutputStream out = new JarOutputStream(fileOut);
junpack.unpack(in, out);
//in.close(); // p200 closes in but not out
out.close();
try (JarOutputStream out = new JarOutputStream(fileOut)) {
junpack.unpack(in, out);
// p200 closes in but not out
}
// At this point, we have a good jarfile (or newfile, if -r)
}
......@@ -411,8 +411,7 @@ class Driver {
long filelen = new File(jarfile).length();
if (filelen <= 0) return "";
long skiplen = Math.max(0, filelen - tail.length);
InputStream in = new FileInputStream(new File(jarfile));
try {
try (InputStream in = new FileInputStream(new File(jarfile))) {
in.skip(skiplen);
in.read(tail);
for (int i = tail.length-4; i >= 0; i--) {
......@@ -426,8 +425,6 @@ class Driver {
}
}
return "";
} finally {
in.close();
}
}
......
......@@ -241,9 +241,9 @@ class NativeUnpack {
void run(File inFile, JarOutputStream jstream) throws IOException {
// %%% maybe memory-map the file, and pass it straight into unpacker
ByteBuffer mappedFile = null;
FileInputStream fis = new FileInputStream(inFile);
run(fis, jstream, mappedFile);
fis.close();
try (FileInputStream fis = new FileInputStream(inFile)) {
run(fis, jstream, mappedFile);
}
// Note: caller is responsible to finish with jstream.
}
......
......@@ -540,9 +540,9 @@ class PackageReader extends BandStructure {
Index index = initCPIndex(tag, cpMap);
if (optDumpBands) {
PrintStream ps = new PrintStream(getDumpStream(index, ".idx"));
printArrayTo(ps, index.cpMap, 0, index.cpMap.length);
ps.close();
try (PrintStream ps = new PrintStream(getDumpStream(index, ".idx"))) {
printArrayTo(ps, index.cpMap, 0, index.cpMap.length);
}
}
}
......@@ -828,26 +828,27 @@ class PackageReader extends BandStructure {
attr_definition_headers.readFrom(in);
attr_definition_name.readFrom(in);
attr_definition_layout.readFrom(in);
PrintStream dump = !optDumpBands ? null
: new PrintStream(getDumpStream(attr_definition_headers, ".def"));
for (int i = 0; i < numAttrDefs; i++) {
int header = attr_definition_headers.getByte();
Utf8Entry name = (Utf8Entry) attr_definition_name.getRef();
Utf8Entry layout = (Utf8Entry) attr_definition_layout.getRef();
int ctype = (header & ADH_CONTEXT_MASK);
int index = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
Attribute.Layout def = new Attribute.Layout(ctype,
name.stringValue(),
layout.stringValue());
// Check layout string for Java 6 extensions.
String pvLayout = def.layoutForPackageMajver(getPackageMajver());
if (!pvLayout.equals(def.layout())) {
throw new IOException("Bad attribute layout in version 150 archive: "+def.layout());
try (PrintStream dump = !optDumpBands ? null
: new PrintStream(getDumpStream(attr_definition_headers, ".def")))
{
for (int i = 0; i < numAttrDefs; i++) {
int header = attr_definition_headers.getByte();
Utf8Entry name = (Utf8Entry) attr_definition_name.getRef();
Utf8Entry layout = (Utf8Entry) attr_definition_layout.getRef();
int ctype = (header & ADH_CONTEXT_MASK);
int index = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
Attribute.Layout def = new Attribute.Layout(ctype,
name.stringValue(),
layout.stringValue());
// Check layout string for Java 6 extensions.
String pvLayout = def.layoutForPackageMajver(getPackageMajver());
if (!pvLayout.equals(def.layout())) {
throw new IOException("Bad attribute layout in version 150 archive: "+def.layout());
}
this.setAttributeLayoutIndex(def, index);
if (dump != null) dump.println(index+" "+def);
}
this.setAttributeLayoutIndex(def, index);
if (dump != null) dump.println(index+" "+def);
}
if (dump != null) dump.close();
attr_definition_headers.doneDisbursing();
attr_definition_name.doneDisbursing();
attr_definition_layout.doneDisbursing();
......
......@@ -458,9 +458,9 @@ class PackageWriter extends BandStructure {
Utils.log.info("Writing "+cpMap.length+" "+ConstantPool.tagName(tag)+" entries...");
if (optDumpBands) {
PrintStream ps = new PrintStream(getDumpStream(index, ".idx"));
printArrayTo(ps, cpMap, 0, cpMap.length);
ps.close();
try (PrintStream ps = new PrintStream(getDumpStream(index, ".idx"))) {
printArrayTo(ps, cpMap, 0, cpMap.length);
}
}
switch (tag) {
......@@ -923,33 +923,34 @@ class PackageWriter extends BandStructure {
}
});
attrDefsWritten = new Attribute.Layout[numAttrDefs];
PrintStream dump = !optDumpBands ? null
: new PrintStream(getDumpStream(attr_definition_headers, ".def"));
int[] indexForDebug = Arrays.copyOf(attrIndexLimit, ATTR_CONTEXT_LIMIT);
for (int i = 0; i < defs.length; i++) {
int header = ((Integer)defs[i][0]).intValue();
Attribute.Layout def = (Attribute.Layout) defs[i][1];
attrDefsWritten[i] = def;
assert((header & ADH_CONTEXT_MASK) == def.ctype());
attr_definition_headers.putByte(header);
attr_definition_name.putRef(ConstantPool.getUtf8Entry(def.name()));
String layout = def.layoutForPackageMajver(getPackageMajver());
attr_definition_layout.putRef(ConstantPool.getUtf8Entry(layout));
// Check that we are transmitting that correct attribute index:
boolean debug = false;
assert(debug = true);
if (debug) {
int hdrIndex = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
if (hdrIndex < 0) hdrIndex = indexForDebug[def.ctype()]++;
int realIndex = (attrIndexTable.get(def)).intValue();
assert(hdrIndex == realIndex);
}
if (dump != null) {
int index = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
dump.println(index+" "+def);
try (PrintStream dump = !optDumpBands ? null
: new PrintStream(getDumpStream(attr_definition_headers, ".def")))
{
int[] indexForDebug = Arrays.copyOf(attrIndexLimit, ATTR_CONTEXT_LIMIT);
for (int i = 0; i < defs.length; i++) {
int header = ((Integer)defs[i][0]).intValue();
Attribute.Layout def = (Attribute.Layout) defs[i][1];
attrDefsWritten[i] = def;
assert((header & ADH_CONTEXT_MASK) == def.ctype());
attr_definition_headers.putByte(header);
attr_definition_name.putRef(ConstantPool.getUtf8Entry(def.name()));
String layout = def.layoutForPackageMajver(getPackageMajver());
attr_definition_layout.putRef(ConstantPool.getUtf8Entry(layout));
// Check that we are transmitting that correct attribute index:
boolean debug = false;
assert(debug = true);
if (debug) {
int hdrIndex = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
if (hdrIndex < 0) hdrIndex = indexForDebug[def.ctype()]++;
int realIndex = (attrIndexTable.get(def)).intValue();
assert(hdrIndex == realIndex);
}
if (dump != null) {
int index = (header >> ADH_BIT_SHIFT) - ADH_BIT_IS_LSB;
dump.println(index+" "+def);
}
}
}
if (dump != null) dump.close();
}
void writeAttrCounts() throws IOException {
......
......@@ -122,26 +122,23 @@ final class PropMap implements SortedMap<Object, Object> {
// Define certain attribute layouts by default.
// Do this after the previous props are put in place,
// to allow override if necessary.
InputStream propStr = null;
try {
String propFile = "intrinsic.properties";
propStr = PackerImpl.class.getResourceAsStream(propFile);
props.load(new BufferedInputStream(propStr));
for (Map.Entry<Object, Object> e : props.entrySet()) {
String key = (String) e.getKey();
String val = (String) e.getValue();
if (key.startsWith("attribute.")) {
e.setValue(Attribute.normalizeLayoutString(val));
}
String propFile = "intrinsic.properties";
try (InputStream propStr = PackerImpl.class.getResourceAsStream(propFile)) {
if (propStr == null) {
throw new RuntimeException(propFile + " cannot be loaded");
}
props.load(propStr);
} catch (IOException ee) {
throw new RuntimeException(ee);
} finally {
try {
if (propStr != null) {
propStr.close();
}
} catch (IOException ignore) {}
}
for (Map.Entry<Object, Object> e : props.entrySet()) {
String key = (String) e.getKey();
String val = (String) e.getValue();
if (key.startsWith("attribute.")) {
e.setValue(Attribute.normalizeLayoutString(val));
}
}
defaultProps = (new HashMap<>(props)); // shrink to fit
......
......@@ -161,8 +161,9 @@ public class UnpackerImpl extends TLGlobals implements Pack200.Unpacker {
}
// Use the stream-based implementation.
// %%% Reconsider if native unpacker learns to memory-map the file.
FileInputStream instr = new FileInputStream(in);
unpack(instr, out);
try (FileInputStream instr = new FileInputStream(in)) {
unpack(instr, out);
}
if (props.getBoolean(Utils.UNPACK_REMOVE_PACKFILE)) {
in.delete();
}
......
......@@ -268,18 +268,18 @@ class Utils {
// 4947205 : Peformance is slow when using pack-effort=0
out = new BufferedOutputStream(out);
out = new NonCloser(out); // protect from JarOutputStream.close()
JarOutputStream jout = new JarOutputStream(out);
copyJarFile(in, jout);
jout.close();
try (JarOutputStream jout = new JarOutputStream(out)) {
copyJarFile(in, jout);
}
}
static void copyJarFile(JarFile in, OutputStream out) throws IOException {
// 4947205 : Peformance is slow when using pack-effort=0
out = new BufferedOutputStream(out);
out = new NonCloser(out); // protect from JarOutputStream.close()
JarOutputStream jout = new JarOutputStream(out);
copyJarFile(in, jout);
jout.close();
try (JarOutputStream jout = new JarOutputStream(out)) {
copyJarFile(in, jout);
}
}
// Wrapper to prevent closing of client-supplied stream.
static private
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册