Fixed some minor potential resource leaks.

This commit is contained in:
Ian Brandt 2012-10-30 23:26:21 -07:00
parent 0d24df7628
commit 35f04b4df6
2 changed files with 45 additions and 32 deletions

View File

@ -67,42 +67,51 @@ public class TsvToJson
} }
} }
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), Charsets.UTF_8)); BufferedReader in = null;
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), Charsets.UTF_8)); BufferedWriter out = null;
String line = null; try {
int count = 0; in = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), Charsets.UTF_8));
long currTime = System.currentTimeMillis(); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), Charsets.UTF_8));
long startTime = currTime; String line = null;
while ((line = in.readLine()) != null) { int count = 0;
if (count % 1000000 == 0) { long currTime = System.currentTimeMillis();
long nowTime = System.currentTimeMillis(); long startTime = currTime;
System.out.printf("Processed [%,d] lines in %,d millis. Incremental time %,d millis.%n", count, nowTime - startTime, nowTime - currTime); while ((line = in.readLine()) != null) {
currTime = nowTime; if (count % 1000000 == 0) {
} long nowTime = System.currentTimeMillis();
++count; System.out.printf("Processed [%,d] lines in %,d millis. Incremental time %,d millis.%n", count, nowTime - startTime, nowTime - currTime);
String[] splits = line.split("\t"); currTime = nowTime;
}
++count;
String[] splits = line.split("\t");
if (splits.length == 30) { if (splits.length == 30) {
continue; continue;
} }
if (splits.length != handlers.length) { if (splits.length != handlers.length) {
throw new IAE("splits.length[%d] != handlers.length[%d]; line[%s]", splits.length, handlers.length, line); throw new IAE("splits.length[%d] != handlers.length[%d]; line[%s]", splits.length, handlers.length, line);
} }
Map<String, Object> jsonMap = Maps.newLinkedHashMap(); Map<String, Object> jsonMap = Maps.newLinkedHashMap();
for (int i = 0; i < handlers.length; ++i) { for (int i = 0; i < handlers.length; ++i) {
jsonMap.put(handlers[i].getFieldName(), handlers[i].process(splits[i])); jsonMap.put(handlers[i].getFieldName(), handlers[i].process(splits[i]));
} }
final String str = mapper.writeValueAsString(jsonMap); final String str = mapper.writeValueAsString(jsonMap);
out.write(str); out.write(str);
out.write("\n"); out.write("\n");
}
System.out.printf("Completed %,d lines in %,d millis.%n", count, System.currentTimeMillis() - startTime);
out.flush();
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} }
System.out.printf("Completed %,d lines in %,d millis.%n", count, System.currentTimeMillis() - startTime);
out.flush();
out.close();
in.close();
} }
public static interface FieldHandler public static interface FieldHandler

View File

@ -416,9 +416,11 @@ public class IndexMerger
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
File indexFile = new File(outDir, "index.drd"); File indexFile = new File(outDir, "index.drd");
FileOutputStream fileOutputStream = null;
FileChannel channel = null; FileChannel channel = null;
try { try {
channel = new FileOutputStream(indexFile).getChannel(); fileOutputStream = new FileOutputStream(indexFile);
channel = fileOutputStream.getChannel();
channel.write(ByteBuffer.wrap(new byte[]{IndexIO.CURRENT_VERSION_ID})); channel.write(ByteBuffer.wrap(new byte[]{IndexIO.CURRENT_VERSION_ID}));
GenericIndexed.fromIterable(mergedDimensions, GenericIndexed.stringStrategy).writeToChannel(channel); GenericIndexed.fromIterable(mergedDimensions, GenericIndexed.stringStrategy).writeToChannel(channel);
@ -438,6 +440,8 @@ public class IndexMerger
finally { finally {
Closeables.closeQuietly(channel); Closeables.closeQuietly(channel);
channel = null; channel = null;
Closeables.closeQuietly(fileOutputStream);
fileOutputStream = null;
} }
IndexIO.checkFileSize(indexFile); IndexIO.checkFileSize(indexFile);
log.info("outDir[%s] completed index.drd in %,d millis.", outDir, System.currentTimeMillis() - startTime); log.info("outDir[%s] completed index.drd in %,d millis.", outDir, System.currentTimeMillis() - startTime);