improve reader and writer

This commit is contained in:
Sebastian Sdorra
2010-09-19 17:09:38 +02:00
parent ff21849582
commit 2ceb785838
4 changed files with 61 additions and 76 deletions

View File

@@ -7,10 +7,6 @@
package sonia.scm.io;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.ByteArrayInputStream;
@@ -52,20 +48,7 @@ public abstract class AbstractReader<T>
*/
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));
}
/**
@@ -95,19 +78,6 @@ public abstract class AbstractReader<T>
*/
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));
}
}

View File

@@ -49,17 +49,7 @@ public abstract class AbstractWriter<T>
*/
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));
}
/**

View File

@@ -7,6 +7,10 @@
package sonia.scm.io;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.BufferedReader;
@@ -44,47 +48,55 @@ public class INIConfigurationReader extends AbstractReader<INIConfiguration>
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);
if (sectionMatcher.matches())
while (line != null)
{
String name = sectionMatcher.group(1);
line = line.trim();
if (section != null)
Matcher sectionMatcher = sectionPattern.matcher(line);
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);
line = reader.readLine();
}
else if ((section != null) &&!line.startsWith(";")
&&!line.startsWith("#"))
if (section != null)
{
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);
}
configuration.addSection(section);
}
line = reader.readLine();
}
if (section != null)
finally
{
configuration.addSection(section);
Util.close(input);
}
return configuration;

View File

@@ -7,6 +7,10 @@
package sonia.scm.io;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
@@ -33,13 +37,22 @@ public class INIConfigurationWriter extends AbstractWriter<INIConfiguration>
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);
writer.flush();
for (INISection section : object.getSections())
{
writer.println(section.toString());
}
writer.flush();
}
finally
{
Util.close(writer);
}
}
}