Skip to content

Commit

Permalink
Use try-with-resources, skip bytes on the second pass
Browse files Browse the repository at this point in the history
  • Loading branch information
dkomanov committed Sep 16, 2024
1 parent 20ca6b3 commit d40270c
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/main/java/edu/tufts/eaftan/hprofparser/parser/HprofParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,37 @@ public void parse(File file) throws IOException {
* [u1]* - body
*/

FileInputStream fs = new FileInputStream(file);
DataInputStream in = new DataInputStream(new BufferedInputStream(fs));

// header
String format = readUntilNull(in);
int idSize = in.readInt();
long startTime = in.readLong();
handler.header(format, idSize, startTime);

// records
boolean done;
do {
done = parseRecord(in, idSize, true);
} while (!done);
in.close();

FileInputStream fsSecond = new FileInputStream(file);
DataInputStream inSecond = new DataInputStream(new BufferedInputStream(fsSecond));
readUntilNull(inSecond); // format
inSecond.readInt(); // idSize
inSecond.readLong(); // startTime
do {
done = parseRecord(inSecond, idSize, false);
} while (!done);
inSecond.close();
String format;
int idSize;
long startTime;
try (
FileInputStream fs = new FileInputStream(file);
DataInputStream in = new DataInputStream(new BufferedInputStream(fs));
) {
// header
format = readUntilNull(in);
idSize = in.readInt();
startTime = in.readLong();
handler.header(format, idSize, startTime);

// records
boolean done;
do {
done = parseRecord(in, idSize, true);
} while (!done);
}

try (
FileInputStream fs = new FileInputStream(file);
DataInputStream in = new DataInputStream(new BufferedInputStream(fs));
) {
in.skipBytes(format.length() + 1 + 4 + 8); // skip header (1 for null terminator)
boolean done;
do {
done = parseRecord(in, idSize, false);
} while (!done);
}

handler.finished();
}

Expand Down

0 comments on commit d40270c

Please sign in to comment.