diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PropertiesStrategy.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PropertiesStrategy.java index d6fdff86..f635a748 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PropertiesStrategy.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/PropertiesStrategy.java @@ -92,7 +92,12 @@ private Resource loadResource(String docName, File propFile) throws IOException, RuntimeException { Resource doc = new Resource(docName); // doc.setContentType(contentType); - propReader.extractTemplate(doc, new FileInputStream(propFile)); + FileInputStream in = new FileInputStream(propFile); + try { + propReader.extractTemplate(doc, in); + } finally { + in.close(); + } return doc; } @@ -107,7 +112,12 @@ public Resource loadSrcDoc(File sourceDir, String docName) private TranslationsResource loadTranslationsResource(Resource srcDoc, File transFile) throws IOException, RuntimeException { TranslationsResource targetDoc = new TranslationsResource(); - propReader.extractTarget(targetDoc, new FileInputStream(transFile)); + FileInputStream in = new FileInputStream(transFile); + try { + propReader.extractTarget(targetDoc, in); + } finally { + in.close(); + } return targetDoc; } diff --git a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java index 4fcf58d3..b9a50fef 100644 --- a/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java +++ b/zanata-client-commands/src/main/java/org/zanata/client/commands/push/RawPushCommand.java @@ -21,6 +21,7 @@ package org.zanata.client.commands.push; import java.io.ByteArrayInputStream; +import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -301,57 +302,59 @@ private boolean pushSourceDocumentToServer(File sourceDir, */ private void pushDocumentToServer(String docId, String fileType, String locale, File docFile) { - String md5hash = calculateFileHash(docFile); - if (docFile.length() <= getOpts().getChunkSize()) { - log.info(" transmitting file [{}] as single chunk", - docFile.getAbsolutePath()); - InputStream fileStream; - try { - fileStream = new FileInputStream(docFile); - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } - DocumentFileUploadForm uploadForm = - generateUploadForm(true, true, fileType, md5hash, - docFile.length(), fileStream); - ClientResponse response = - uploadDocumentPart(docId, locale, uploadForm); - checkChunkUploadStatus(response); - } else { - StreamChunker chunker; - try { - chunker = new StreamChunker(docFile, getOpts().getChunkSize()); - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } - log.info(" transmitting file [{}] as {} chunks", - docFile.getAbsolutePath(), chunker.totalChunks()); - ClientResponse uploadResponse; - DocumentFileUploadForm uploadForm; - Long uploadId = null; - - for (InputStream chunkStream : chunker) { - log.info(" pushing chunk {} of {}", - chunker.currentChunkNumber(), chunker.totalChunks()); - boolean isFirst = chunker.currentChunkNumber() == 1; - boolean isLast = chunker.getRemainingChunks() == 0; - long chunkSize = chunker.currentChunkSize(); - uploadForm = - generateUploadForm(isFirst, isLast, fileType, md5hash, - chunkSize, chunkStream); - if (!isFirst) { - uploadForm.setUploadId(uploadId); + try { + String md5hash = calculateFileHash(docFile); + if (docFile.length() <= getOpts().getChunkSize()) { + log.info(" transmitting file [{}] as single chunk", + docFile.getAbsolutePath()); + InputStream fileStream = new FileInputStream(docFile); + try { + DocumentFileUploadForm uploadForm = + generateUploadForm(true, true, fileType, md5hash, + docFile.length(), fileStream); + ClientResponse response = + uploadDocumentPart(docId, locale, uploadForm); + checkChunkUploadStatus(response); + } finally { + fileStream.close(); } - uploadResponse = uploadDocumentPart(docId, locale, uploadForm); - checkChunkUploadStatus(uploadResponse); - if (isFirst) { - uploadId = uploadResponse.getEntity().getUploadId(); - if (uploadId == null) { - throw new RuntimeException( - "server did not return upload id"); + } else { + StreamChunker chunker = new StreamChunker(docFile, getOpts().getChunkSize()); + try { + log.info(" transmitting file [{}] as {} chunks", + docFile.getAbsolutePath(), chunker.totalChunks()); + ClientResponse uploadResponse; + DocumentFileUploadForm uploadForm; + Long uploadId = null; + + for (InputStream chunkStream : chunker) { + log.info(" pushing chunk {} of {}", + chunker.currentChunkNumber(), chunker.totalChunks()); + boolean isFirst = chunker.currentChunkNumber() == 1; + boolean isLast = chunker.getRemainingChunks() == 0; + long chunkSize = chunker.currentChunkSize(); + uploadForm = + generateUploadForm(isFirst, isLast, fileType, md5hash, + chunkSize, chunkStream); + if (!isFirst) { + uploadForm.setUploadId(uploadId); + } + uploadResponse = uploadDocumentPart(docId, locale, uploadForm); + checkChunkUploadStatus(uploadResponse); + if (isFirst) { + uploadId = uploadResponse.getEntity().getUploadId(); + if (uploadId == null) { + throw new RuntimeException( + "server did not return upload id"); + } + } } + } finally { + chunker.close(); } } + } catch (IOException e) { + throw new RuntimeException(e); } } @@ -397,16 +400,18 @@ private ClientResponse uploadDocumentPart( } private String calculateFileHash(File srcFile) { - InputStream fileStream; try { - fileStream = new FileInputStream(srcFile); MessageDigest md = MessageDigest.getInstance("MD5"); - fileStream = new DigestInputStream(fileStream, md); - byte[] buffer = new byte[256]; - while (fileStream.read(buffer) > 0) { - // continue + InputStream fileStream = new FileInputStream(srcFile); + try { + fileStream = new DigestInputStream(fileStream, md); + byte[] buffer = new byte[256]; + while (fileStream.read(buffer) > 0) { + // continue + } + } finally { + fileStream.close(); } - fileStream.close(); String md5hash = new String(Hex.encodeHex(md.digest())); return md5hash; } catch (FileNotFoundException e) { @@ -418,8 +423,8 @@ private String calculateFileHash(File srcFile) { } } - private static class StreamChunker implements Iterable { - + private static class StreamChunker implements Iterable, + Closeable { private int totalChunkCount; private int chunksRetrieved; @@ -431,12 +436,17 @@ private static class StreamChunker implements Iterable { public StreamChunker(File file, int chunkSize) throws FileNotFoundException { this.file = file; - fileStream = new FileInputStream(file); buffer = new byte[chunkSize]; chunksRetrieved = 0; totalChunkCount = (int) (file.length() / chunkSize + (file.length() % chunkSize == 0 ? 0 : 1)); + fileStream = new FileInputStream(file); + } + + @Override + public void close() throws IOException { + fileStream.close(); } public int totalChunks() {