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.
951 lines
24 KiB
951 lines
24 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;
|
||
|
|
||
6 years ago
|
import android.app.Activity;
|
||
|
import android.content.Context;
|
||
|
import android.content.res.Resources;
|
||
|
import android.content.res.TypedArray;
|
||
10 years ago
|
import com.fasterxml.jackson.core.JsonFactory;
|
||
|
import com.fasterxml.jackson.core.JsonParser;
|
||
|
import com.fasterxml.jackson.core.json.UTF8StreamJsonParser;
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.io.Closeables;
|
||
|
import java.io.BufferedInputStream;
|
||
|
import java.io.Closeable;
|
||
|
import java.io.DataOutputStream;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
10 years ago
|
import java.io.InputStreamReader;
|
||
10 years ago
|
import java.io.ObjectInputStream;
|
||
|
import java.io.ObjectOutputStream;
|
||
|
import java.io.OutputStream;
|
||
|
import java.io.PipedInputStream;
|
||
|
import java.io.PipedOutputStream;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.io.RandomAccessFile;
|
||
|
import java.net.HttpURLConnection;
|
||
|
import java.net.ServerSocket;
|
||
|
import java.net.Socket;
|
||
|
import java.net.URL;
|
||
|
import java.net.URLConnection;
|
||
|
import java.nio.channels.FileChannel;
|
||
|
import java.util.Scanner;
|
||
|
import java.util.jar.JarFile;
|
||
|
import java.util.jar.JarInputStream;
|
||
|
import java.util.jar.JarOutputStream;
|
||
9 years ago
|
import java.util.zip.Deflater;
|
||
10 years ago
|
import java.util.zip.GZIPInputStream;
|
||
|
import java.util.zip.GZIPOutputStream;
|
||
9 years ago
|
import java.util.zip.Inflater;
|
||
10 years ago
|
import java.util.zip.ZipFile;
|
||
|
import javax.net.ssl.HttpsURLConnection;
|
||
|
|
||
|
public class ResourceLeaks {
|
||
|
|
||
6 years ago
|
// FileOutputStream tests
|
||
10 years ago
|
|
||
|
public void fileOutputStreamNotClosed() throws IOException {
|
||
|
FileOutputStream fis = new FileOutputStream("file.txt");
|
||
|
}
|
||
|
|
||
|
public void fileOutputStreamNotClosedAfterWrite() {
|
||
|
byte[] arr = {1, 2, 3};
|
||
|
FileOutputStream fis = null;
|
||
|
try {
|
||
|
fis = new FileOutputStream("file.txt");
|
||
|
fis.write(arr);
|
||
|
fis.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void fileOutputStreamClosed() throws IOException {
|
||
|
FileOutputStream fis = new FileOutputStream("file.txt");
|
||
|
fis.close();
|
||
|
}
|
||
|
|
||
9 years ago
|
public void fileOutputStreamOneLeak() throws IOException {
|
||
|
FileOutputStream fis = new FileOutputStream("file.txt");
|
||
|
if (fis != null) {
|
||
|
} else {
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
public int fileOutputStreamTwoLeaks1(boolean ok) throws IOException {
|
||
10 years ago
|
FileOutputStream fis = new FileOutputStream("file.txt");
|
||
|
if (ok) {
|
||
|
fis.write(1);
|
||
|
return 1;
|
||
|
} else {
|
||
|
fis.write(2);
|
||
|
return 2;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public void fileOutputStreamTwoLeaks2() throws IOException {
|
||
|
FileOutputStream fis = new FileOutputStream("file.txt");
|
||
|
if (fis != null) {
|
||
|
} else {
|
||
|
}
|
||
|
fis = new FileOutputStream("x");
|
||
|
}
|
||
|
|
||
6 years ago
|
// TwoResources tests
|
||
10 years ago
|
|
||
|
public static void twoResources() throws IOException {
|
||
|
FileInputStream fis = null;
|
||
|
FileOutputStream fos = null;
|
||
|
try {
|
||
|
fis = new FileInputStream(new File("infile.txt"));
|
||
|
fos = new FileOutputStream(new File("outfile.txt"));
|
||
|
fos.write(fis.read());
|
||
|
} finally {
|
||
|
if (fis != null) fis.close();
|
||
|
if (fos != null) fos.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void twoResourcesHeliosFix() throws IOException {
|
||
10 years ago
|
FileInputStream fis = null;
|
||
|
FileOutputStream fos = null;
|
||
10 years ago
|
try {
|
||
10 years ago
|
fis = new FileInputStream(new File("whatever.txt"));
|
||
10 years ago
|
try {
|
||
10 years ago
|
fos = new FileOutputStream(new File("everwhat.txt"));
|
||
10 years ago
|
fos.write(fis.read());
|
||
|
} finally {
|
||
10 years ago
|
if (fos != null) fos.close();
|
||
10 years ago
|
}
|
||
|
} finally {
|
||
10 years ago
|
if (fis != null) fis.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public static void twoResourcesCommonFix() throws IOException {
|
||
|
FileInputStream fis = null;
|
||
|
FileOutputStream fos = null;
|
||
|
try {
|
||
|
fis = new FileInputStream(new File("infile.txt"));
|
||
|
fos = new FileOutputStream(new File("outfile.txt"));
|
||
|
fos.write(fis.read());
|
||
|
} finally {
|
||
|
try {
|
||
|
if (fis != null) fis.close();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
try {
|
||
|
if (fos != null) fos.close();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void twoResourcesServerSocket() throws IOException {
|
||
|
ServerSocket a = null;
|
||
|
ServerSocket b = null;
|
||
|
try {
|
||
|
a = new ServerSocket();
|
||
|
b = new ServerSocket();
|
||
|
} finally {
|
||
|
if (a != null) a.close();
|
||
|
if (b != null) b.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void twoResourcesRandomAccessFile() throws IOException {
|
||
|
RandomAccessFile a = null;
|
||
|
RandomAccessFile b = null;
|
||
|
try {
|
||
|
a = new RandomAccessFile("", "rw");
|
||
|
b = new RandomAccessFile("", "rw");
|
||
|
} finally {
|
||
|
if (a != null) a.close();
|
||
|
if (b != null) b.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void twoResourcesRandomAccessFileCommonFix() throws IOException {
|
||
|
RandomAccessFile a = null;
|
||
|
RandomAccessFile b = null;
|
||
|
try {
|
||
|
a = new RandomAccessFile("", "rw");
|
||
|
b = new RandomAccessFile("", "rw");
|
||
|
} finally {
|
||
|
try {
|
||
|
if (a != null) a.close();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
try {
|
||
|
if (b != null) b.close();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// NestedResource tests
|
||
10 years ago
|
|
||
|
// BufferedInputStream does not throw exception, and its close
|
||
|
// closes the FileInputStream as well
|
||
|
public void nestedGood() throws IOException {
|
||
|
BufferedInputStream b = new BufferedInputStream(new FileInputStream("file.txt"));
|
||
|
b.close();
|
||
|
}
|
||
|
|
||
|
// GZipInputStream can throw IO Exception
|
||
|
// in which case the new FileInputStream will be dangling
|
||
|
public void nestedBad1() throws IOException {
|
||
|
GZIPInputStream g = new GZIPInputStream(new FileInputStream("file.txt"));
|
||
|
g.close();
|
||
|
}
|
||
|
|
||
|
public void nestedBad2() throws IOException {
|
||
|
GZIPOutputStream g = new GZIPOutputStream(new FileOutputStream("file.txt"));
|
||
|
g.close();
|
||
|
}
|
||
|
|
||
|
/* Fixed versions of this are below with ObjectInputStream tests */
|
||
|
public void objectInputStreamClosedNestedBad() throws IOException {
|
||
|
ObjectInputStream oin = null;
|
||
|
try {
|
||
|
oin = new ObjectInputStream(new FileInputStream("file.txt"));
|
||
|
int a = oin.available();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (oin != null) oin.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Fixed versions of this are below with ObjectInputStream tests */
|
||
|
public void objectOutputStreamClosedNestedBad() throws IOException {
|
||
|
ObjectOutputStream oin = null;
|
||
|
try {
|
||
|
oin = new ObjectOutputStream(new FileOutputStream("file.txt"));
|
||
|
oin.write(3);
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (oin != null) oin.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// ZipFile tests (Jarfile Tests also test Zipfiles)
|
||
10 years ago
|
|
||
|
public static void zipFileLeakExceptionalBranch() throws IOException {
|
||
|
ZipFile j = null;
|
||
|
try {
|
||
|
j = new ZipFile("");
|
||
|
} catch (IOException e) {
|
||
|
FileOutputStream fis = new FileOutputStream("file.txt");
|
||
6 years ago
|
// The purpose of this is to cause a leak, from when ZipFile constructor throws
|
||
10 years ago
|
} finally {
|
||
|
if (j != null) j.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void zipFileNoLeak() throws IOException {
|
||
|
ZipFile j = null;
|
||
|
try {
|
||
|
j = new ZipFile("");
|
||
|
} finally {
|
||
|
if (j != null) j.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// JarFile tests
|
||
10 years ago
|
|
||
|
public boolean jarFileClosed() {
|
||
|
JarFile jarFile = null;
|
||
|
try {
|
||
|
jarFile = new JarFile("");
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
try {
|
||
|
if (jarFile != null) {
|
||
|
jarFile.close();
|
||
|
}
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public boolean jarFileNotClosed() {
|
||
|
JarFile jarFile = null;
|
||
|
try {
|
||
|
jarFile = new JarFile("");
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
6 years ago
|
// FileInputStream tests
|
||
10 years ago
|
|
||
|
public void fileInputStreamNotClosedAfterRead() {
|
||
|
FileInputStream fis;
|
||
|
try {
|
||
|
fis = new FileInputStream("file.txt");
|
||
|
fis.read();
|
||
|
fis.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void fileInputStreamClosed() throws IOException {
|
||
|
FileInputStream fis = null;
|
||
|
try {
|
||
|
fis = new FileInputStream("file.txt");
|
||
|
fis.available();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (fis != null) fis.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// PipedInputStream tests
|
||
10 years ago
|
|
||
|
public void pipedInputStreamNotClosedAfterRead(PipedOutputStream pout) {
|
||
|
PipedInputStream pin;
|
||
|
try {
|
||
|
pin = new PipedInputStream(pout);
|
||
|
int data = pin.read();
|
||
|
pin.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedInputStreamClosed(PipedOutputStream pout) throws IOException {
|
||
|
PipedInputStream pin = null;
|
||
|
try {
|
||
|
pin = new PipedInputStream(pout);
|
||
|
int data = pin.read();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
pin.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// PipedOutputStream tests
|
||
10 years ago
|
|
||
|
public void pipedOutputStreamNotClosedAfterWrite() {
|
||
|
byte[] arr = {1, 2, 3, 4, 5};
|
||
|
PipedOutputStream pout;
|
||
|
try {
|
||
|
pout = new PipedOutputStream();
|
||
|
pout.write(arr);
|
||
|
pout.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void pipedOutputStreamClosed(PipedInputStream pin) throws IOException {
|
||
|
PipedOutputStream pout = null;
|
||
|
try {
|
||
|
pout = new PipedOutputStream(pin);
|
||
|
pout.flush();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
pout.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// ObjectOutputStream tests
|
||
10 years ago
|
|
||
|
public void objectOutputStreamNotClosedAfterWrite() {
|
||
|
byte[] arr = {1, 2, 3, 4, 5};
|
||
|
ObjectOutputStream oout;
|
||
|
try {
|
||
|
oout = new ObjectOutputStream(new FileOutputStream("file.txt"));
|
||
|
oout.write(arr);
|
||
|
oout.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void objectOutputStreamClosed() throws IOException {
|
||
|
ObjectOutputStream oout = null;
|
||
|
FileOutputStream fis = new FileOutputStream("file.txt");
|
||
|
try {
|
||
|
oout = new ObjectOutputStream(fis);
|
||
|
oout.flush();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
fis.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// ObjectInputStream tests
|
||
10 years ago
|
|
||
|
public void objectInputStreamNotClosedAfterRead() {
|
||
|
ObjectInputStream oin;
|
||
|
try {
|
||
|
oin = new ObjectInputStream(new FileInputStream("file.txt"));
|
||
|
oin.read();
|
||
|
oin.close();
|
||
|
} catch (IOException e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void objectInputStreamClosed() throws IOException {
|
||
|
ObjectInputStream oin = null;
|
||
|
FileInputStream fis = new FileInputStream("file.txt");
|
||
|
try {
|
||
|
oin = new ObjectInputStream(fis);
|
||
|
int a = oin.available();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
if (oin != null) {
|
||
|
oin.close();
|
||
|
} else {
|
||
|
fis.close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void objectInputStreamClosed2() throws IOException {
|
||
|
ObjectInputStream oin = null;
|
||
|
FileInputStream fis = new FileInputStream("file.txt");
|
||
|
try {
|
||
|
oin = new ObjectInputStream(fis);
|
||
|
int a = oin.available();
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
fis.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// JarInputStream tests
|
||
10 years ago
|
|
||
|
public static void jarInputStreamNoLeak() throws IOException {
|
||
|
FileInputStream fos = new FileInputStream("");
|
||
|
try {
|
||
|
JarInputStream g = new JarInputStream(fos);
|
||
|
g.close();
|
||
|
} catch (IOException e) {
|
||
|
fos.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void jarInputStreamLeak() throws IOException {
|
||
|
FileInputStream fos = new FileInputStream("");
|
||
|
try {
|
||
|
JarInputStream g = new JarInputStream(fos); // Testing exceptional condition in constructor
|
||
|
g.close();
|
||
|
} catch (IOException e) {
|
||
|
// fos.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void nestedBadJarInputStream(File file) throws IOException {
|
||
|
JarInputStream g = new JarInputStream(new FileInputStream(file));
|
||
|
g.close();
|
||
|
}
|
||
|
|
||
6 years ago
|
// JarOutputStream tests
|
||
10 years ago
|
|
||
|
public static void jarOutputStreamNoLeak() throws IOException {
|
||
|
FileOutputStream fos = new FileOutputStream("");
|
||
|
try {
|
||
|
JarOutputStream g = new JarOutputStream(fos);
|
||
|
g.close();
|
||
|
} catch (IOException e) {
|
||
|
fos.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void jarOutputStreamLeak() throws IOException {
|
||
|
FileOutputStream fos = new FileOutputStream("");
|
||
|
try {
|
||
|
JarOutputStream g = new JarOutputStream(fos); // Testing exceptional condition in constructor
|
||
|
g.close();
|
||
|
} catch (IOException e) {
|
||
|
// fos.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void nestedBadJarOutputStream() throws IOException {
|
||
|
JarOutputStream g = new JarOutputStream(new FileOutputStream("file.txt"));
|
||
|
g.close();
|
||
|
}
|
||
|
|
||
6 years ago
|
// Socket tests
|
||
10 years ago
|
|
||
|
public void socketNotClosed() {
|
||
|
Socket socket = new Socket();
|
||
|
}
|
||
|
|
||
|
public void socketClosed() throws IOException {
|
||
|
Socket socket = new Socket();
|
||
|
socket.close();
|
||
|
}
|
||
|
|
||
6 years ago
|
// Socket InputStream tests
|
||
10 years ago
|
|
||
|
public int socketInputStreamNotClosed(Socket socket) throws IOException {
|
||
|
InputStream stream = socket.getInputStream();
|
||
|
return stream.read();
|
||
|
}
|
||
|
|
||
|
public void socketInputStreamClosed() throws IOException {
|
||
|
Socket socket = new Socket();
|
||
|
InputStream stream = socket.getInputStream();
|
||
|
try {
|
||
|
stream.close();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
socket.close();
|
||
|
}
|
||
|
|
||
6 years ago
|
// Socket OutputStream tests
|
||
10 years ago
|
|
||
|
public void socketOutputStreamNotClosed(Socket socket) throws IOException {
|
||
|
OutputStream stream = socket.getOutputStream();
|
||
|
stream.write(10);
|
||
|
}
|
||
|
|
||
|
public void socketOutputStreamClosed() throws IOException {
|
||
|
Socket socket = new Socket();
|
||
|
OutputStream stream = socket.getOutputStream();
|
||
|
try {
|
||
|
stream.close();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
socket.close();
|
||
|
}
|
||
|
|
||
6 years ago
|
// ServerSocket tests
|
||
10 years ago
|
|
||
|
public void serverSocketNotClosed() throws IOException {
|
||
|
ServerSocket listener = new ServerSocket(9090);
|
||
|
while (true) {
|
||
|
Socket socket = listener.accept();
|
||
|
try {
|
||
6 years ago
|
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
|
||
10 years ago
|
out.println("");
|
||
|
} finally {
|
||
|
socket.close();
|
||
|
}
|
||
|
listener.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void serverSocketClosed() throws IOException {
|
||
|
ServerSocket socket = new ServerSocket();
|
||
|
socket.close();
|
||
|
}
|
||
|
|
||
|
public void serverSocketWithSocketClosed() throws IOException {
|
||
|
ServerSocket listener = new ServerSocket(9090);
|
||
|
try {
|
||
|
while (true) {
|
||
|
Socket socket = listener.accept();
|
||
|
try {
|
||
6 years ago
|
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
|
||
10 years ago
|
out.println("");
|
||
|
} finally {
|
||
|
socket.close();
|
||
|
}
|
||
|
}
|
||
|
} finally {
|
||
|
listener.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// HttpURLConnection
|
||
10 years ago
|
|
||
|
public void openHttpURLConnectionDisconnected() throws IOException {
|
||
|
String content = "TEXT";
|
||
|
DataOutputStream outputStream = null;
|
||
|
HttpURLConnection connection = null;
|
||
|
URL address = new URL("http://www.facebook.com");
|
||
|
connection = (HttpURLConnection) address.openConnection();
|
||
|
try {
|
||
|
outputStream = new DataOutputStream(connection.getOutputStream());
|
||
|
outputStream.writeBytes(content);
|
||
|
outputStream.flush();
|
||
|
|
||
|
} finally {
|
||
|
connection.disconnect();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void openHttpURLConnectionNotDisconnected() throws IOException {
|
||
|
String content = "TEXT";
|
||
|
DataOutputStream outputStream = null;
|
||
|
HttpURLConnection connection = null;
|
||
|
URL address = new URL("http://www.facebook.com");
|
||
|
connection = (HttpURLConnection) address.openConnection();
|
||
|
|
||
|
outputStream = new DataOutputStream(connection.getOutputStream());
|
||
|
outputStream.writeBytes(content);
|
||
|
}
|
||
|
|
||
|
public void openHttpsURLConnectionNotDisconnected() throws IOException {
|
||
|
HttpsURLConnection connection = null;
|
||
|
URL address = new URL("https://www.facebook.com");
|
||
|
connection = (HttpsURLConnection) address.openConnection();
|
||
|
}
|
||
|
|
||
|
public void openHttpsURLConnectionDisconnected() throws IOException {
|
||
|
HttpsURLConnection connection = null;
|
||
|
URL address = new URL("https://www.facebook.com");
|
||
|
connection = (HttpsURLConnection) address.openConnection();
|
||
|
connection.disconnect();
|
||
|
}
|
||
|
|
||
|
public void closedWithCloseables() throws IOException {
|
||
|
FileInputStream fs = new FileInputStream("file.txt");
|
||
|
try {
|
||
|
fs.read();
|
||
|
} finally {
|
||
|
Closeables.close(fs, false);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void closedQuietlyWithCloseables() throws IOException {
|
||
|
FileInputStream fs = new FileInputStream("file.txt");
|
||
|
try {
|
||
|
fs.read();
|
||
|
} finally {
|
||
|
Closeables.closeQuietly(fs);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void closeNullWithCloseables() throws IOException {
|
||
|
FileInputStream fs = null;
|
||
|
try {
|
||
|
fs = new FileInputStream("file.txt");
|
||
|
} finally {
|
||
|
Closeables.close(fs, true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void closeNullQuietlyWithCloseables() throws IOException {
|
||
|
FileInputStream fs = null;
|
||
|
try {
|
||
|
fs = new FileInputStream("file.txt");
|
||
|
} finally {
|
||
|
Closeables.closeQuietly(fs);
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
private static void myClose(Closeable closeable, boolean swallowIOException) throws IOException {
|
||
|
if (closeable == null) {
|
||
|
return;
|
||
|
}
|
||
|
try {
|
||
|
closeable.close();
|
||
|
} catch (IOException e) {
|
||
|
if (!swallowIOException) {
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void closeWithCloseablesNestedAlloc() throws IOException {
|
||
|
BufferedInputStream b = null;
|
||
|
try {
|
||
6 years ago
|
b = new BufferedInputStream(new FileInputStream("file.txt"));
|
||
9 years ago
|
} finally {
|
||
|
myClose(b, false);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
// JsonParser tests
|
||
|
|
||
|
public void parseFromStringAndNotClose(JsonFactory factory) throws IOException {
|
||
|
UTF8StreamJsonParser parser = null;
|
||
|
try {
|
||
8 years ago
|
parser = (UTF8StreamJsonParser) factory.createParser(new File("[]"));
|
||
10 years ago
|
Object o = parser.readValueAs(Object.class);
|
||
|
ignore(o);
|
||
|
} catch (Exception e) {
|
||
|
} finally {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void parseFromInputStreamAndClose(JsonFactory factory) throws IOException {
|
||
|
JsonParser parser = null;
|
||
|
FileInputStream in = null;
|
||
|
try {
|
||
|
in = new FileInputStream("");
|
||
|
parser = factory.createParser(in);
|
||
|
Object o = parser.readValueAs(Object.class);
|
||
|
ignore(o);
|
||
|
} catch (Exception e) {
|
||
|
} finally {
|
||
|
if (in != null) in.close();
|
||
|
}
|
||
|
// parser does not own a resources which is closed externally
|
||
|
}
|
||
|
|
||
|
public void parseFromInputStreamAndLeak(JsonFactory factory) throws IOException {
|
||
|
JsonParser parser = null;
|
||
|
FileInputStream in = null;
|
||
|
try {
|
||
|
in = new FileInputStream("");
|
||
|
parser = factory.createParser(in);
|
||
|
Object o = parser.readValueAs(Object.class);
|
||
|
ignore(o);
|
||
|
} catch (Exception e) {
|
||
|
} finally {
|
||
|
if (parser != null) parser.close();
|
||
|
}
|
||
|
// parser does not own a resource which is leaked
|
||
|
}
|
||
|
|
||
6 years ago
|
private void ignore(Object o) {}
|
||
10 years ago
|
|
||
|
// Installation.java examples. Even the fix was a fp for a while
|
||
|
// for several reasons, so this test is just to make sure it remains
|
||
|
// banished forever
|
||
|
|
||
6 years ago
|
private String readInstallationFileGood(File installation) throws IOException {
|
||
10 years ago
|
RandomAccessFile f = new RandomAccessFile(installation, "r");
|
||
|
try {
|
||
|
byte[] bytes = new byte[(int) f.length()];
|
||
|
f.readFully(bytes);
|
||
|
return new String(bytes);
|
||
|
} finally {
|
||
|
f.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
private String readInstallationFileBad(File installation) throws IOException {
|
||
10 years ago
|
RandomAccessFile f = new RandomAccessFile(installation, "r");
|
||
|
byte[] bytes = new byte[(int) f.length()];
|
||
|
f.readFully(bytes);
|
||
|
f.close();
|
||
|
return new String(bytes);
|
||
|
}
|
||
|
|
||
|
private int readConfigCloseStream(String mTurnConfigUrl) {
|
||
|
try {
|
||
|
URL url = new URL(mTurnConfigUrl);
|
||
|
URLConnection connection = url.openConnection();
|
||
|
InputStream stream = connection.getInputStream();
|
||
|
try {
|
||
|
return stream.read();
|
||
|
} finally {
|
||
|
stream.close();
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
private int readConfigNotCloseStream(String mTurnConfigUrl) {
|
||
|
try {
|
||
|
URL url = new URL(mTurnConfigUrl);
|
||
|
URLConnection connection = url.openConnection();
|
||
|
InputStream stream = connection.getInputStream();
|
||
|
return stream.read();
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
private void readConfigNotClosedOK(String mTurnConfigUrl) {
|
||
|
try {
|
||
|
URL url = new URL(mTurnConfigUrl);
|
||
|
URLConnection connection = url.openConnection();
|
||
|
ignore(connection);
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TypedArray
|
||
|
|
||
|
public void themeObtainTypedArrayAndRecycle(Resources.Theme theme) {
|
||
6 years ago
|
TypedArray array = theme.obtainStyledAttributes(new int[] {});
|
||
10 years ago
|
ignore(array);
|
||
|
array.recycle();
|
||
|
}
|
||
|
|
||
|
public void themeObtainTypedArrayAndLeak(Resources.Theme theme) {
|
||
6 years ago
|
TypedArray array = theme.obtainStyledAttributes(new int[] {});
|
||
10 years ago
|
ignore(array);
|
||
|
}
|
||
|
|
||
|
public void activityObtainTypedArrayAndRecycle(Activity activity) {
|
||
6 years ago
|
TypedArray array = activity.obtainStyledAttributes(new int[] {});
|
||
10 years ago
|
ignore(array);
|
||
|
array.recycle();
|
||
|
}
|
||
|
|
||
|
public void activityObtainTypedArrayAndLeak(Activity activity) {
|
||
6 years ago
|
TypedArray array = activity.obtainStyledAttributes(new int[] {});
|
||
10 years ago
|
ignore(array);
|
||
|
}
|
||
|
|
||
|
public void contextObtainTypedArrayAndRecycle(Context context) {
|
||
6 years ago
|
TypedArray array = context.obtainStyledAttributes(new int[] {});
|
||
10 years ago
|
ignore(array);
|
||
|
array.recycle();
|
||
|
}
|
||
|
|
||
|
public void contextObtainTypedArrayAndLeak(Context context) {
|
||
6 years ago
|
TypedArray array = context.obtainStyledAttributes(new int[] {});
|
||
10 years ago
|
ignore(array);
|
||
|
}
|
||
|
|
||
|
// FileChannel
|
||
|
|
||
|
void copyFileLeak(File src, File dst) throws IOException {
|
||
|
FileChannel inChannel = new FileInputStream(src).getChannel();
|
||
|
FileChannel outChannel = new FileOutputStream(dst).getChannel();
|
||
|
try {
|
||
|
inChannel.transferTo(0, inChannel.size(), outChannel);
|
||
|
} finally {
|
||
6 years ago
|
if (inChannel != null) inChannel.close();
|
||
|
if (outChannel != null) outChannel.close();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void copyFileClose(File src, File dst) throws IOException {
|
||
|
FileChannel inChannel = new FileInputStream(src).getChannel();
|
||
|
try {
|
||
|
ignore(inChannel);
|
||
|
} finally {
|
||
|
inChannel.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected long checkNotNullCauseNoLeak(URL mUrl) throws IOException {
|
||
|
URL url = new URL("http://www.facebook.com");
|
||
|
HttpURLConnection serverConnection =
|
||
|
(HttpURLConnection) Preconditions.checkNotNull(url.openConnection());
|
||
|
try {
|
||
|
ignore(serverConnection);
|
||
|
} catch (NumberFormatException nfe) {
|
||
|
} finally {
|
||
|
serverConnection.disconnect();
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void scannerNotClosed() throws IOException {
|
||
|
Scanner scanner = new Scanner(new FileInputStream("file.txt"));
|
||
|
}
|
||
|
|
||
|
void scannerClosed() throws IOException {
|
||
|
Scanner scanner = new Scanner(new FileInputStream("file.txt"));
|
||
|
scanner.close();
|
||
|
}
|
||
|
|
||
|
void processDestroyed() {
|
||
|
Process process = null;
|
||
|
try {
|
||
|
process = Runtime.getRuntime().exec("");
|
||
|
} catch (IOException e) {
|
||
|
} finally {
|
||
|
process.destroy();
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
void processForciblyDestroyed() throws IOException {
|
||
|
Process process = null;
|
||
|
try {
|
||
|
process = Runtime.getRuntime().exec("");
|
||
|
} finally {
|
||
|
ignore(process.destroyForcibly());
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
class Container {
|
||
|
FileInputStream inputStream;
|
||
|
}
|
||
|
|
||
|
native Container load(FileInputStream inputStream);
|
||
|
|
||
|
public Container resourceReturnedIndirectly() {
|
||
|
FileInputStream inputStream;
|
||
|
Container container = null;
|
||
|
try {
|
||
|
inputStream = new FileInputStream("pif.txt");
|
||
|
container = load(inputStream);
|
||
|
} catch (FileNotFoundException e) {
|
||
|
return null;
|
||
|
}
|
||
|
return container;
|
||
|
}
|
||
|
|
||
|
native void unknownClose(Closeable c);
|
||
|
|
||
|
public void resourceClosedBySkippedMethod() {
|
||
|
FileInputStream inputStream = null;
|
||
|
try {
|
||
|
inputStream = new FileInputStream("pif.txt");
|
||
|
} catch (FileNotFoundException e) {
|
||
|
return;
|
||
|
} finally {
|
||
|
unknownClose(inputStream);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public int tryWithResource() {
|
||
|
try (FileInputStream inputStream = new FileInputStream("paf.txt")) {
|
||
|
return inputStream.read();
|
||
|
} catch (IOException e) {
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
public InputStreamReader withCharset(URLConnection urlConnection) {
|
||
|
InputStreamReader reader = null;
|
||
|
try {
|
||
6 years ago
|
reader = new InputStreamReader(urlConnection.getInputStream(), "iso-8859-1");
|
||
10 years ago
|
} catch (Exception e) {
|
||
|
return null;
|
||
|
} finally {
|
||
|
if (reader != null) {
|
||
|
try {
|
||
|
reader.close();
|
||
|
} catch (IOException e) {
|
||
|
// do nothing
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return reader;
|
||
|
}
|
||
|
|
||
9 years ago
|
public void withZipFile() throws IOException {
|
||
|
ZipFile f = new ZipFile("hi");
|
||
|
InputStream s = f.getInputStream(f.getEntry("there"));
|
||
|
if (s != null) s.toString();
|
||
|
f.close();
|
||
|
}
|
||
|
|
||
9 years ago
|
public void deflaterLeak() {
|
||
|
Deflater comp = new Deflater();
|
||
|
}
|
||
|
|
||
|
public void deflaternoLeak() {
|
||
|
Deflater comp = new Deflater();
|
||
|
comp.end();
|
||
|
}
|
||
|
|
||
|
public void inflaterLeak() {
|
||
|
Inflater decomp = new Inflater();
|
||
|
}
|
||
|
|
||
|
public void inflaterNoLeak() {
|
||
|
Inflater decomp = new Inflater();
|
||
|
decomp.end();
|
||
|
}
|
||
|
|
||
9 years ago
|
void NoResourceLeakWarningAfterCheckState(File f, int x) throws IOException {
|
||
|
InputStream stream = new FileInputStream(f);
|
||
|
Preconditions.checkState(x > 0);
|
||
|
stream.close();
|
||
|
}
|
||
10 years ago
|
}
|