Skip to content

Commit

Permalink
Amends jenkinsci#3211: Restore Source compatibility of hudson.util.Te…
Browse files Browse the repository at this point in the history
…xtFile#lines()
  • Loading branch information
oleg-nenashev committed Mar 10, 2018
1 parent 20bc2e8 commit 9e7e733
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
46 changes: 45 additions & 1 deletion core/src/main/java/hudson/util/TextFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package hudson.util;

import com.google.common.collect.AbstractIterator;
import edu.umd.cs.findbugs.annotations.CreatesObligation;

import hudson.Util;
Expand All @@ -40,6 +41,7 @@
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

/**
* Represents a text file.
Expand Down Expand Up @@ -80,16 +82,58 @@ public String read() throws IOException {
return out.toString();
}

/**
* @throws RuntimeException in the case of {@link IOException} in {@link #readLines()}
* @deprecated This method does not properly propagate errors and may lead to file descriptor leaks
* if the collection is not fully iterated. Use {@link #readLines()} instead.
*/
@Deprecated
public @Nonnull Iterable<String> lines() {
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
final LinesStream stream;
try {
stream = readLines();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
final Iterator<String> it = stream.iterator();

return new Iterator<String>() {
@Override
public boolean hasNext() {
boolean res = it.hasNext();
if (!it.hasNext()) {
try {
stream.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
return res;
}

@Override
public String next() {
return it.next();
}
};
}
};
}

/**
* Creates a new {@link jenkins.util.io.LinesStream} of the file.
* <p>
* Note: The caller is responsible for closing the returned
* <code>LinesStream</code>.
* @throws IOException if the file cannot be converted to a
* {@link java.nio.file.Path} or if the file cannot be opened for reading
* @since TODO
*/
@CreatesObligation
public @Nonnull LinesStream lines() throws IOException {
public @Nonnull LinesStream readLines() throws IOException {
return new LinesStream(Util.fileToPath(file));
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/jenkins/security/s2m/ConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public synchronized void load2() throws IOException {
COL result = create();

if (exists()) {
try (LinesStream stream = lines()) {
try (LinesStream stream = readLines()) {
for (String line : stream) {
if (line.startsWith("#")) continue; // comment
T r = parse(line);
Expand Down

0 comments on commit 9e7e733

Please sign in to comment.