Use the try-with-resource construct where possible
This commit is contained in:
@@ -35,6 +35,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -97,9 +98,8 @@ public class HelpController {
|
||||
}
|
||||
|
||||
private static List<String> getLatestLogEntries(File logFile) {
|
||||
try {
|
||||
List<String> lines = new ArrayList<>(LOG_LINES_TO_SHOW);
|
||||
ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile, Charset.defaultCharset());
|
||||
try (ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile, Charset.defaultCharset())) {
|
||||
String current;
|
||||
while ((current = reader.readLine()) != null) {
|
||||
if (lines.size() >= LOG_LINES_TO_SHOW) {
|
||||
@@ -108,7 +108,7 @@ public class HelpController {
|
||||
lines.add(0, current);
|
||||
}
|
||||
return lines;
|
||||
} catch (Exception e) {
|
||||
} catch (IOException e) {
|
||||
LOG.warn("Could not open log file " + logFile, e);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.airsonic.player.io.ShoutCastOutputStream;
|
||||
import org.airsonic.player.security.JWTAuthenticationToken;
|
||||
import org.airsonic.player.service.*;
|
||||
import org.airsonic.player.service.sonos.SonosHelper;
|
||||
import org.airsonic.player.util.FileUtil;
|
||||
import org.airsonic.player.util.HttpRange;
|
||||
import org.airsonic.player.util.StringUtil;
|
||||
import org.airsonic.player.util.Util;
|
||||
@@ -86,7 +85,6 @@ public class StreamController {
|
||||
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
|
||||
TransferStatus status = null;
|
||||
PlayQueueInputStream in = null;
|
||||
Player player = playerService.getPlayer(request, response, false, true);
|
||||
User user = securityService.getUserByName(player.getUsername());
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
@@ -225,10 +223,11 @@ public class StreamController {
|
||||
|
||||
status = statusService.createStreamStatus(player);
|
||||
|
||||
in = new PlayQueueInputStream(player, status, maxBitRate, preferredTargetFormat, videoTranscodingSettings,
|
||||
try (
|
||||
PlayQueueInputStream in = new PlayQueueInputStream(player, status, maxBitRate, preferredTargetFormat, videoTranscodingSettings,
|
||||
transcodingService, audioScrobblerService, mediaFileService, searchService);
|
||||
|
||||
try (OutputStream out = makeOutputStream(request, response, range, isSingleFile, player, settingsService)) {
|
||||
OutputStream out = makeOutputStream(request, response, range, isSingleFile, player, settingsService)
|
||||
) {
|
||||
final int BUFFER_SIZE = 2048;
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
|
||||
@@ -278,9 +277,7 @@ public class StreamController {
|
||||
securityService.updateUserByteCounts(user, status.getBytesTransfered(), 0L, 0L);
|
||||
statusService.removeStreamStatus(status);
|
||||
}
|
||||
FileUtil.closeQuietly(in);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.airsonic.player.service.SettingsService;
|
||||
import org.airsonic.player.service.StatusService;
|
||||
import org.airsonic.player.upload.MonitoredDiskFileItemFactory;
|
||||
import org.airsonic.player.upload.UploadListener;
|
||||
import org.airsonic.player.util.FileUtil;
|
||||
import org.airsonic.player.util.StringUtil;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.FileItemFactory;
|
||||
@@ -184,12 +183,10 @@ public class UploadController {
|
||||
}
|
||||
|
||||
entryFile.getParentFile().mkdirs();
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
inputStream = zipFile.getInputStream(entry);
|
||||
outputStream = new FileOutputStream(entryFile);
|
||||
|
||||
try (
|
||||
OutputStream outputStream = new FileOutputStream(entryFile);
|
||||
InputStream inputStream = zipFile.getInputStream(entry)
|
||||
) {
|
||||
byte[] buf = new byte[8192];
|
||||
while (true) {
|
||||
int n = inputStream.read(buf);
|
||||
@@ -198,19 +195,14 @@ public class UploadController {
|
||||
}
|
||||
outputStream.write(buf, 0, n);
|
||||
}
|
||||
|
||||
LOG.info("Unzipped " + entryFile);
|
||||
unzippedFiles.add(entryFile);
|
||||
} finally {
|
||||
FileUtil.closeQuietly(inputStream);
|
||||
FileUtil.closeQuietly(outputStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
zipFile.close();
|
||||
file.delete();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,18 +50,14 @@ public class InputStreamReaderThread extends Thread {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(input));
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
|
||||
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
||||
if (log) {
|
||||
LOG.info('(' + name + ") " + line);
|
||||
}
|
||||
}
|
||||
} catch (IOException x) {
|
||||
// Intentionally ignored.
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
FileUtil.closeQuietly(reader);
|
||||
FileUtil.closeQuietly(input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,14 +53,10 @@ public class LastFmCache extends Cache {
|
||||
|
||||
@Override
|
||||
public InputStream load(String cacheEntryName) {
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(getXmlFile(cacheEntryName));
|
||||
try (FileInputStream in = new FileInputStream(getXmlFile(cacheEntryName))) {
|
||||
return new ByteArrayInputStream(IOUtils.toByteArray(in));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
} finally {
|
||||
FileUtil.closeQuietly(in);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,14 +29,12 @@ import org.apache.http.conn.ConnectTimeoutException;
|
||||
import org.apache.http.impl.client.BasicResponseHandler;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.*;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Comparator;
|
||||
@@ -188,21 +186,17 @@ public class VersionService {
|
||||
* @param resourceName The resource name.
|
||||
* @return The first line of the resource.
|
||||
*/
|
||||
private String readLineFromResource(String resourceName) {
|
||||
private String readLineFromResource(@NonNull String resourceName) {
|
||||
InputStream in = VersionService.class.getResourceAsStream(resourceName);
|
||||
if (in == null) {
|
||||
return null;
|
||||
}
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
|
||||
reader = new BufferedReader(new InputStreamReader(in));
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
|
||||
return reader.readLine();
|
||||
|
||||
} catch (IOException x) {
|
||||
return null;
|
||||
} finally {
|
||||
FileUtil.closeQuietly(reader);
|
||||
FileUtil.closeQuietly(in);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,10 +279,7 @@ public final class StringUtil {
|
||||
* @throws IOException If an I/O error occurs.
|
||||
*/
|
||||
public static String[] readLines(InputStream in) throws IOException {
|
||||
BufferedReader reader = null;
|
||||
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(in));
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
||||
line = line.trim();
|
||||
@@ -294,7 +291,6 @@ public final class StringUtil {
|
||||
|
||||
} finally {
|
||||
FileUtil.closeQuietly(in);
|
||||
FileUtil.closeQuietly(reader);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user