diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-02 16:25:50 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-02 16:31:38 +0200 |
commit | a18f0a986d74edad8d61d188334910093071c2a8 (patch) | |
tree | 0983850ca1f476872a3259c2bdfe150cc3d25b8e /sonar-batch-protocol/src/main | |
parent | 5904b952979414d585507a4f3ae66e29ca8ba6fc (diff) | |
download | sonarqube-a18f0a986d74edad8d61d188334910093071c2a8.tar.gz sonarqube-a18f0a986d74edad8d61d188334910093071c2a8.zip |
SONAR-6277 Replace ReportStream by CloseableIterator and remove FileStream (IOUtils.lineIterator should be used instead)
Diffstat (limited to 'sonar-batch-protocol/src/main')
5 files changed, 5 insertions, 203 deletions
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/FileStream.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/FileStream.java deleted file mode 100644 index b07daee9ee0..00000000000 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/FileStream.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.batch.protocol; - -import org.apache.commons.io.Charsets; -import org.apache.commons.io.IOUtils; - -import java.io.Closeable; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Iterator; - -/** - * An object to iterate over lines in a file. - * A LineStream is opened upon creation and is closed by invoking the close method. - * - * Inspired by {@link java.nio.file.DirectoryStream} - */ -public class FileStream implements Closeable, Iterable<String> { - - private final File file; - private InputStream inputStream; - - public FileStream(File file) { - this.file = file; - } - - @Override - public Iterator<String> iterator() { - if (this.inputStream != null) { - throw new IllegalStateException("Iterator already obtained"); - } else { - try { - this.inputStream = ProtobufUtil.createInputStream(file); - return IOUtils.lineIterator(inputStream, Charsets.UTF_8); - } catch (IOException e) { - throw new IllegalStateException("Failed to read lines from file " + file, e); - } - } - } - - @Override - public void close() { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - throw new IllegalStateException("Failed to close input stream of file " + file, e); - } - } - } -} 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 1e39578c692..a2579c07937 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 @@ -19,7 +19,6 @@ */ package org.sonar.batch.protocol; -import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.Message; import com.google.protobuf.Parser; @@ -38,22 +37,6 @@ public class ProtobufUtil { } } - static <T extends Message> T readInputStream(InputStream inputStream, Parser<T> parser) { - try { - return parser.parseDelimitedFrom(inputStream); - } catch (InvalidProtocolBufferException e) { - throw new IllegalStateException("Failed to read input stream", e); - } - } - - static InputStream createInputStream(File file) { - try { - return new BufferedInputStream(new FileInputStream(file)); - } catch (FileNotFoundException e) { - throw new IllegalStateException("Unable to find file " + file, e); - } - } - public static void writeToFile(Message message, File toFile) { try (OutputStream out = new BufferedOutputStream(new FileOutputStream(toFile, false))) { message.writeTo(out); diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java deleted file mode 100644 index 315bc42b2b6..00000000000 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.batch.protocol; - -import com.google.protobuf.Message; -import com.google.protobuf.Parser; - -import java.io.Closeable; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Iterator; -import java.util.NoSuchElementException; - -/** - * An object to iterate over protobuf messages in a file. - * A ReportStream is opened upon creation and is closed by invoking the close method. - * - * Warning, while it extends Iterable, it is not a general-purpose Iterable as it supports only a single Iterator; - * invoking the iterator method to obtain a second or subsequent iterator throws IllegalStateException. - * - * Inspired by {@link java.nio.file.DirectoryStream} - */ -public class ReportStream<R extends Message> implements Closeable, Iterable<R> { - - private final File file; - private final Parser<R> parser; - private InputStream inputStream; - - public ReportStream(File file, Parser<R> parser) { - this.file = file; - this.parser = parser; - } - - @Override - public Iterator<R> iterator() { - if (this.inputStream != null) { - throw new IllegalStateException("Iterator already obtained"); - } else { - this.inputStream = ProtobufUtil.createInputStream(file); - return new ReportIterator<>(inputStream, parser); - } - } - - @Override - public void close() { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - throw new IllegalStateException("Failed to close input stream of file " + file, e); - } - } - } - - private static class ReportIterator<R extends Message> implements Iterator<R> { - - private final Parser<R> parser; - private InputStream inputStream; - private R currentMessage; - - public ReportIterator(InputStream inputStream, Parser<R> parser) { - this.inputStream = inputStream; - this.parser = parser; - } - - @Override - public boolean hasNext() { - if (currentMessage == null) { - currentMessage = ProtobufUtil.readInputStream(inputStream, parser); - } - return currentMessage != null; - } - - @Override - public R next() { - if (!hasNext()) { - throw new NoSuchElementException(); - } - R messageToReturn = currentMessage; - currentMessage = null; - return messageToReturn; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - } -} diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java index ba90c68b4c8..90720f6e5e1 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java @@ -19,9 +19,7 @@ */ package org.sonar.batch.protocol.output; -import org.sonar.batch.protocol.FileStream; import org.sonar.batch.protocol.ProtobufUtil; -import org.sonar.batch.protocol.ReportStream; import org.sonar.batch.protocol.output.BatchReport.Issues; import javax.annotation.CheckForNull; @@ -128,20 +126,20 @@ public class BatchReportReader { } @CheckForNull - public ReportStream<BatchReport.Coverage> readFileCoverage(int fileRef) { + public File readFileCoverage(int fileRef) { File file = fileStructure.fileFor(FileStructure.Domain.COVERAGE, fileRef); if (doesFileExists(file)) { - return new ReportStream<>(file, BatchReport.Coverage.PARSER); + return file; } return null; } - public FileStream readSourceLines(int fileRef) { + public File readFileSource(int fileRef) { File file = fileStructure.fileFor(FileStructure.Domain.SOURCE, fileRef); if (!doesFileExists(file)) { throw new IllegalStateException("Unable to find source for file #" + fileRef + ". File does not exist: " + file); } - return new FileStream(file); + return file; } private boolean doesFileExists(File file) { diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java index 51d17f9afa1..97b7e5f2caa 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java @@ -34,7 +34,7 @@ public class BatchReportWriter { this.fileStructure = new FileStructure(dir); } - FileStructure getFileStructure() { + public FileStructure getFileStructure() { return fileStructure; } |