mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
20.26.2016 79bcc8f168174bc1aa57d592d60ad5fb8de797e6
Use try-with-resources
2 files modified
120 ■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/loggers/GZIPAction.java 57 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/loggers/ZIPAction.java 63 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/loggers/GZIPAction.java
@@ -64,11 +64,6 @@
  @Override
  public boolean execute()
  {
    FileInputStream fis = null;
    GZIPOutputStream gzip = null;
    boolean inputStreamOpen = false;
    boolean outputStreamOpen = false;
    try
    {
      if(!originalFile.exists())
@@ -77,25 +72,18 @@
        return false;
      }
      fis = new FileInputStream(originalFile);
      inputStreamOpen = true;
      FileOutputStream fos = new FileOutputStream(newFile);
      gzip = new GZIPOutputStream(fos);
      outputStreamOpen = true;
      byte[] buf = new byte[8192];
      int n;
      while((n = fis.read(buf)) != -1)
      try (FileInputStream fis = new FileInputStream(originalFile);
          FileOutputStream fos = new FileOutputStream(newFile);
          GZIPOutputStream gzip = new GZIPOutputStream(fos);)
      {
        gzip.write(buf, 0, n);
        byte[] buf = new byte[8192];
        int n;
        while ((n = fis.read(buf)) != -1)
        {
          gzip.write(buf, 0, n);
        }
      }
      gzip.close();
      outputStreamOpen = false;
      fis.close();
      inputStreamOpen = false;
      if(deleteOriginal && !originalFile.delete())
      {
        System.err.println("Cannot delete original file:" + originalFile);
@@ -106,34 +94,7 @@
    } catch(IOException ioe)
    {
      logger.traceException(ioe);
      if (inputStreamOpen)
      {
        try
        {
          fis.close();
        }
        catch (Exception fe)
        {
         logger.traceException(fe);
          // Cannot do much. Ignore.
        }
      }
      if (outputStreamOpen)
      {
        try
        {
          gzip.close();
        }
        catch (Exception ge)
        {
          logger.traceException(ge);
          // Cannot do much. Ignore.
        }
      }
      return false;
    }
  }
}
opendj-server-legacy/src/main/java/org/opends/server/loggers/ZIPAction.java
@@ -61,11 +61,6 @@
  @Override
  public boolean execute()
  {
    FileInputStream fis = null;
    ZipOutputStream zip = null;
    boolean inputStreamOpen = false;
    boolean outputStreamOpen = false;
    try
    {
      if(!originalFile.exists())
@@ -74,27 +69,20 @@
        return false;
      }
      fis = new FileInputStream(originalFile);
      inputStreamOpen = true;
      FileOutputStream fos = new FileOutputStream(newFile);
      zip = new ZipOutputStream(fos);
      outputStreamOpen = true;
      ZipEntry zipEntry = new ZipEntry(originalFile.getName());
      zip.putNextEntry(zipEntry);
      byte[] buf = new byte[8192];
      int n;
      while((n = fis.read(buf)) != -1)
      try (FileInputStream fis = new FileInputStream(originalFile);
          FileOutputStream fos = new FileOutputStream(newFile);
          ZipOutputStream zip = new ZipOutputStream(fos))
      {
        zip.write(buf, 0, n);
      }
        ZipEntry zipEntry = new ZipEntry(originalFile.getName());
        zip.putNextEntry(zipEntry);
      zip.close();
      outputStreamOpen = false;
      fis.close();
      inputStreamOpen = false;
        byte[] buf = new byte[8192];
        int n;
        while ((n = fis.read(buf)) != -1)
        {
          zip.write(buf, 0, n);
        }
      }
      if(deleteOriginal && !originalFile.delete())
      {
@@ -106,34 +94,7 @@
    } catch(IOException ioe)
    {
      logger.traceException(ioe);
      if (inputStreamOpen)
      {
        try
        {
          fis.close();
        }
        catch (Exception fe)
        {
          logger.traceException(fe);
          // Cannot do much. Ignore.
        }
      }
      if (outputStreamOpen)
      {
        try
        {
          zip.close();
        }
        catch (Exception ze)
        {
          logger.traceException(ze);
          // Cannot do much. Ignore.
        }
      }
      return false;
    }
  }
}