diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-05-13 15:40:24 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-05-13 15:49:00 +0200 |
commit | a0b334f10eedb58bb5bbcf1fbf05054e93927988 (patch) | |
tree | e4c86a715575108352abac63b6fef06ba5e9e9d9 /sonar-batch-protocol | |
parent | 01b50ea9c1e4528e6fb998d3478dc4fd569f6e70 (diff) | |
download | sonarqube-a0b334f10eedb58bb5bbcf1fbf05054e93927988.tar.gz sonarqube-a0b334f10eedb58bb5bbcf1fbf05054e93927988.zip |
Fix quality flaws
Diffstat (limited to 'sonar-batch-protocol')
-rw-r--r-- | sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java index 3b85d604365..3962cfd4ed1 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java @@ -21,8 +21,16 @@ package org.sonar.batch.protocol; import com.google.protobuf.Message; import com.google.protobuf.Parser; +import org.apache.commons.io.IOUtils; -import java.io.*; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; public class ProtobufUtil { private ProtobufUtil() { @@ -30,36 +38,52 @@ public class ProtobufUtil { } public static <T extends Message> T readFile(File file, Parser<T> parser) { - try (InputStream input = new BufferedInputStream(new FileInputStream(file))) { + InputStream input = null; + try { + input = new BufferedInputStream(new FileInputStream(file)); return parser.parseFrom(input); } catch (IOException e) { throw new IllegalStateException("Failed to read file: " + file, e); + } finally { + IOUtils.closeQuietly(input); } } public static void writeToFile(Message message, File toFile) { - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(toFile, false))) { + OutputStream out = null; + try { + out = new BufferedOutputStream(new FileOutputStream(toFile, false)); message.writeTo(out); } catch (IOException e) { throw new IllegalStateException("Unable to write protocol buffer data to file " + toFile, e); + } finally { + IOUtils.closeQuietly(out); } } public static void appendToFile(Message message, File toFile) { - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(toFile, true))) { + OutputStream out = null; + try { + out = new BufferedOutputStream(new FileOutputStream(toFile, true)); message.writeDelimitedTo(out); } catch (IOException e) { throw new IllegalStateException("Unable to append protocol buffer data to file " + toFile, e); + } finally { + IOUtils.closeQuietly(out); } } public static <MESSAGE extends Message> void writeMessagesToFile(Iterable<MESSAGE> messages, File file) { - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { + OutputStream out = null; + try { + out = new BufferedOutputStream(new FileOutputStream(file, true)); for (MESSAGE message : messages) { message.writeDelimitedTo(out); } } catch (IOException e) { throw new IllegalStateException("Failed to read file: " + file, e); + } finally { + IOUtils.closeQuietly(out); } } |