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 /ddms/libs | |
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 'ddms/libs')
6 files changed, 113 insertions, 79 deletions
diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/EmulatorConsole.java b/ddms/libs/ddmlib/src/com/android/ddmlib/EmulatorConsole.java index f3ab28c..2f4175f 100644 --- a/ddms/libs/ddmlib/src/com/android/ddmlib/EmulatorConsole.java +++ b/ddms/libs/ddmlib/src/com/android/ddmlib/EmulatorConsole.java @@ -532,9 +532,13 @@ public final class EmulatorConsole { // need to make sure the string format uses dot and not comma Formatter formatter = new Formatter(Locale.US); - formatter.format(COMMAND_GPS, longitude, latitude, elevation); + try { + formatter.format(COMMAND_GPS, longitude, latitude, elevation); - return processCommand(formatter.toString()); + return processCommand(formatter.toString()); + } finally { + formatter.close(); + } } /** diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/SyncService.java b/ddms/libs/ddmlib/src/com/android/ddmlib/SyncService.java index bf0b4e1..f207567 100644 --- a/ddms/libs/ddmlib/src/com/android/ddmlib/SyncService.java +++ b/ddms/libs/ddmlib/src/com/android/ddmlib/SyncService.java @@ -523,51 +523,55 @@ public final class SyncService { FileOutputStream fos = null; try { fos = new FileOutputStream(f); - } catch (IOException e) { - Log.e("ddms", String.format("Failed to open local file %s for writing, Reason: %s", - f.getAbsolutePath(), e.toString())); - throw new SyncException(SyncError.FILE_WRITE_ERROR); - } - // the buffer to read the data - byte[] data = new byte[SYNC_DATA_MAX]; + // the buffer to read the data + byte[] data = new byte[SYNC_DATA_MAX]; - // loop to get data until we're done. - while (true) { - // check if we're cancelled - if (monitor.isCanceled() == true) { - throw new SyncException(SyncError.CANCELED); - } + // loop to get data until we're done. + while (true) { + // check if we're cancelled + if (monitor.isCanceled() == true) { + throw new SyncException(SyncError.CANCELED); + } - // if we're done, we stop the loop - if (checkResult(pullResult, ID_DONE)) { - break; - } - if (checkResult(pullResult, ID_DATA) == false) { - // hmm there's an error - throw new SyncException(SyncError.TRANSFER_PROTOCOL_ERROR, - readErrorMessage(pullResult, timeOut)); - } - int length = ArrayHelper.swap32bitFromArray(pullResult, 4); - if (length > SYNC_DATA_MAX) { - // buffer overrun! - // error and exit - throw new SyncException(SyncError.BUFFER_OVERRUN); - } + // if we're done, we stop the loop + if (checkResult(pullResult, ID_DONE)) { + break; + } + if (checkResult(pullResult, ID_DATA) == false) { + // hmm there's an error + throw new SyncException(SyncError.TRANSFER_PROTOCOL_ERROR, + readErrorMessage(pullResult, timeOut)); + } + int length = ArrayHelper.swap32bitFromArray(pullResult, 4); + if (length > SYNC_DATA_MAX) { + // buffer overrun! + // error and exit + throw new SyncException(SyncError.BUFFER_OVERRUN); + } - // now read the length we received - AdbHelper.read(mChannel, data, length, timeOut); + // now read the length we received + AdbHelper.read(mChannel, data, length, timeOut); - // get the header for the next packet. - AdbHelper.read(mChannel, pullResult, -1, timeOut); + // get the header for the next packet. + AdbHelper.read(mChannel, pullResult, -1, timeOut); - // write the content in the file - fos.write(data, 0, length); + // write the content in the file + fos.write(data, 0, length); - monitor.advance(length); - } + monitor.advance(length); + } - fos.flush(); + fos.flush(); + } catch (IOException e) { + Log.e("ddms", String.format("Failed to open local file %s for writing, Reason: %s", + f.getAbsolutePath(), e.toString())); + throw new SyncException(SyncError.FILE_WRITE_ERROR); + } finally { + if (fos != null) { + fos.close(); + } + } } @@ -637,48 +641,51 @@ public final class SyncService { // create the header for the action msg = createSendFileReq(ID_SEND, remotePathContent, 0644); - } catch (UnsupportedEncodingException e) { - throw new SyncException(SyncError.REMOTE_PATH_ENCODING, e); - } - - // and send it. We use a custom try/catch block to make the difference between - // file and network IO exceptions. - AdbHelper.write(mChannel, msg, -1, timeOut); - // create the buffer used to read. - // we read max SYNC_DATA_MAX, but we need 2 4 bytes at the beginning. - if (mBuffer == null) { - mBuffer = new byte[SYNC_DATA_MAX + 8]; - } - System.arraycopy(ID_DATA, 0, mBuffer, 0, ID_DATA.length); + // and send it. We use a custom try/catch block to make the difference between + // file and network IO exceptions. + AdbHelper.write(mChannel, msg, -1, timeOut); - // look while there is something to read - while (true) { - // check if we're canceled - if (monitor.isCanceled() == true) { - throw new SyncException(SyncError.CANCELED); + // create the buffer used to read. + // we read max SYNC_DATA_MAX, but we need 2 4 bytes at the beginning. + if (mBuffer == null) { + mBuffer = new byte[SYNC_DATA_MAX + 8]; } + System.arraycopy(ID_DATA, 0, mBuffer, 0, ID_DATA.length); - // read up to SYNC_DATA_MAX - int readCount = fis.read(mBuffer, 8, SYNC_DATA_MAX); + // look while there is something to read + while (true) { + // check if we're canceled + if (monitor.isCanceled() == true) { + throw new SyncException(SyncError.CANCELED); + } - if (readCount == -1) { - // we reached the end of the file - break; - } + // read up to SYNC_DATA_MAX + int readCount = fis.read(mBuffer, 8, SYNC_DATA_MAX); - // now send the data to the device - // first write the amount read - ArrayHelper.swap32bitsToArray(readCount, mBuffer, 4); + if (readCount == -1) { + // we reached the end of the file + break; + } - // now write it - AdbHelper.write(mChannel, mBuffer, readCount+8, timeOut); + // now send the data to the device + // first write the amount read + ArrayHelper.swap32bitsToArray(readCount, mBuffer, 4); - // and advance the monitor - monitor.advance(readCount); + // now write it + AdbHelper.write(mChannel, mBuffer, readCount+8, timeOut); + + // and advance the monitor + monitor.advance(readCount); + } + } catch (UnsupportedEncodingException e) { + throw new SyncException(SyncError.REMOTE_PATH_ENCODING, e); + } finally { + // close the local file + if (fis != null) { + fis.close(); + } } - // close the local file - fis.close(); // create the DONE message long time = System.currentTimeMillis() / 1000; diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/log/EventLogParser.java b/ddms/libs/ddmlib/src/com/android/ddmlib/log/EventLogParser.java index 22c0703..b2d8256 100644 --- a/ddms/libs/ddmlib/src/com/android/ddmlib/log/EventLogParser.java +++ b/ddms/libs/ddmlib/src/com/android/ddmlib/log/EventLogParser.java @@ -124,8 +124,9 @@ public final class EventLogParser { * @return <code>true</code> if success, <code>false</code> if failure. */ public boolean init(String filePath) { + BufferedReader reader = null; try { - BufferedReader reader = new BufferedReader(new FileReader(filePath)); + reader = new BufferedReader(new FileReader(filePath)); String line = null; do { @@ -138,6 +139,14 @@ public final class EventLogParser { return true; } catch (IOException e) { return false; + } finally { + try { + if (reader != null) { + reader.close(); + } + } catch (IOException e) { + // ignore + } } } diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogPanel.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogPanel.java index 4faac3a..937ee40 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogPanel.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventLogPanel.java @@ -914,10 +914,13 @@ public class EventLogPanel extends TablePanel implements ILogListener, byte[] buffer = new byte[256]; FileInputStream fis = new FileInputStream(fileName); - - int count; - while ((count = fis.read(buffer)) != -1) { - logReceiver.parseNewData(buffer, 0, count); + try { + int count; + while ((count = fis.read(buffer)) != -1) { + logReceiver.parseNewData(buffer, 0, count); + } + } finally { + fis.close(); } } diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogPanel.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogPanel.java index d60bae8..a347155 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogPanel.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/logcat/LogPanel.java @@ -730,8 +730,9 @@ public class LogPanel extends SelectionDependentPanel { Arrays.sort(selection); // loop on the selection and output the file. + FileWriter writer = null; try { - FileWriter writer = new FileWriter(fileName); + writer = new FileWriter(fileName); for (int i : selection) { TableItem item = currentTable.getItem(i); @@ -744,6 +745,14 @@ public class LogPanel extends SelectionDependentPanel { } catch (IOException e) { return false; + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException e) { + // ignore + } + } } } } diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/net/NetworkPanel.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/net/NetworkPanel.java index f8cb7a3..febd99c 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/net/NetworkPanel.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/net/NetworkPanel.java @@ -439,7 +439,9 @@ public class NetworkPanel extends TablePanel { } else { final int size = mTrackedItems.size(); item.color = nextSeriesColor(size); - item.label = "0x" + new Formatter().format("%08x", tag); + Formatter formatter = new Formatter(); + item.label = "0x" + formatter.format("%08x", tag); + formatter.close(); } // create color chip to display as legend in table |