1. Sebastian Sdorra
  2. scm-manager

Commits

Sebastian Sdorra  committed 98e7c5d

improve reader and writer

  • Participants
  • Parent commits 818fba1
  • Branches default

Comments (0)

Files changed (4)

File scm-core/src/main/java/sonia/scm/io/AbstractReader.java

View file
  • Ignore whitespace
 
 package sonia.scm.io;
 
-//~--- non-JDK imports --------------------------------------------------------
-
-import sonia.scm.util.Util;
-
 //~--- JDK imports ------------------------------------------------------------
 
 import java.io.ByteArrayInputStream;
    */
   public T read(byte[] data) throws IOException
   {
-    T result = null;
-    InputStream input = null;
-
-    try
-    {
-      input = new ByteArrayInputStream(data);
-      result = read(input);
-    }
-    finally
-    {
-      Util.close(input);
-    }
-
-    return result;
+    return read(new ByteArrayInputStream(data));
   }
 
   /**
    */
   public T read(File file) throws IOException
   {
-    T result = null;
-    InputStream input = null;
-
-    try
-    {
-      input = new FileInputStream(file);
-      result = read(input);
-    }
-    finally
-    {
-      Util.close(input);
-    }
-
-    return result;
+    return read(new FileInputStream(file));
   }
 }

File scm-core/src/main/java/sonia/scm/io/AbstractWriter.java

View file
  • Ignore whitespace
    */
   public void write(T object, File file) throws IOException
   {
-    OutputStream output = null;
-
-    try
-    {
-      output = new FileOutputStream(file);
-      write(object, output);
-    }
-    finally
-    {
-      Util.close(output);
-    }
+    write(object, new FileOutputStream(file));
   }
 
   /**

File scm-core/src/main/java/sonia/scm/io/INIConfigurationReader.java

View file
  • Ignore whitespace
 
 package sonia.scm.io;
 
+//~--- non-JDK imports --------------------------------------------------------
+
+import sonia.scm.util.Util;
+
 //~--- JDK imports ------------------------------------------------------------
 
 import java.io.BufferedReader;
   public INIConfiguration read(InputStream input) throws IOException
   {
     INIConfiguration configuration = new INIConfiguration();
-    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
-    INISection section = null;
-    String line = reader.readLine();
 
-    while (line != null)
+    try
     {
-      line = line.trim();
+      BufferedReader reader = new BufferedReader(new InputStreamReader(input));
+      INISection section = null;
+      String line = reader.readLine();
 
-      Matcher sectionMatcher = sectionPattern.matcher(line);
+      while (line != null)
+      {
+        line = line.trim();
 
-      if (sectionMatcher.matches())
-      {
-        String name = sectionMatcher.group(1);
+        Matcher sectionMatcher = sectionPattern.matcher(line);
 
-        if (section != null)
+        if (sectionMatcher.matches())
         {
-          configuration.addSection(section);
+          String name = sectionMatcher.group(1);
+
+          if (section != null)
+          {
+            configuration.addSection(section);
+          }
+
+          section = new INISection(name);
+        }
+        else if ((section != null) &&!line.startsWith(";")
+                 &&!line.startsWith("#"))
+        {
+          int index = line.indexOf("=");
+
+          if (index > 0)
+          {
+            String key = line.substring(0, index).trim();
+            String value = line.substring(index + 1, line.length()).trim();
+
+            section.setParameter(key, value);
+          }
         }
 
-        section = new INISection(name);
-      }
-      else if ((section != null) &&!line.startsWith(";")
-               &&!line.startsWith("#"))
-      {
-        int index = line.indexOf("=");
-
-        if (index > 0)
-        {
-          String key = line.substring(0, index).trim();
-          String value = line.substring(index + 1, line.length()).trim();
-
-          section.setParameter(key, value);
-        }
+        line = reader.readLine();
       }
 
-      line = reader.readLine();
+      if (section != null)
+      {
+        configuration.addSection(section);
+      }
     }
-
-    if (section != null)
+    finally
     {
-      configuration.addSection(section);
+      Util.close(input);
     }
 
     return configuration;

File scm-core/src/main/java/sonia/scm/io/INIConfigurationWriter.java

View file
  • Ignore whitespace
 
 package sonia.scm.io;
 
+//~--- non-JDK imports --------------------------------------------------------
+
+import sonia.scm.util.Util;
+
 //~--- JDK imports ------------------------------------------------------------
 
 import java.io.IOException;
   public void write(INIConfiguration object, OutputStream output)
           throws IOException
   {
-    PrintWriter writer = new PrintWriter(output);
+    PrintWriter writer = null;
 
-    for (INISection section : object.getSections())
+    try
     {
-      writer.println(section.toString());
+      writer = new PrintWriter(output);
+
+      for (INISection section : object.getSections())
+      {
+        writer.println(section.toString());
+      }
+
+      writer.flush();
     }
-
-    writer.flush();
+    finally
+    {
+      Util.close(writer);
+    }
   }
 }