Use the try-with-resource construct where possible

master
jvoisin 5 years ago committed by GitHub
parent a81dbc7e23
commit a9a7b08230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      airsonic-main/src/main/java/org/airsonic/player/controller/HelpController.java
  2. 11
      airsonic-main/src/main/java/org/airsonic/player/controller/StreamController.java
  3. 16
      airsonic-main/src/main/java/org/airsonic/player/controller/UploadController.java
  4. 8
      airsonic-main/src/main/java/org/airsonic/player/io/InputStreamReaderThread.java
  5. 6
      airsonic-main/src/main/java/org/airsonic/player/service/LastFmCache.java
  6. 14
      airsonic-main/src/main/java/org/airsonic/player/service/VersionService.java
  7. 6
      airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java

@ -35,6 +35,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -97,9 +98,8 @@ public class HelpController {
} }
private static List<String> getLatestLogEntries(File logFile) { private static List<String> getLatestLogEntries(File logFile) {
try {
List<String> lines = new ArrayList<>(LOG_LINES_TO_SHOW); 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; String current;
while ((current = reader.readLine()) != null) { while ((current = reader.readLine()) != null) {
if (lines.size() >= LOG_LINES_TO_SHOW) { if (lines.size() >= LOG_LINES_TO_SHOW) {
@ -108,7 +108,7 @@ public class HelpController {
lines.add(0, current); lines.add(0, current);
} }
return lines; return lines;
} catch (Exception e) { } catch (IOException e) {
LOG.warn("Could not open log file " + logFile, e); LOG.warn("Could not open log file " + logFile, e);
return null; return null;
} }

@ -26,7 +26,6 @@ import org.airsonic.player.io.ShoutCastOutputStream;
import org.airsonic.player.security.JWTAuthenticationToken; import org.airsonic.player.security.JWTAuthenticationToken;
import org.airsonic.player.service.*; import org.airsonic.player.service.*;
import org.airsonic.player.service.sonos.SonosHelper; import org.airsonic.player.service.sonos.SonosHelper;
import org.airsonic.player.util.FileUtil;
import org.airsonic.player.util.HttpRange; import org.airsonic.player.util.HttpRange;
import org.airsonic.player.util.StringUtil; import org.airsonic.player.util.StringUtil;
import org.airsonic.player.util.Util; import org.airsonic.player.util.Util;
@ -86,7 +85,6 @@ public class StreamController {
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
TransferStatus status = null; TransferStatus status = null;
PlayQueueInputStream in = null;
Player player = playerService.getPlayer(request, response, false, true); Player player = playerService.getPlayer(request, response, false, true);
User user = securityService.getUserByName(player.getUsername()); User user = securityService.getUserByName(player.getUsername());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
@ -225,10 +223,11 @@ public class StreamController {
status = statusService.createStreamStatus(player); 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); transcodingService, audioScrobblerService, mediaFileService, searchService);
OutputStream out = makeOutputStream(request, response, range, isSingleFile, player, settingsService)
try (OutputStream out = makeOutputStream(request, response, range, isSingleFile, player, settingsService)) { ) {
final int BUFFER_SIZE = 2048; final int BUFFER_SIZE = 2048;
byte[] buf = new byte[BUFFER_SIZE]; byte[] buf = new byte[BUFFER_SIZE];
@ -278,9 +277,7 @@ public class StreamController {
securityService.updateUserByteCounts(user, status.getBytesTransfered(), 0L, 0L); securityService.updateUserByteCounts(user, status.getBytesTransfered(), 0L, 0L);
statusService.removeStreamStatus(status); 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.service.StatusService;
import org.airsonic.player.upload.MonitoredDiskFileItemFactory; import org.airsonic.player.upload.MonitoredDiskFileItemFactory;
import org.airsonic.player.upload.UploadListener; import org.airsonic.player.upload.UploadListener;
import org.airsonic.player.util.FileUtil;
import org.airsonic.player.util.StringUtil; import org.airsonic.player.util.StringUtil;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileItemFactory;
@ -184,12 +183,10 @@ public class UploadController {
} }
entryFile.getParentFile().mkdirs(); entryFile.getParentFile().mkdirs();
InputStream inputStream = null; try (
OutputStream outputStream = null; OutputStream outputStream = new FileOutputStream(entryFile);
try { InputStream inputStream = zipFile.getInputStream(entry)
inputStream = zipFile.getInputStream(entry); ) {
outputStream = new FileOutputStream(entryFile);
byte[] buf = new byte[8192]; byte[] buf = new byte[8192];
while (true) { while (true) {
int n = inputStream.read(buf); int n = inputStream.read(buf);
@ -198,19 +195,14 @@ public class UploadController {
} }
outputStream.write(buf, 0, n); outputStream.write(buf, 0, n);
} }
LOG.info("Unzipped " + entryFile); LOG.info("Unzipped " + entryFile);
unzippedFiles.add(entryFile); unzippedFiles.add(entryFile);
} finally {
FileUtil.closeQuietly(inputStream);
FileUtil.closeQuietly(outputStream);
} }
} }
} }
zipFile.close(); zipFile.close();
file.delete(); file.delete();
} }
} }

@ -50,18 +50,14 @@ public class InputStreamReaderThread extends Thread {
} }
public void run() { public void run() {
BufferedReader reader = null; try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
try {
reader = new BufferedReader(new InputStreamReader(input));
for (String line = reader.readLine(); line != null; line = reader.readLine()) { for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (log) { if (log) {
LOG.info('(' + name + ") " + line); LOG.info('(' + name + ") " + line);
} }
} }
} catch (IOException x) { } catch (IOException ignored) {
// Intentionally ignored.
} finally { } finally {
FileUtil.closeQuietly(reader);
FileUtil.closeQuietly(input); FileUtil.closeQuietly(input);
} }
} }

@ -53,14 +53,10 @@ public class LastFmCache extends Cache {
@Override @Override
public InputStream load(String cacheEntryName) { public InputStream load(String cacheEntryName) {
FileInputStream in = null; try (FileInputStream in = new FileInputStream(getXmlFile(cacheEntryName))) {
try {
in = new FileInputStream(getXmlFile(cacheEntryName));
return new ByteArrayInputStream(IOUtils.toByteArray(in)); return new ByteArrayInputStream(IOUtils.toByteArray(in));
} catch (Exception e) { } catch (Exception e) {
return null; 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.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Comparator; import java.util.Comparator;
@ -188,21 +186,17 @@ public class VersionService {
* @param resourceName The resource name. * @param resourceName The resource name.
* @return The first line of the resource. * @return The first line of the resource.
*/ */
private String readLineFromResource(String resourceName) { private String readLineFromResource(@NonNull String resourceName) {
InputStream in = VersionService.class.getResourceAsStream(resourceName); InputStream in = VersionService.class.getResourceAsStream(resourceName);
if (in == null) { if (in == null) {
return null; return null;
} }
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in)); try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
return reader.readLine(); return reader.readLine();
} catch (IOException x) { } catch (IOException x) {
return null; return null;
} finally { } finally {
FileUtil.closeQuietly(reader);
FileUtil.closeQuietly(in); FileUtil.closeQuietly(in);
} }
} }

@ -279,10 +279,7 @@ public final class StringUtil {
* @throws IOException If an I/O error occurs. * @throws IOException If an I/O error occurs.
*/ */
public static String[] readLines(InputStream in) throws IOException { public static String[] readLines(InputStream in) throws IOException {
BufferedReader reader = null; try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
try {
reader = new BufferedReader(new InputStreamReader(in));
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
for (String line = reader.readLine(); line != null; line = reader.readLine()) { for (String line = reader.readLine(); line != null; line = reader.readLine()) {
line = line.trim(); line = line.trim();
@ -294,7 +291,6 @@ public final class StringUtil {
} finally { } finally {
FileUtil.closeQuietly(in); FileUtil.closeQuietly(in);
FileUtil.closeQuietly(reader);
} }
} }

Loading…
Cancel
Save