You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
4.8 KiB
206 lines
4.8 KiB
10 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
9 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
9 years ago
|
*/
|
||
10 years ago
|
|
||
|
package codetoanalyze.java.infer;
|
||
|
|
||
|
import java.io.BufferedReader;
|
||
8 years ago
|
import java.io.File;
|
||
6 years ago
|
import java.io.FileInputStream;
|
||
10 years ago
|
import java.io.FileReader;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.io.PipedReader;
|
||
|
import java.io.PipedWriter;
|
||
|
import java.io.PushbackReader;
|
||
|
import java.io.Reader;
|
||
|
|
||
|
public class ReaderLeaks {
|
||
|
|
||
8 years ago
|
private void ignore(Object o) {}
|
||
10 years ago
|
|
||
6 years ago
|
// Reader tests
|
||
10 years ago
|
|
||
|
public void readerNotClosedAfterRead() {
|
||
|
Reader r;
|
||
|
try {
|
||
|
r = new FileReader("testing.txt");
|
||
|
r.read();
|
||
|
r.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void readerClosed() throws IOException {
|
||
|
Reader r = null;
|
||
|
try {
|
||
|
r = new FileReader("testing.txt");
|
||
|
boolean ready = r.ready();
|
||
|
r.close();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (r != null) r.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// BufferedReader tests
|
||
10 years ago
|
|
||
|
public void bufferedReaderNotClosedAfterRead() {
|
||
|
BufferedReader reader;
|
||
|
try {
|
||
|
reader = new BufferedReader(new FileReader("testing.txt"));
|
||
8 years ago
|
ignore(reader.read());
|
||
10 years ago
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void bufferedReaderClosed() throws IOException {
|
||
|
BufferedReader reader = null;
|
||
|
try {
|
||
|
reader = new BufferedReader(new FileReader("testing.txt"));
|
||
8 years ago
|
ignore(reader.read());
|
||
10 years ago
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (reader != null) reader.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
public void noNeedToCloseBufferReaderWrapperOk(File file) throws IOException {
|
||
|
try (InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file))) {
|
||
|
BufferedReader reader = new BufferedReader(inputStreamReader);
|
||
|
ignore(reader.readLine());
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// InputStreamReader tests
|
||
10 years ago
|
|
||
|
public void inputStreamReaderNotClosedAfterRead() {
|
||
|
InputStreamReader reader;
|
||
|
try {
|
||
|
reader = new InputStreamReader(new FileInputStream("testing.txt"));
|
||
|
reader.read();
|
||
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void inputStreamReaderClosed() throws IOException {
|
||
|
InputStreamReader reader = null;
|
||
|
try {
|
||
|
reader = new InputStreamReader(new FileInputStream("testing.txt"));
|
||
8 years ago
|
ignore(reader.read());
|
||
10 years ago
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (reader != null) reader.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// FileReader tests
|
||
10 years ago
|
|
||
|
public void fileReaderNotClosedAfterRead() {
|
||
|
FileReader reader;
|
||
|
try {
|
||
|
reader = new FileReader("testing.txt");
|
||
|
reader.read();
|
||
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void fileReaderClosed() throws IOException {
|
||
|
FileReader reader = null;
|
||
|
try {
|
||
|
reader = new FileReader("testing.txt");
|
||
|
reader.read();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (reader != null) reader.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// PushbackReader tests
|
||
10 years ago
|
|
||
|
public void pushbackReaderNotClosedAfterRead() {
|
||
|
PushbackReader reader;
|
||
|
try {
|
||
|
reader = new PushbackReader(new InputStreamReader(new FileInputStream("testing.txt")));
|
||
|
reader.read();
|
||
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pushbackReaderClosed() throws IOException {
|
||
|
PushbackReader reader = null;
|
||
|
try {
|
||
|
reader = new PushbackReader(new InputStreamReader(new FileInputStream("testing.txt")));
|
||
|
reader.read();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (reader != null) reader.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// PipedReader tests
|
||
10 years ago
|
|
||
|
public void pipedReaderNotClosedAfterConstructedWithWriter() {
|
||
|
PipedReader reader;
|
||
|
PipedWriter writer;
|
||
|
try {
|
||
|
writer = new PipedWriter();
|
||
|
reader = new PipedReader(writer);
|
||
|
reader.read();
|
||
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedReaderNotClosedAfterConnect(PipedWriter writer) {
|
||
|
PipedReader reader;
|
||
|
try {
|
||
|
reader = new PipedReader();
|
||
|
reader.connect(writer);
|
||
|
reader.read();
|
||
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedReaderNotConnected() {
|
||
|
PipedReader reader;
|
||
|
try {
|
||
|
reader = new PipedReader();
|
||
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedReaderClosed(PipedWriter writer) throws IOException {
|
||
|
PipedReader reader = null;
|
||
|
try {
|
||
|
reader = new PipedReader();
|
||
|
reader.connect(writer);
|
||
|
reader.read();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (reader != null) reader.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedReaderFalsePositive() throws IOException {
|
||
|
PipedReader reader;
|
||
|
PipedWriter writer = null;
|
||
|
try {
|
||
|
reader = new PipedReader(writer);
|
||
|
reader.read();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (writer != null) writer.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|