diff options
author | Tor Norbye <tnorbye@google.com> | 2012-05-30 16:06:03 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-05-30 17:33:18 -0700 |
commit | 547c7761208632134d33eace29c81a4e60cd0a69 (patch) | |
tree | 84ae7260854779022d621ae2e3001ab351244105 /traceview | |
parent | 7e4b8e9d595e45baa9d87cdb8282f02759e73abc (diff) | |
download | sdk-547c7761208632134d33eace29c81a4e60cd0a69.zip sdk-547c7761208632134d33eace29c81a4e60cd0a69.tar.gz sdk-547c7761208632134d33eace29c81a4e60cd0a69.tar.bz2 |
Fix "Resource leak: <Foo> is never closed"
This changeset fixes various code fragments opening
resources without closing them.
Change-Id: I6ed48a32dc5de4c11cab394dd3883ebbb54d2938
Diffstat (limited to 'traceview')
-rw-r--r-- | traceview/src/com/android/traceview/DmTraceReader.java | 101 | ||||
-rw-r--r-- | traceview/src/com/android/traceview/MainWindow.java | 42 |
2 files changed, 84 insertions, 59 deletions
diff --git a/traceview/src/com/android/traceview/DmTraceReader.java b/traceview/src/com/android/traceview/DmTraceReader.java index 285897b..b49d75e 100644 --- a/traceview/src/com/android/traceview/DmTraceReader.java +++ b/traceview/src/com/android/traceview/DmTraceReader.java @@ -103,12 +103,16 @@ public class DmTraceReader extends TraceReader { private MappedByteBuffer mapFile(String filename, long offset) throws IOException { MappedByteBuffer buffer = null; FileInputStream dataFile = new FileInputStream(filename); - File file = new File(filename); - FileChannel fc = dataFile.getChannel(); - buffer = fc.map(FileChannel.MapMode.READ_ONLY, offset, file.length() - offset); - buffer.order(ByteOrder.LITTLE_ENDIAN); + try { + File file = new File(filename); + FileChannel fc = dataFile.getChannel(); + buffer = fc.map(FileChannel.MapMode.READ_ONLY, offset, file.length() - offset); + buffer.order(ByteOrder.LITTLE_ENDIAN); - return buffer; + return buffer; + } finally { + dataFile.close(); // this *also* closes the associated channel, fc + } } private void readDataFileHeader(MappedByteBuffer buffer) { @@ -402,63 +406,68 @@ public class DmTraceReader extends TraceReader { static final int PARSE_OPTIONS = 4; long parseKeys() throws IOException { + long offset = 0; BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader( new FileInputStream(mTraceFileName), "US-ASCII")); - } catch (FileNotFoundException ex) { - System.err.println(ex.getMessage()); - } - long offset = 0; - int mode = PARSE_VERSION; - String line = null; - while (true) { - line = in.readLine(); - if (line == null) { - throw new IOException("Key section does not have an *end marker"); - } - - // Calculate how much we have read from the file so far. The - // extra byte is for the line ending not included by readLine(). - offset += line.length() + 1; - if (line.startsWith("*")) { - if (line.equals("*version")) { - mode = PARSE_VERSION; - continue; - } - if (line.equals("*threads")) { - mode = PARSE_THREADS; - continue; + int mode = PARSE_VERSION; + String line = null; + while (true) { + line = in.readLine(); + if (line == null) { + throw new IOException("Key section does not have an *end marker"); } - if (line.equals("*methods")) { - mode = PARSE_METHODS; - continue; + + // Calculate how much we have read from the file so far. The + // extra byte is for the line ending not included by readLine(). + offset += line.length() + 1; + if (line.startsWith("*")) { + if (line.equals("*version")) { + mode = PARSE_VERSION; + continue; + } + if (line.equals("*threads")) { + mode = PARSE_THREADS; + continue; + } + if (line.equals("*methods")) { + mode = PARSE_METHODS; + continue; + } + if (line.equals("*end")) { + break; + } } - if (line.equals("*end")) { + switch (mode) { + case PARSE_VERSION: + mVersionNumber = Integer.decode(line); + mode = PARSE_OPTIONS; + break; + case PARSE_THREADS: + parseThread(line); + break; + case PARSE_METHODS: + parseMethod(line); + break; + case PARSE_OPTIONS: + parseOption(line); break; } } - switch (mode) { - case PARSE_VERSION: - mVersionNumber = Integer.decode(line); - mode = PARSE_OPTIONS; - break; - case PARSE_THREADS: - parseThread(line); - break; - case PARSE_METHODS: - parseMethod(line); - break; - case PARSE_OPTIONS: - parseOption(line); - break; + } catch (FileNotFoundException ex) { + System.err.println(ex.getMessage()); + } finally { + if (in != null) { + in.close(); } } if (mClockSource == null) { mClockSource = ClockSource.THREAD_CPU; } + return offset; } diff --git a/traceview/src/com/android/traceview/MainWindow.java b/traceview/src/com/android/traceview/MainWindow.java index 3414d84..ebab72b 100644 --- a/traceview/src/com/android/traceview/MainWindow.java +++ b/traceview/src/com/android/traceview/MainWindow.java @@ -146,20 +146,36 @@ public class MainWindow extends ApplicationWindow { // write into it. File temp = File.createTempFile(base, ".trace"); temp.deleteOnExit(); - FileChannel dstChannel = new FileOutputStream(temp).getChannel(); - // First copy the contents of the key file into our temp file. - FileChannel srcChannel = new FileInputStream(base + ".key").getChannel(); - long size = dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); - srcChannel.close(); - - // Then concatenate the data file. - srcChannel = new FileInputStream(base + ".data").getChannel(); - dstChannel.transferFrom(srcChannel, size, srcChannel.size()); - - // Clean up. - srcChannel.close(); - dstChannel.close(); + FileOutputStream dstStream = null; + FileInputStream keyStream = null; + FileInputStream dataStream = null; + + try { + dstStream = new FileOutputStream(temp); + FileChannel dstChannel = dstStream.getChannel(); + + // First copy the contents of the key file into our temp file. + keyStream = new FileInputStream(base + ".key"); + FileChannel srcChannel = keyStream.getChannel(); + long size = dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); + srcChannel.close(); + + // Then concatenate the data file. + dataStream = new FileInputStream(base + ".data"); + srcChannel = dataStream.getChannel(); + dstChannel.transferFrom(srcChannel, size, srcChannel.size()); + } finally { + if (dstStream != null) { + dstStream.close(); // also closes dstChannel + } + if (keyStream != null) { + keyStream.close(); // also closes srcChannel + } + if (dataStream != null) { + dataStream.close(); + } + } // Return the path of the temp file. return temp.getPath(); |