From a0b334f10eedb58bb5bbcf1fbf05054e93927988 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 13 May 2015 15:40:24 +0200 Subject: Fix quality flaws --- .../org/sonar/batch/protocol/ProtobufUtil.java | 34 ++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'sonar-batch-protocol') 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 readFile(File file, Parser 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 void writeMessagesToFile(Iterable 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); } } -- cgit v1.2.3