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.
185 lines
4.2 KiB
185 lines
4.2 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.BufferedWriter;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.FileWriter;
|
||
|
import java.io.IOException;
|
||
|
import java.io.OutputStreamWriter;
|
||
|
import java.io.PipedReader;
|
||
|
import java.io.PipedWriter;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.io.Writer;
|
||
|
|
||
|
public class WriterLeaks {
|
||
|
|
||
6 years ago
|
// Writer tests
|
||
10 years ago
|
|
||
|
public void writerNotClosedAfterWrite() {
|
||
|
Writer writer;
|
||
|
try {
|
||
|
writer = new PrintWriter("file.txt");
|
||
|
writer.write(10);
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void writerClosed() throws IOException {
|
||
|
Writer writer = null;
|
||
|
try {
|
||
|
writer = new PrintWriter("file.txt");
|
||
|
writer.write(10);
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (writer != null) writer.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// PrintWriter tests
|
||
10 years ago
|
|
||
|
public void printWriterNotClosedAfterAppend() {
|
||
|
PrintWriter writer;
|
||
|
try {
|
||
|
writer = new PrintWriter("file.txt");
|
||
|
writer = writer.append('0');
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void printWriterClosed() throws IOException {
|
||
|
PrintWriter writer = null;
|
||
|
try {
|
||
|
writer = new PrintWriter("file.txt");
|
||
|
writer = writer.append(null);
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (writer != null) writer.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// BufferedWriter tests
|
||
10 years ago
|
|
||
|
public void bufferedWriterNotClosedAfterWrite() {
|
||
|
BufferedWriter writer;
|
||
|
try {
|
||
|
FileWriter fw = new FileWriter("file.txt");
|
||
|
writer = new BufferedWriter(fw);
|
||
|
writer.write("word");
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void bufferedWriterClosed() throws IOException {
|
||
|
BufferedWriter writer = null;
|
||
|
try {
|
||
|
FileWriter fw = new FileWriter("file.txt");
|
||
|
writer = new BufferedWriter(fw);
|
||
|
writer.flush();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (writer != null) writer.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// OutputStreamWriter tests
|
||
10 years ago
|
|
||
|
public void outputStreamWriterNotClosedAfterWrite() {
|
||
|
OutputStreamWriter writer;
|
||
|
try {
|
||
|
writer = new OutputStreamWriter(new FileOutputStream("file.txt"));
|
||
|
writer.write("word");
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void outputStreamWriterClosed() throws IOException {
|
||
|
OutputStreamWriter writer = null;
|
||
|
try {
|
||
|
writer = new OutputStreamWriter(new FileOutputStream("file.txt"));
|
||
|
writer.write(10);
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (writer != null) writer.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// FileWriter tests
|
||
10 years ago
|
|
||
|
public void fileWriterNotClosedAfterWrite() {
|
||
|
FileWriter writer;
|
||
|
try {
|
||
|
writer = new FileWriter("file.txt");
|
||
|
writer.write("word");
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void fileWriterClosed() throws IOException {
|
||
|
FileWriter writer = null;
|
||
|
try {
|
||
|
writer = new FileWriter("file.txt");
|
||
|
writer.write(10);
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (writer != null) writer.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// PipedWriter tests
|
||
10 years ago
|
|
||
|
public void pipedWriterNotClosedAfterConstructedWithReader() {
|
||
|
PipedWriter writer;
|
||
|
PipedReader reader;
|
||
|
try {
|
||
|
reader = new PipedReader();
|
||
|
writer = new PipedWriter(reader);
|
||
|
writer.write(42);
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedWriterNotClosedAfterConnect(PipedReader reader) {
|
||
|
PipedWriter writer;
|
||
|
try {
|
||
|
writer = new PipedWriter();
|
||
|
writer.connect(reader);
|
||
|
writer.write(42);
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedWriterNotConnected() {
|
||
|
PipedWriter writer;
|
||
|
try {
|
||
|
writer = new PipedWriter();
|
||
|
writer.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedWriterClosed(PipedReader reader) throws IOException {
|
||
|
PipedWriter writer = null;
|
||
|
try {
|
||
|
writer = new PipedWriter();
|
||
|
writer.connect(reader);
|
||
|
writer.write(42);
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
6 years ago
|
if (writer != null) writer.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|