Chromium Code Reviews| Index: tools/binary_size/java/src/org/chromium/tools/binary_size/NmDumper.java |
| diff --git a/tools/binary_size/java/src/org/chromium/tools/binary_size/NmDumper.java b/tools/binary_size/java/src/org/chromium/tools/binary_size/NmDumper.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c4803f83737e46a88b15de1a7e369625e26fa4bf |
| --- /dev/null |
| +++ b/tools/binary_size/java/src/org/chromium/tools/binary_size/NmDumper.java |
| @@ -0,0 +1,126 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +package org.chromium.tools.binary_size; |
| + |
| +import java.io.FileNotFoundException; |
| +import java.io.IOException; |
| +import java.io.PrintWriter; |
| + |
|
Peter Beverloo
2014/01/08 16:06:23
micro nit: double empty line.
|
| + |
| +class NmDumper { |
|
Peter Beverloo
2014/01/08 16:06:23
This class could use a sentence or two of document
|
| + private final String outPath; |
| + private final String skipPath; |
| + private final String failPath; |
| + private final Output output; |
| + |
| + NmDumper(final String outPath, final String failPath, final String skipPath) { |
| + this.outPath = outPath; |
| + this.failPath = failPath; |
| + this.skipPath = skipPath; |
| + output = new Output(); |
| + } |
| + |
| + void close() { |
| + output.closeAll(); |
| + } |
| + |
| + void skipped(String line) { |
| + output.printSkip(line); |
| + } |
| + |
| + void failed(Record record) { |
| + output.printFail(simulateNmOutput(record)); |
| + } |
| + |
| + void succeeded(Record record) { |
| + output.print(simulateNmOutput(record)); |
| + } |
| + |
| + private static final String simulateNmOutput(final Record record) { |
| + StringBuilder builder = new StringBuilder(record.address); |
| + builder.append(' '); |
| + builder.append(record.size); |
| + builder.append(' '); |
| + builder.append(record.symbolType); |
| + builder.append(' '); |
| + builder.append(record.symbolName != null ? record.symbolName : "unknown"); |
| + if (record.location != null) { |
| + builder.append('\t'); |
| + builder.append(record.location); |
| + } |
| + return builder.toString(); |
| + } |
| + |
| + private class Output { |
| + private final PrintWriter skipWriter; |
| + private final PrintWriter failWriter; |
| + private final PrintWriter outWriter; |
| + |
| + private Output() { |
| + try { |
| + outWriter = new PrintWriter(outPath); |
| + } catch (FileNotFoundException e) { |
| + throw new RuntimeException("Can't open output file: " + outPath, e); |
| + } |
| + |
| + if (failPath != null) { |
| + try { |
| + failWriter = new PrintWriter(failPath); |
| + } catch (FileNotFoundException e) { |
| + throw new RuntimeException("Can't open fail file: " + failPath, e); |
| + } |
| + } else { |
| + failWriter = null; |
| + } |
| + |
| + if (skipPath != null) { |
| + try { |
| + skipWriter = new PrintWriter(skipPath); |
| + } catch (IOException e) { |
| + throw new RuntimeException("Can't open skip file: " + skipPath, e); |
| + } |
| + } else { |
| + skipWriter = null; |
| + } |
| + } |
| + |
| + private synchronized void println(PrintWriter writer, String string) { |
|
Peter Beverloo
2014/01/08 16:06:23
nit: Since all callers have already been declared
|
| + if (writer != null) { |
| + writer.println(string); |
| + writer.flush(); |
| + } |
| + } |
| + |
| + synchronized void print(String string) { |
| + println(outWriter, string); |
| + } |
| + |
| + synchronized void printSkip(String string) { |
| + println(skipWriter, string); |
| + println(outWriter, string); |
| + } |
| + |
| + synchronized void printFail(String string) { |
| + println(failWriter, string); |
| + println(outWriter, string); |
| + } |
| + |
| + private void closeAll() { |
| + for (PrintWriter writer : new PrintWriter[]{outWriter, failWriter, skipWriter}) { |
| + if (writer != null) { |
|
Peter Beverloo
2014/01/08 16:06:23
We can avoid a level of nesting here by using the
|
| + try { |
| + writer.flush(); } |
|
Peter Beverloo
2014/01/08 16:06:23
nit: position of the }. On line 118 as well.
|
| + catch (Exception ignored) { |
| + // Nothing to be done. |
| + } |
| + try { |
| + writer.close(); } |
| + catch (Exception ignored) { |
| + // Nothing to be done. |
| + } |
| + } |
| + } |
| + } |
| + } |
| +} |