mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 17:05:43 +01:00
improve utils
This commit is contained in:
@@ -9,7 +9,7 @@ package sonia.scm.io;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -96,7 +96,7 @@ public class INIConfigurationReader extends AbstractReader<INIConfiguration>
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.close(input);
|
||||
IOUtil.close(input);
|
||||
}
|
||||
|
||||
return configuration;
|
||||
|
||||
@@ -9,7 +9,7 @@ package sonia.scm.io;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -52,7 +52,7 @@ public class INIConfigurationWriter extends AbstractWriter<INIConfiguration>
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.close(writer);
|
||||
IOUtil.close(writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package sonia.scm.io;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -66,8 +66,8 @@ public class RegexResourceProcessor extends AbstractResourceProcessor
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.close(reader);
|
||||
Util.close(writer);
|
||||
IOUtil.close(reader);
|
||||
IOUtil.close(writer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ package sonia.scm.io;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -150,7 +150,7 @@ public class SimpleCommand implements Command
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.close(input);
|
||||
IOUtil.close(input);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.xml.bind.JAXB;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -115,8 +116,8 @@ public abstract class AbstractSimpleRepositoryHandler<T extends SimpleRepository
|
||||
|
||||
if (directory.exists() && repositoryFile.exists())
|
||||
{
|
||||
Util.delete(directory);
|
||||
Util.delete(repositoryFile);
|
||||
IOUtil.delete(directory);
|
||||
IOUtil.delete(repositoryFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
192
scm-core/src/main/java/sonia/scm/util/IOUtil.java
Normal file
192
scm-core/src/main/java/sonia/scm/util/IOUtil.java
Normal file
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class IOUtil
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final Logger logger = Logger.getLogger(IOUtil.class.getName());
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param closeable
|
||||
*/
|
||||
public static void close(Closeable closeable)
|
||||
{
|
||||
if (closeable != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
closeable.close();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param reader
|
||||
* @param writer
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void copy(Reader reader, Writer writer) throws IOException
|
||||
{
|
||||
char[] buffer = new char[0xFFFF];
|
||||
|
||||
for (int len; (len = reader.read(buffer)) != -1; )
|
||||
{
|
||||
writer.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param in
|
||||
* @param out
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void copy(InputStream in, OutputStream out) throws IOException
|
||||
{
|
||||
byte[] buffer = new byte[0xFFFF];
|
||||
|
||||
for (int len; (len = in.read(buffer)) != -1; )
|
||||
{
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param reader
|
||||
* @param writer
|
||||
*/
|
||||
public static void copyThread(Reader reader, Writer writer)
|
||||
{
|
||||
new Thread(new IOCopyThread(reader, writer)).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param file
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void delete(File file) throws IOException
|
||||
{
|
||||
if (file.isDirectory())
|
||||
{
|
||||
File[] children = file.listFiles();
|
||||
|
||||
if (children != null)
|
||||
{
|
||||
for (File child : children)
|
||||
{
|
||||
delete(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!file.delete())
|
||||
{
|
||||
throw new IOException("could not delete file ".concat(file.getPath()));
|
||||
}
|
||||
}
|
||||
|
||||
//~--- inner classes --------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class description
|
||||
*
|
||||
*
|
||||
* @version Enter version here..., 10/09/28
|
||||
* @author Enter your name here...
|
||||
*/
|
||||
private static class IOCopyThread implements Runnable
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param reader
|
||||
* @param writer
|
||||
*/
|
||||
public IOCopyThread(Reader reader, Writer writer)
|
||||
{
|
||||
this.reader = reader;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
//~--- methods ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
copy(reader, writer);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
close(reader);
|
||||
close(writer);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields -------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Reader reader;
|
||||
|
||||
/** Field description */
|
||||
private Writer writer;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -39,75 +41,6 @@ public class Util
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param closeable
|
||||
*/
|
||||
public static void close(Closeable closeable)
|
||||
{
|
||||
if (closeable != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
closeable.close();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param in
|
||||
* @param out
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void copy(InputStream in, OutputStream out) throws IOException
|
||||
{
|
||||
byte[] buffer = new byte[0xFFFF];
|
||||
|
||||
for (int len; (len = in.read(buffer)) != -1; )
|
||||
{
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param file
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void delete(File file) throws IOException
|
||||
{
|
||||
if (file.isDirectory())
|
||||
{
|
||||
File[] children = file.listFiles();
|
||||
|
||||
if (children != null)
|
||||
{
|
||||
for (File child : children)
|
||||
{
|
||||
delete(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!file.delete())
|
||||
{
|
||||
throw new IOException("could not delete file ".concat(file.getPath()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user